Self MATLAB programming threshold image segmentation

Realization of ideas

  1. Rendering the original image histogram, histogram and the estimated observed values ​​of the gradation range corresponding to the two peaks
  2. Search the bottom (in the range of tone values of two peaks corresponding to the minimum value ) corresponding to the gradation value
  3. The bottom part of the searched corresponding to the gradation value of the gradation threshold value, and larger or smaller than the gray threshold value for the corresponding treatment
    Looking threshold

The gradation values ​​of all pixels in the image that is greater than the threshold of points of the object, called the target point; grayscale images those pixels less than or equal to the threshold value are considered background dots, referred to as background points.

In this case there are many ways to process, can be adjusted according to actual situation:

  • Thresholding method gradation value not less than the threshold values of the pixels and the gray scale value is less than the threshold values of the pixels are set to 0 and 1, in particular to see split bright object from a dark background in the still bright background segmentation dark It objects.
  • Semi thresholding method than the threshold value of the bright pixel gray value remains unchanged, and the small threshold of dark pixels to black; gray value or smaller than the threshold value of the dark pixel remains constant, but will larger than the threshold value of bright pixels to white.

Achieve results

Segmentation

Reference Code

Used in matlab cameraman carrying image processing, two-dimensional grayscale image, size (im, 1), size (im, 2) respectively corresponding to the ranks of the length of the two-dimensional array of grayscale.

im=imread('cameraman.tif');
im2=imhist(im);		//im2为原图像对应的灰度直方图,数组索引号代表灰度值,索引号所对应的值为该灰度值下像素点的个数
min=size(im,1)*size(im,2); 
minindex=0;
figure('name','灰度直方图')
imhist(im)
figure('name','半阈值化图像分割')
subplot(1,2,1)
imshow(im)
title('原图像')
for i=10:165		//观察灰度直方图估计两个峰值对应的灰度值范围
    if(im2(i)<min)
        min=im2(i);
        minindex=i;	//注意索引号对应的才是灰度值!!
    end
end
for i=1:size(im,1)
    for j=1:size(im,2)
        if(im(i,j)>=minindex)
            im(i,j)=255;	//半阈值化的方法,从亮的背景中分割出暗的物体
        end
    end
end
subplot(1,2,2)
imshow(im)
title('阈值分割图像')
Published 18 original articles · won praise 40 · views 50000 +

Guess you like

Origin blog.csdn.net/seawaysyyy/article/details/103319837