For simple grayscale image, if the gray target and background there are some differences, it can be used to extract certain threshold. The key is to determine the threshold value, commonly used methods are:
Modal method, P parameter method, the variable threshold method, Otsu iterative approximation method and the like.
Modal Method:
Histogram trough taken as the threshold value. It applies to object and background gray quite different, each target and background histogram peak a grayscale image.
If the histogram is more intense unevenness, difficult to find trough, averaging can be treated with relatively smooth art points to the histogram.
P parameter method:
When the proportion of the target area of the image is known, and the maximum value is greater than the target minimum gray background gray value, P parameter method may be employed. In practice, if the target gray close to white, then
Start pixels cumulative histogram on the right side, until the cumulative total number of pixels of pixels a proportion of P, the gradation current as the threshold value (or accumulated from the left).
For example, FIG moon Proportion as 0.45, and the lowest gray background gray which overlap slightly, as the threshold determination 75, the final target base extraction.
The variable threshold method:
When changing the image gray background, you can set different thresholds for different parts of the image, so that the interference can be removed well.
FIG dandelion extract the example, the upper image multiple illumination, mainly dandelion feathers, a lower and less light, and predominantly Stems, taken on the middle and lower thresholds of different extraction effect will be much better.
Otsu method:
Application of Otsu of the most extensive and have a good adaptive threshold. Suitable for batch extraction or a moving image grayscale image extraction (however it turns out, when Otsu met reflective interference will be very serious).
Otsu principle is as follows:
Segmentation threshold T is set, whereby the divided region denoted by R & lt . 1 and R & lt 2 .
R & lt . 1 proportion of total pixels of the image [theta] . 1 , R & lt 2 accounts for a proportion of total pixel image [theta] 2 .
Average gray value of the image [mu] 0 , R & lt . 1 average gray of [mu] . 1 , R & lt 2 average gray of [mu] 2 .
Obviously there are:
s 0 = h 1 * s 1 + h 2 * m 2
If the threshold is selected to be proper, then R & lt . 1 and R & lt 2 gradation difference should be large, the [mu] . 1 and [mu] 0 , [mu] 2 and [mu] 0 the absolute value of the difference also large.
Defined between-class variance to the reaction gradation difference Size:
p 2 = i 1 (m 1 -m 0 ) 2 + i 2 (m 2 -m 0 ) 2
For each selected threshold T, we have a corresponding σ, σ and the maximum threshold value T is the optimal threshold.
In addition, despite the simplified formula to reduce the number of iterations of the method ( https://www.cnblogs.com/kensporger/p/11270452.html ), the amount of computation process Otsu is still very large.
Iterative approximation method:
This is a simplified version of Otsu method, the following steps:
Taking the maximum and minimum average gray gradation (the image may be an average gray scale) to the initial threshold T, thereby to obtain two regions of R & lt . 1 and R & lt 2
Calculation R & lt . 1 and R & lt 2 gray value of [mu] . 1 , [mu] 2
Selecting a new threshold = T ([mu] . 1 + [mu] 2 ) / 2, repeat the above steps until no change in T or varied within a certain range.
The effect of using the method shown in FIG, probably only about five iterations:
However, the method of determining the threshold is very rough, often with the optimal threshold will have greater deviations, such as the moon before the extraction, the performance was just not very good:
Most matlab simulation test code above are listed below:
% Determination threshold parameter value p law function thres is = pthres (File, p) IMGA = imread (File); % grayscale IMGA = rgb2gray (IMGA); % Get size [SIZEX, sizeY] = size (IMGA); % histograms Histogram = zeros ( 256 , . 1 ); for I = . 1 : SIZEX for J = . 1 : sizeY Histogram (IMGA (I, J) + . 1 ) = Histogram (IMGA (I, J) + . 1 ) + . 1 ; End end %cumulative pixel pixels_cnt = 0 ; thres is = 256 ; the while (pixels_cnt <P * SIZEX * sizeY) pixels_cnt = pixels_cnt + Histogram (thres is); thres is = thres- . 1 ; end % based on the threshold value binarization mybw = imbinarize (IMGA, (thres is - . 1 ) / 255 ); the subplot ( . 1 , 2 , . 1 ); imshow (IMGA); title ( ' original ' ); the subplot ( . 1, 2, 2 ); imshow (mybw); title ( ' P parameter binarization method ' ); End
%多阈值分割实例 imga=imread('multi_thres.jpg'); imga=rgb2gray(imga); single = imbinarize(a,85/255); multi(250:300,:) = imbinarize(a(250:300,:),85/255); multi(150:250,:) = imbinarize(a(150:250,:),150/255); multi(1:150,:) = imbinarize(a(. 1 : 150 , :), 170. / 255 ); the subplot ( . 1 , . 3 , . 1 ); imshow (IMGA); title ( ' original ' ); the subplot ( . 1 , . 3 , 2 ); imshow (SINGLE); title ( ' single threshold process " ); the subplot ( . 1 , . 3 , . 3 ); imshow (multi); title ( ' multi-thresholding ' );
% Iterative approximation function thres is = Iterator (File) IMGA = imread (File); % grayscale IMGA = rgb2gray (IMGA); % Get size [SIZEX, sizeY] = size (IMGA); % histogram total gray statistical gray_sum = 0 ; Histogram = zeros ( 256 , . 1 ); for I = . 1 : SIZEX for J = . 1 : sizeY Histogram (IMGA (I, J) + . 1 ) = Histogram (IMGA (I, J) + . 1 ) +1; gray_sum= gray_sum+double(imga(i,j)+1); end end %初始阈值 thres=gray_sum/(sizex*sizey); old_thres=0; %迭代逼近 while(abs(thres-old_thres)>1) disp(thres); u1=0;u2=0; cnt1=0;cnt2=0; for i=1:thres u1= u1 + histogram (i) * i;% average gradation statistical R1 region CNT1 = CNT1 + Histogram (I); End for I = 256 : - . 1 : thres is U2 = U2 + Histogram (I) * I;% statistical R2 region average gray CNT2 = CNT2 + Histogram (I); End U1 = U1 / CNT1; U2 = U2 / CNT2; old_thres = thres is; thres is = (U1 + U2) / 2 ;% new threshold End mybw = imbinarize (IMGA, (thres is - 1) / 256 ); the subplot ( . 1 , 2 , . 1 ); imshow (IMGA); title ( ' original ' ); the subplot ( . 1 , 2 , 2 ); imshow (mybw); title ( ' iterative approximation method binarization ' ); End