数字图像处理之matlab实验(六):图像分割

在图像处理领域,我们更关注的是一些关于目标的分析或者描述信息,比如图片里面是否有猫,以及是什么品种的猫?在在做这一步之前,我们需要先把图像中的猫分割出来。可以说图像分割是最基础也是最重要的一步操作,会影响后面的识别工作的准确性。

图像分割就是将图像分成前景(目标)图像和背景图像。以上图为例,我们想要分割出猫,就需要找到猫的边界。因此图像分割也可以用一些边缘检测算法实现。

1、基于双峰阈值法图像分割

这类算法仅仅适用于部分容易找到阈值的情况。

clear,clc,close all;
Image=rgb2gray(imread('lotus1.jpg'));
subplot(221),imshow(Image),title('原始图像');
%imhist(Image);
hist1=imhist(Image);
hist2=hist1;
iter=0;
while 1
    [is,peak]=Bimodal(hist1);
    if is==0
        hist2(1)=(hist1(1)*2+hist1(2))/3;
        for j=2:255
            hist2(j)=(hist1(j-1)+hist1(j)+hist1(j+1))/3;
        end
        hist2(256)=(hist1(255)+hist1(256)*2)/3;
        hist1=hist2;
        iter=iter+1;
        if iter>1000
            break;
        end
    else
        break;
    end
end

[trough,pos]=min(hist1(peak(1):peak(2)));
thresh=pos+peak(1);
subplot(222),stem(1:256,hist1,'Marker','none');
hold on
stem([thresh,thresh],[0,trough],'Linewidth',2);
hold off
result=zeros(size(Image));
result(Image>thresh)=1;
subplot(223),imshow(result),title('基于双峰直方图的阈值化');
imwrite(result,'bilotus1.jpg'); 
function [is,peak]=Bimodal(histgram)
    count=0;
    for j=2:255
        if histgram(j-1)<histgram(j) && histgram(j+1)<histgram(j)
            count=count+1;
            peak(count)=j;
            if count>2 
                is=0;
                return;
            end
        end
    end
    if count==2
        is=1;
    else
        is=0;
    end
end


2、OSTU法二值化图像

Image=rgb2gray(imread('lotus1.jpg'));
subplot(121),imshow(Image),title('原始图像');
T=graythresh(Image);
result=im2bw(Image,T);
subplot(122),imshow(result),title('OTSU方法二值化图像 ');

 3、基于边缘的图像分割

请参考图像锐化部分的边缘提取数字图像处理之matlab实验(三):空间滤波器_苗妮的博客-CSDN博客

4、基于区域生长的图像分割

clear,clc,close all;
Image=im2double(imread('lotus1.jpg'));
[height,width,channel]=size(Image);
if channel==3
    Image=rgb2gray(Image);
end
figure,imshow(Image);
% Image=[1 0 4 6 5 1;1 0 4 6 6 2;0 1 5 5 5 1;0 0 5 6 5 0;0 0 1 6 0 1;1 0 1 2 1 1];
% [height,width,channel]=size(Image);
% figure,imshow(Image);
[seedx,seedy,button] = ginput(1);
seedx=round(seedx);
seedy=round(seedy);
region=zeros(height,width);
region(seedy,seedx)=1;
region_mean=Image(seedy,seedx);
region_num=1;
flag=zeros(height,width);
flag(seedy,seedx)=1;
neighbor=[-1 -1;-1 0;-1 1;0 -1;0 1;1 -1;1 0;1 1];
for k=1:8
    y=seedy+neighbor(k,1);
    x=seedx+neighbor(k,2);
    waiting(k,:)=[y,x];
    flag(y,x)=2;
end

pos=1;
len=length(waiting);
while pos<len
    len=length(waiting);
    current=waiting(pos,:);
    pos=pos+1;
    pixel=Image(current(1),current(2));
    pdist=abs(pixel-region_mean);
    if pdist<40/255 
        region(current(1),current(2))=1;
        region_mean=region_mean*region_num+pixel;
        region_num=region_num+1;
        region_mean=region_mean/region_num;
        for k=1:8   
            newpoint=current+neighbor(k,:);
            if newpoint(1)>0 && newpoint(1)<=height && newpoint(2)>0 && newpoint(2)<width && flag(newpoint(1),newpoint(2))==0
                waiting(end+1,:)=newpoint;
                flag(newpoint(1),newpoint(2))=2;
            end
        end        
    end
end
figure,imshow(region),title('区域生长');
imwrite(region,'regiongrow.jpg');
    

猜你喜欢

转载自blog.csdn.net/u014655960/article/details/127675300