Matlab|Image Processing 04|Image Segmentation-Threshold Segmentation Method

1. Artificial threshold segmentation method threshold_test1.m

1. Analyze the impact of modifying the threshold on the segmentation results

Analysis: Taking the gray value of the first valley in the histogram as the threshold, the image segmentation effect is better. When the threshold changes, part of the information in the segmented image is lost. In this figure, when the threshold decreases, the segmented image has more black parts, and when the threshold increases, the segmented image has more white parts.
Insert image description here

[I,map]=imread('cameraman.tif');   % 读取灰度图像  
imshow(I);figure;                     % 显示原图图像
J=imhist(I);imhist(I);              % 提取图像中的直方图信息;将图像转换为直方图信息
[M,N]=size(I);                        % 直方图信息的高和宽
for i=1:1:M                           
for j=1:1:N                           % 遍历像素点
    if I(i,j)>80                      % 阈值为80
        g(i,j)=0;                      %大于80的点取0(黑色
    else g(i,j)=1;                    % 小于等于80的点取1(白色
       end
 end
end
figure;imshow(g);                     % 显示手动阈值分割后的图像

2. Iterative threshold segmentation method-automatic threshold method threshold_test2.m

%2 迭代阈值分割
f=imread('cameraman.tif');            %读取图像       
subplot(1,2,1);imshow(f);           
title('原始图像');                   
f=double(f);                          % 返回double类型   
T=(min(f(:))+max(f(:)))/2;            % 求原图大小平均值,并作为初始阈值T       
done=false;                           % 赋值,if(false)  
i=0;                                  % 赋值,迭代次数
while~done                            % 与更新后的done比较   
    r1=find(f<=T);                    % 小于阈值的点,作为目标    
    r2=find(f>T);                     % 大于阈值的点,作为背景
    Tnew=(mean(f(r1))+mean(f(r2)))/2; % 计算新的阈值Tnew,目标灰度值和背景都取平均
done=abs(Tnew-T)<1;                   % 迭代至两次阈值的灰度变化不超过1时停止
    T=Tnew;                           % 更新阈值T  
    i=i+1;                            % 更新迭代次数   
end
f(r1)=0;                              % 进行二值分割,小于最终的阈值的点为0(黑色),    
f(r2)=1;                              % 大于最终的阈值的点为1(白色)             
subplot(1,2,2);                       
imshow(f);                          
title('迭代阈值二值化图像');  

3. Adaptive threshold method

f=imread('cameraman.tif');
figure,subplot(1,2,1),imshow(f),title('原始图像');
h = imbinarize(f,'adaptive','ForegroundPolarity','dark','Sensitivity',0.4);
 %自适应阈值分割,ForegroundPolarity 参数指示前景比背景暗
subplot(1,2,2),imshow(h),title('自适应阈值分割图像');

Insert image description here

4. Compare the segmentation results of manual threshold (threshold_test1.m), automatic threshold (threshold_test2.m), adaptive threshold and watershed segmentation algorithm (Water tset.m), and analyze their advantages and disadvantages.

Answer:
1. The threshold segmentation algorithm can complete image segmentation quickly, but it considers grayscale information but not spatial information. It is not suitable for multi-channel images and does not It is suitable for images with little difference in feature values ​​and is sensitive to noise and grayscale unevenness. It does not perform well for image segmentation with a small grayscale difference between the background and the target area. Manual threshold segmentation using the bimodal method requires certain prior knowledge of images, because the same histogram can correspond to several different images. The automatic threshold method iteration method is only suitable for situations where the histogram has obvious troughs, and is not ideal for other situations.
2. The idea of ​​the adaptive threshold method is not to calculate the threshold of the global image, but to calculate the local threshold according to the brightness distribution of different areas of the image. Therefore, different thresholds can be calculated adaptively for different areas of the image. threshold, so it is called adaptive threshold method. The advantage is that it is very little affected by changes in image contrast and brightness under certain conditions. The disadvantage is that this method only relies on the pixel grayscale information of the image and does not consider the spatial correlation information between pixels (such as neighborhood information). In addition, due to the existence of external interference, the peaks and troughs of the grayscale histogram are not necessarily obvious. Poor anti-interference performance.
3. The watershed algorithm can deal with cell adhesion problems and has high segmentation accuracy. However, due to the presence of noise points or other interference factors, there is often a phenomenon of over-segmentation. This is because the existence of too many local extreme points creates many small catchment basins, resulting in the segmented image being unable to make sense in the image. area is represented.

Guess you like

Origin blog.csdn.net/ariarko/article/details/128541536