1, Introduction
1 threshold
The simplest way to block an image is to set a threshold to binarize the image. How should we choose this threshold
For the image with obvious boundary in the histogram of the image, we can easily find this threshold, but if the boundary of the histogram of the image is not obvious, it will become very difficult to find this threshold. Therefore, we have global threshold and local threshold.
2 global threshold
Global threshold is that we only have one threshold to binarize the image in the whole image, but it has its limitations. For example, when there is Gaussian noise in the image, we can't find a good threshold to separate the boundaries of the image
In addition, if the image boundary appears under local contrast, that is, the threshold is different at different locations, the effect of global threshold is also very bad.
Regardless of the shortcomings of the global threshold, let'S see how we can obtain the global threshold T through calculation? We also OTSU'S algorithm.
Here are some mathematical concepts embodied in the image
OTSU algorithm is to divide the image into two blocks, and then maximize the variance between the two blocks, that is, to maximize the square of the difference between the mean of the two blocks and the global mean
Since there is only such a threshold, we can simply make this value traverse 0-255 and find it σ B is the maximum value, which is the threshold we want. We can understand that this value divides the image into two farthest pieces. We have the function of graythresh in matlab to realize this process.
The following figure shows an example where the OTSU algorithm is not ideal.
In order to overcome the above shortcomings, we have two solutions: 1 First denoise through low-pass filter, and then OTSU 2 Only the pixels of the edge part are considered to calculate the threshold, which can greatly reduce the influence of other unimportant parts on the threshold calculation process
3 local threshold
Let's look at local threshold / adaptive threshold. Its principle is to divide the image into blocks and apply different thresholds to different parts. In matlab, we have the function blockproc to realize this process
We can see that the effect is indeed greatly improved compared with before, but the disadvantage is particularly obvious, that is, the image will be segmented
We can adjust the size of the block, but in this case, if the pixel value in the block does not change much, all the pixels in the block are divided into black or white and the boundary is missing (for example, there is a white pixel block in the black window frame above the window in the upper right corner). Therefore, the selection of the block is very important.
A better way is to calculate the threshold in an area around each pixel, and calculate whether the value of this pixel is 1 or 0 according to the mean variance in this block
4 RGB map threshold
In addition to applying the threshold to the gray image, we can also apply it to the RGB image. We can set a color to get objects with similar colors
For example, if we take the color of the ribbon in the figure below, we can get the result as shown in the figure on the right
2, Source code
close all; clear; clc; warning off all; %% Type I scratch 1-1.jpg 1-2.jpg I = imread('1-1.jpg'); IGrey = rgb2gray(I); %IGrey = adapthisteq(IGrey); % Contrast-limited adaptive histogram equalization (CLAHE) Ibw = im2bw(IGrey);% Ibw It is a binary image and does not need to calculate the threshold Ibw = ~Ibw; Ibw = bwareaopen(Ibw,20) ;%Will be less than XX Pixel unit removal figure, subplot(1,2,1) imshow(I); title('Original drawing'); subplot(1,2,2) imshow(Ibw); title('Scratch detection diagram'); I = imread('1-2.jpg'); IGrey = rgb2gray(I); level = graythresh(IGrey) Ibw = im2bw(IGrey,level);% Ibw It is a binary image and does not need to calculate the threshold Ibw = ~Ibw; figure, subplot(1,2,1) imshow(I); title('Original drawing'); subplot(1,2,2) imshow(Ibw); title('Scratch detection diagram'); %% Type II scratch 2-1.jpg 2-2.jpg I = imread('2-1.jpg'); IGrey = rgb2gray(I); w2 = fspecial('average',[15 15]); %% Define a filter first IMean = imfilter(IGrey,w2,'replicate'); %%Let the image pass through the filter result = abs(imsubtract(im2double(IGrey),im2double(IMean))); maxValue = max(max(result)); threshod = maxValue * 0.3; result(result >= threshod) = 1; result(result < threshod) = 0; Ibw = bwareaopen(im2uint8(result),10);%Will be less than XX Pixel unit removal figure, subplot(1,2,1) imshow(I); title('Original drawing'); subplot(1,2,2) imshow(Ibw); title('Scratch detection diagram'); I = imread('2-2.jpg'); IGrey = rgb2gray(I); w2 = fspecial('average',[15 15]); %% Define a filter first IMean = imfilter(IGrey,w2,'replicate'); %%Let the image pass through the filter result = abs(imsubtract(im2double(IGrey),im2double(IMean))); maxValue = max(max(result));
3, Operation results
4, matlab version and references
1 matlab version
2014a
2 references
[1] Cai Limei MATLAB image processing -- theory, algorithm and example analysis [M] Tsinghua University Press, 2020
[2] Yang Dan, Zhao Haibin, long Zhe Detailed explanation of MATLAB image processing example [M] Tsinghua University Press, 2013
[3] Zhou pin MATLAB image processing and graphical user interface design [M] Tsinghua University Press, 2013
[4] Liu Chenglong Proficient in MATLAB image processing [M] Tsinghua University Press, 2015