4

基于分水岭算法的图像分割-Matlab版本

 2 years ago
source link: https://blog.51cto.com/domi/5939249
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

基于分水岭算法的图像分割-Matlab版本

精选 原创

domi+1 2022-12-15 12:30:58 博主文章分类:matlab ©著作权

文章标签 Io 分水岭算法 彩色图像 文章分类 其它 编程语言 yyds干货盘点 阅读数179

✅作者简介:热爱科研的算法开发者,Python、Matlab项目可交流、沟通、学习。

🍎个人主页:算法工程师的学习日志

简介

分水岭算法是一种图像区域分割法,分割的过程中将图片转化为灰度图,然后将灰度值看作是海拔,然后向较低点注水,这种基于地形学的解释,我们着重考虑三种点:

基于分水岭算法的图像分割-Matlab版本_彩色图像

1)极小值点,该点对应一个盆地的最低点,当我们在盆地里滴一滴水的时候,由于重力作用,水最终会汇聚到该点。注意:可能存在一个最小值面,该平面内的都是极小值点。

2)盆地的其它位置点,该位置滴的水滴会汇聚到局部最小点。

3)盆地的边缘点,是该盆地和其它盆地交接点,在该点滴一滴水,会等概率的流向任何一个盆地。

基于分水岭算法的图像分割-Matlab版本_分水岭算法_02

明白上述三种点之后,我们开始往盆地的极小值点注水,然后随着注水的深入,每一个极小值点慢慢的向外扩展,然后知道两个盆地的水汇合,汇合处就是我们需要的分水岭。

从下图可以直观理解一下,首先这三块区域都含有极小值点

基于分水岭算法的图像分割-Matlab版本_分水岭算法_03

然后逐渐填充就能获得分水岭(即分界线)

基于分水岭算法的图像分割-Matlab版本_Io_04

得到分界线就能完成图像分割

clear, close all;
clc;
%1.读取图像并求取图像的边界。

rgb = imread('1.png');%读取原图像
I = rgb2gray(rgb);%转化为灰度图像
figure; subplot(121)%显示灰度图像
imshow(I)
text(732,501,'Image courtesy of Corel','FontSize',7,'HorizontalAlignment','right')
hy = fspecial('sobel');%sobel算子,应用sobel算子锐化图像
hx = hy';
Iy = imfilter(double(I), hy, 'replicate');%滤波求y方向边缘
Ix = imfilter(double(I), hx, 'replicate');%滤波求x方向边缘
gradmag = sqrt(Ix.^2 + Iy.^2);%求摸
subplot(122); imshow(gradmag,[]), %显示梯度
title('Gradient magnitude (gradmag)')

%2. 直接使用梯度模值进行分水岭算法:(往往会存在过的分割的情况,效果不好)

L = watershed(gradmag);%直接应用分水岭算法
Lrgb = label2rgb(L);%转化为彩色图像
figure; imshow(Lrgb), %显示分割后的图像
title('Watershed transform of gradient magnitude (Lrgb)')%过分割现象

%3.分别对前景和背景进行标记:本例中使用形态学重建技术对前景对象进行标记,首先使用开操作,开操作之后可以去掉一些很小的目标。
%开和闭这两种运算可以除去比结构元素小的特定图像细节,同时保证不产生全局几何失真。
%开运算可以把比结构元素小的突刺滤掉,切断细长搭接而起到分离作用;
%闭运算可以把比结构元素小的缺口或孔填充上,搭接短的间断而起到连接作用。
se = strel('disk', 20);%圆形结构元素,STREL('disk',R,N),R is the specified radius, When N is greater than 0, the disk-shaped structuring
%element is approximated by a sequence of N
Io = imopen(I, se);%形态学开操作
figure; subplot(121)
imshow(Io), %显示执行开操作后的图像
title('Opening (Io)')
Ie = imerode(I, se);%对图像进行腐蚀,基本参数:待处理的输入图像以及结构元素对象
Iobr = imreconstruct(Ie, I);%形态学重建
subplot(122); imshow(Iobr), %显示重建后的图像
title('Opening-by-reconstruction (Iobr)')
Ioc = imclose(Io, se);%形态学关操作,首先膨胀,然后腐蚀,两个操作使用同样的结构元素
figure; subplot(121)
imshow(Ioc), %显示关操作后的图像
title('Opening-closing (Ioc)')
Iobrd = imdilate(Iobr, se);%对图像进行膨胀,基本参数:待处理的输入图像和结构元素对象。
Iobrcbr = imreconstruct(imcomplement(Iobrd), ...
imcomplement(Iobr));%形态学重建
Iobrcbr = imcomplement(Iobrcbr);%图像求反
subplot(122); imshow(Iobrcbr), %显示重建求反后的图像,figure4
title('Opening-closing by reconstruction (Iobrcbr)')
%As you can see by comparing Iobrcbr with Ioc,
%reconstruction-based opening and closing are more
%effective than standard opening and closing at removing
%small blemishes without affecting the overall
%shapes of the objects. Calculate the regional maxima
%of Iobrcbr to obtain good foreground markers.
fgm = imregionalmax(Iobrcbr);%局部极大值
figure; imshow(fgm), %显示重建后局部极大值图像,figure5
title('Regional maxima of opening-closing by reconstruction (fgm)')
I2 = I; %前景标记图与原图叠加
I2(fgm) = 255;%局部极大值处像素值设为255
figure; imshow(I2), %在原图上显示极大值区域,figure6
title('Regional maxima superimposed on original image (I2)')
se2 = strel(ones(5,5));%结构元素
fgm2 = imclose(fgm, se2);%关操作
fgm3 = imerode(fgm2, se2);%腐蚀
fgm4 = bwareaopen(fgm3, 20);%开操作
I3 = I;
I3(fgm4) = 255;%前景处设置为255
figure; subplot(121)
imshow(I3)%显示修改后的极大值区域,figure7
title('Modified regional maxima')
bw = im2bw(Iobrcbr, graythresh(Iobrcbr));%转化为二值图像
subplot(122); imshow(bw), %显示二值图像,figure7
title('Thresholded opening-closing by reconstruction')

%4. 进行分水岭变换并显示:


DL = watershed(Iobrcbr);%分水岭变换
figure
imshow(DL)
title('分水岭')
labels = imdilate(DL==0,ones(3,3));
I4 = labeloverlay(I,labels);
figure
imshow(I4)
title('分水岭边界线')


%%
% 将标签矩阵显示为彩色图像。标签矩阵
Lrgb = label2rgb(DL,'jet','w','shuffle');
figure
imshow(Lrgb)
title('分水岭Label')
%%
figure
imshow(I)
hold on
himage = imshow(Lrgb);
himage.AlphaData = 0.2; % 使用透明方式将此伪颜色标签矩阵叠加到原始强度图像上
title('彩色Label')
基于分水岭算法的图像分割-Matlab版本_分水岭算法_05

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK