A list,

1 the threshold

The simplest way to divide an image into blocks is to set a threshold for binarization of the image. Then how should we choose this threshold



It is easy to find this threshold for images with obvious boundary of image histogram, but if the boundary of image histogram is not obvious, it will be very difficult to find this threshold. So we have global thresholds and local thresholds.



2 Global threshold

Global threshold means that there is only one threshold in the whole image to binarize the image, but it has its limitations. For example, in the case of gaussian noise in the image, we cannot find a good threshold to separate the image boundary



In addition, if the boundary of the image appears under local comparison, that is, the threshold value at different positions is different, the effect of global threshold value is also very bad.



So let’s ignore the disadvantages of the global threshold, and let’s see how do we compute this global threshold T? We have OTSU’s algorithm.



Here are some of the mathematical concepts represented graphically



OTSU algorithm is to divide the image into two blocks and 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 one such threshold, we can simply run the value from 0 to 255 to find the maximum value of σB, which is the desired threshold. We can think of it as dividing the image into two pieces that are farthest apart. We have a graythresh function in Matlab to do this.



The figure below shows an example of an OTSU algorithm that is not ideal.



In order to overcome the above shortcomings, we have two solutions: 1. De-noising through low-pass filter and then using OTSU 2. Only consider the pixels of the edge part 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/adaptive thresholds. The idea is to divide the image into chunks and apply different thresholds to different parts. In Matlab we have blockProc to do this



We did see a big improvement over what we had seen before, but the downside was that the images were chunked



We can adjust the size of the block, but if the pixel values within the block don’t change much, the block will be completely black or white without borders (for example, white pixels in the black window frame above the window in the upper right corner). So the selection of blocks is very important.



A better way is to calculate the threshold in a region around each pixel, and determine whether the pixel has a value of 1 or 0 based on the variance of the mean within the block





4 RGB image threshold

In addition to applying thresholds to grayscale images, we can also apply them to RGB images, where we can set a color to get objects that are close to that color



For example, if we take the color of the ribbon below, we can get the result as shown in the picture on the right

Ii. Source code

close all; clear; clc; warning off all; %% First class scratches11.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 is a binary image and does not require a threshold Ibw = ~Ibw; Ibw = bwareaopen(Ibw,20); % Remove the units smaller than XX pixels figure, subplot(1.2.1) 
imshow(I);
title('original');
subplot(1.2.2)
imshow(Ibw);
title('Scratch chart');

I = imread('1-2.jpg'); IGrey = rgb2gray(I); level = graythresh(IGrey) Ibw = im2bw(IGrey,level); % Ibw is a binary image and does not require a threshold Ibw = ~Ibw; figure, subplot(1.2.1) 
imshow(I);
title('original');
subplot(1.2.2)
imshow(Ibw);
title('Scratch chart'); %% Second class scratches21.jpg 2- 2.jpg
I = imread('2-1.jpg');
IGrey = rgb2gray(I);
w2 = fspecial('average'[15 15]); IMean = imfilter(IGrey,w2,'replicate'); %% passes the image 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); % Remove the units smaller than XX pixels figure, subplot(1.2.1) 
imshow(I);
title('original');
subplot(1.2.2)
imshow(Ibw);
title('Scratch chart');

I = imread('2-2.jpg');
IGrey = rgb2gray(I);
w2 = fspecial('average'[15 15]); IMean = imfilter(IGrey,w2,'replicate'); %% passes the image through the filter result =abs(imsubtract(im2double(IGrey),im2double(IMean)));
maxValue = max(max(result));
Copy the code

3. Operation results







Fourth, note

Version: 2014 a