matlab中图像分割技术之三分水岭分割法

  分水岭算法是一种基于形态学的算法,是对图像的梯度分割。而图像分割的目的是将图像分割成不同的特体,即提取物体的边缘。由于物体边缘的灰度变换比较强烈,而梯度图像正好描述了图像的灰度变换情况。因此,可先将原始图像通过梯度算子边缘检测得到梯度图像,再通过梯度图像的分水岭变换来进行图像分割。因此,采用分水岭算法,利用形态学处理函数,不仅能达到有效分割图像的目的,而且也能消除过分割现象。
  分水岭算法作为一种基于区域的图像分割方法,建立在数学形态学的理论基础之上。20世纪70年代末,Becucher和Lantuejoul提出应用分水岭算法进行图像分割,实现了分水岭算法的模拟侵入过程并成功应用于灰度图像。其后,分水岭算法便作为一种经典的图像分割方法被广泛关注。

clc;                %clc的作用就是清屏幕
clear;              %clear是删除所有的变量
close all;          %close all是将所有打开的图片关掉。
filename=('3.jpg');        %读入图像
f=imread(filename);
imshow(f);
Info=imfinfo(filename);
if Info.BitDepth>8
   f=rgb2gray(f);
end
figure, mesh(double(f));        %显示图像,类似集水盆地,类似集水盆地

matlab中图像分割技术之三分水岭分割法_第1张图片

一、一般分水岭算法

		clc;                %clc的作用就是清屏幕
		clear;              %clear是删除所有的变量
		close all;          %close all是将所有打开的图片关掉。
		filename=('3.jpg');        %读入图像
		f=imread(filename);
		imshow(f);
		Info=imfinfo(filename);
		if Info.BitDepth>8
   			f=rgb2gray(f);
		end
		figure, mesh(double(f));        %显示图像,类似集水盆地,类似集水盆地
        b=im2bw(f, graythresh(f));  %二值化,注意应保证集水盆地的值较低(为0),否则就要对b取反
        d=bwdist(b);                %求零值到最近非零值的距离,即集水盆地到分水岭的距离
        l=watershed(-d);            %MATLAB自带分水岭算法,l中的零值即为风水岭
        w=l==0;                     %取出边缘
        g=b&~w;                     %用w作为mask从二值图像中取值
        figure
        subplot(2,3,1),
        imshow(f);
        subplot(2,3,2),
        imshow(b);
        subplot(2,3,3),
        imshow(d);
        subplot(2,3,4),
        imshow(l);
        subplot(2,3,5),
        imshow(w);
        subplot(2,3,6),
        imshow(g);

matlab中图像分割技术之三分水岭分割法_第2张图片

二、用梯度的两次分水岭分割

		clc;                %clc的作用就是清屏幕
		clear;              %clear是删除所有的变量
		close all;          %close all是将所有打开的图片关掉。
		filename=('3.jpg');        %读入图像
		f=imread(filename);
		imshow(f);
		Info=imfinfo(filename);
		if Info.BitDepth>8
  		 f=rgb2gray(f);
		end
		figure, mesh(double(f));        %显示图像,类似集水盆地,类似集水盆地
                h=fspecial('sobel');                           %获得纵方向的sobel算子
        fd=double(f);
        g=sqrt(imfilter(fd, h, 'replicate').^2+imfilter(fd, h', 'replicate').^2);
        l=watershed(g);                              %分水岭运算
        wr=l==0;
        g2=imclose(imopen(g, ones(3,3)), ones(3,3));          %进行开闭运算对图像进行平滑
        l2=watershed(g2);                            %再次进行分水岭运算
        wr2=l2==0;
        f2=f;
        f2(wr2)=255;
        figure
        subplot(2,3,1),
        imshow(f);
        subplot(2,3,2),
        imshow(g);
        subplot(2,3,3),
        imshow(l);
        subplot(2,3,4),
        imshow(g2);
        subplot(2,3,5),
        imshow(l2);
        subplot(2,3,6),
        imshow(f2);

matlab中图像分割技术之三分水岭分割法_第3张图片

三、使用梯度加掩模的三次分水岭算法

		clc;                %clc的作用就是清屏幕
		clear;              %clear是删除所有的变量
		close all;          %close all是将所有打开的图片关掉。
		filename=('3.jpg');        %读入图像
		f=imread(filename);
		imshow(f);
		Info=imfinfo(filename);
		if Info.BitDepth>8
 		  f=rgb2gray(f);
		end
		figure, mesh(double(f));        %显示图像,类似集水盆地,类似集水盆地
                       h=fspecial('sobel');            %获得纵方向的sobel算子
        fd=double(f);
        g=sqrt(imfilter(fd, h, 'replicate').^2+imfilter(fd, h', 'replicate').^2);
        l=watershed(g);                 %分水岭运算
        wr=l==0;
        rm=imregionalmin(g);            %计算图像的区域最小值定位
        im=imextendedmin(f,2);          %上面仅是产生最小值点
        fim=f;
        fim(im)=175;                    %将im在原图上标识出,用以观察
        lim=watershed(bwdist(im));      %再次分水岭计算
        em=lim==0;
        g2=imimposemin(g, im|em);       %在梯度图上标出im和em
        l2=watershed(g2);               %第三次分水岭计算
        f2=f;
        f2(l2==0)=255;                  %从原图对分水岭进行观察
        figure
        subplot(3,3,1),
        imshow(f);
        subplot(3,3,2),
        imshow(g);
        subplot(3,3,3),
        imshow(l);
        subplot(3,3,4),
        imshow(im);
        subplot(3,3,5),
        imshow(fim);
        subplot(3,3,6),
        imshow(lim);
        subplot(3,3,7),
        imshow(g2);
        subplot(3,3,8),
        imshow(l2)
        subplot(3,3,9),
        imshow(f2);

matlab中图像分割技术之三分水岭分割法_第4张图片

图像分割是由图像处理转到图像分析的关键。一方面,它是目标图像表达的基础,对特征测量有重要的影响。另一方面,图像分割和分割的目标表达、特征提取和参数测量等将原始图像转化为数学表达形式,使得利用计算机进行图像分析和理解成为可能。

你可能感兴趣的:(MATLAB图像处理,matlab,图像处理,计算机视觉)