Matlab数字图像处理基础【11】

来源:互联网 发布:在淘宝上如何做代理 编辑:程序博客网 时间:2024/05/01 01:53

第六章 图像分割

实例:使用形态学分水岭算法分割灰度图像


clc;clear;%使用距离变换的分水岭分割I = imread('shape.jpg');%图像二值化f = im2bw(I, graythresh(I));figure,subplot(2,3,1),imshow(I),title('原图像');subplot(2,3,2),imshow(f),title('二值化图像');%二值化图像求补fc = ~f;subplot(2,3,3),imshow(fc),title('二值化图像求补');%计算距离变换D = bwdist(fc);subplot(2,3,4),imshow(D),title('距离变换');%计算分水岭变换L = watershed(-D);%计算分水岭脊线w = L == 0;subplot(2,3,5),imshow(w),title('距离变换的负分水岭脊线');%计算分割结果g1 = f & ~w;subplot(2,3,6),imshow(g1),title('分水岭脊线');%使用梯度的分水岭分割figure,subplot(2,3,1),imshow(I),title('原图像');%定义梯度算子h = fspecial('sobel');fd = double(I);%计算梯度幅度图像g21 = sqrt(imfilter(fd, h, 'replicate') .^ 2 + imfilter(fd, h', 'replicate') .^ 2);subplot(2,3,2),imshow(g21),title('梯度幅度图像');%计算分水岭变换L21 = watershed(g21);%计算分水岭脊线wr1 = L21 == 0;%获取分割结果-首先获取原图像f21 = I;%获取分割结果-分割线像素置为255f21(wr1) = 255;subplot(2,3,3),imshow(wr1),title('分水岭脊线');subplot(2,3,4),imshow(f21),title('分割结果');%使用闭-开运算平滑梯度图像g22 = imclose(imopen(g21, ones(3, 3)), ones(3, 3));%计算分水岭变换L22 = watershed(g22);%计算分水岭脊线wr2 = L22 == 0;%获取分割结果-首先获取原图像f22 = I;%获取分割结果-分割线像素置为255f22(wr2) = 255;subplot(2,3,5),imshow(wr2),title('平滑梯度后的分水岭脊线');subplot(2,3,6),imshow(f22),title('平滑梯度后的分割结果');


实验结果:

图1:

图2:



0 0
原创粉丝点击