Application of the simplex method to optimize the parameters of rank filtering
This article discusses the implementation and comparison of image processing algorithms in the Julia and MATLAB languages. The main task is to use the simplex method to optimize the parameters of rank filtering in order to maximize the contrast of the barcode image.
The algorithm includes the following steps.
- Rank filtering is noise reduction in an image with adaptive thresholds.
- Contrast calculation — evaluation of the differences between black and white barcode elements.
- Simplex optimization — search for filter parameters that provide the highest possible image quality.
The article presents the code in both languages, provides a visual comparison of the results and analyzes the implementation features.
Pkg.add("Statistics")
Let's start by visualizing the input data. The picture below shows the original barcode image, which we will work with in the future.
using Images
Folder = @__DIR__
puth_to_photo = "$Folder/Img.bmp"
cdata = load(puth_to_photo) # Загружаем изображение
Detailed description of image processing algorithms
1. Rank filtering (rangefilt)
Purpose:
Suppression of pulse noise in the barcode image using adaptive thresholds.
Implementations:
- MATLAB:
rangefilt(Initial_Img, T, step) - Julia:
rangefilt(Initial_Img, T, step=1)
Input parameters:
Initial_Img– input image (brightness matrix),T– a vector of threshold values,step– filter offset step (default is 1).
Output data:
- Filtered image (matrix).
Operating principle:
- for each pixel, its neighborhood is analyzed with the size (2*length(T)+1);
- The median of the neighborhood is calculated;
- The pixel is replaced by the median if the difference with its neighbors exceeds the threshold T.
Key differences between implementations:
| Aspect | MATLAB | Julia |
|---|---|---|
| Typing | Dynamic | Strict with type annotations |
| Border processing | Implicit | Explicit size control |
| Vectorization | Embedded | Using broadcast(.+) |
| Performance | JIT compilation | LLVM compilation |
2. Contrast calculation (contrastBC)
Purpose:
Quantification of differences between black and white barcode elements.
Implementations:
- MATLAB:
[contrast, MeanImg] = contrastBC(Initial_Img) - Julia:
contrast, MeanImg = contrastBC(Initial_Img)
Input parameters:
Initial_Img– the input image after filtering.
Output data:
contrast– numerical contrast estimation (scalar),MeanImg– an image with averaged brightness levels.
The algorithm:
- Calculation of the average brightness profile by columns,
- Automatic threshold detection AdaptiveLevel = (Min + Max)/2,
- Division into black (BElPos) and white (WElPos) elements,
- calculation of contrast using the formula:
contrast = ((MeanW - MeanB)/255)*(Max - Min)/(Max + Min)
Features of implementations
- MATLAB uses the built-in function
meanwith a special output format. - Julia uses statistical processing with explicit dimension indication (
dims=1). - Julia version requires explicit type conversion for arithmetic operations.
3. Simplex optimization
Purpose:
search for optimal filtering parameters that maximize contrast.
Implementations:
- MATLAB:
[Xr, I1, MeanImg] = simplex(Xin, J, I) - Julia:
Xr, I1, MeanImg = simplex(Xin, J, I)
Input parameters:
Xin– initial values of the factors,J– variation intervals,I– the original image.
Output data:
Xr– a matrix of all test points and objective function values,I1– optimal filtered image,MeanImg– an image with highlighted black/white elements.
Key steps of the algorithm:
- construction of the initial simplex (regular polyhedron);
- cyclic execution of operations:
- reflection of the worst vertex,
- calculation of the objective function (contrastBC),
- adaptation of the simplex form;
- The stopping criterion is to achieve 50 iterations.
Comparison of implementations:
| Component | MATLAB | Julia |
|---|---|---|
| Working with matrices | Built-in operators | LinearAlgebra package |
| Indexing | 1-based | 0-based for arrays |
| Memory Management | Implicit | Explicit allocation (similar()) |
| Vectorization | Automatic | Requires . operations |
| OOP approach | Procedural | Multimethods |
Features of Julia implementation:
- explicit indication of types to improve performance,
- the use of broadcasting operations (.+, .*),
- modular structure with the allocation of individual functions,
- multithreading support (not used in this implementation).
Features of MATLAB implementation:
- compact recording of matrix operations,
- automatic processing of boundary conditions,
- built-in support for working with images,
- using a global workspace for data exchange.
Algorithm comparison
using MATLAB
mat"""cd($Folder)"""
@time mat"test_m"
@time include("$Folder/test_jl.jl")
Analyzing the results of the code, you can see a difference in the running time of about two times. It can also be seen that, although the general logic of the algorithms is preserved, the choice of language significantly affects:
- the way boundary conditions are handled.,
- Performance optimization methods,
- Code organization style,
- the possibilities of expanding the functionality.
Julia demonstrates a more modern approach with explicit typing and better optimization capabilities, whereas MATLAB offers a more compact notation for matrix operations.
Visualization of results
function center_text(text::String, width::Int)
padding = " " ^ div(width - length(text), 2)
println(padding * text)
end
# Загружаем изображения
Img_contrastBC_julia = load("$Folder/Img_contrastBC_julia.png")
Img_contrastBC_matlab = load("$Folder/Img_contrastBC_matlab.png")
Img_rangfilt_julia = load("$Folder/Img_rangfilt_julia.png")
Img_rangfilt_matlab = load("$Folder/Img_rangfilt_matlab.png")
# Объединяем изображения
center_text("Julia and MATLAB", 140)
println()
center_text("rangfilt", 140)
display(hcat(Img_rangfilt_julia, Img_rangfilt_matlab))
println()
center_text("contrastBC", 140)
display(hcat(Img_contrastBC_julia, Img_contrastBC_matlab))
As we can see, the images are identical, which indicates the uniformity of the implemented algorithms.
Conclusions
- Both languages successfully completed the task, but MATLAB showed a more concise syntax when working with matrices.
- The implemented simplex method effectively optimized the filtering parameters, which is confirmed by the visual contrast improvement.
- Image comparison after processing (
rangfiltandcontrastBC) demonstrates similar results.
Further research may be aimed at:
- acceleration of calculations in Julia through the use of parallel computing,
- testing on more complex noisy images,
- comparison with other optimization methods (gradient descent, genetic algorithms).

