【谷速软件】matlab源码-标记分水岭分割算法

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Step 1: Read in the Color Image and Convert it to Grayscale

第一步:读入彩色图像,将其转化成灰度图像

clc; clear all; close all;

rgb = imread('pears.png');

if ndims(rgb) == 3

I = rgb2gray(rgb);

else

I = rgb;

end

figure('units', 'normalized', 'position', [0 0 1 1]);

subplot(1, 2, 1); imshow(rgb); title('原图');

subplot(1, 2, 2); imshow(I); title('灰度图');

Step 2: Use the Gradient Magnitude as the Segmentation Function

第2步:将梯度幅值作为分割函数

Use the Sobel edge masks, imfilter, and some simple arithmetic to compute the gradient magnitude. The gradient is high at the borders of the objects and low (mostly) inside the objects.

使用Sobel边缘算子对图像进行水平和垂直方向的滤波,然后求取模值,sobel算子滤波后的图像在边界处会显示比较大的值,在没有边界处的值会很小。

hy = fspecial('sobel');

hx = hy';

Iy = imfilter(double(I), hy, 'replicate');

Ix = imfilter(double(I), hx, 'replicate');

gradmag = sqrt(Ix.^2 + Iy.^2);

figure('units', 'normalized', 'position', [0 0 1 1]);

subplot(1, 2, 1); imshow(I,[]), title('灰度图像')

subplot(1, 2, 2); imshow(gradmag,[]), title('梯度幅值图像')

Can you segment the image by using the watershed transform directly on the gradient magnitude?

可否直接对梯度幅值图像使用分水岭算法?

L = watershed(gradmag);

Lrgb = label2rgb(L);

figure('units', 'normalized', 'position', [0 0 1 1]);

subplot(1, 2, 1); imshow(gradmag,[]), title('梯度幅值图像')

subplot(1, 2, 2); imshow(Lrgb); title('梯度幅值做分水岭变换')

No. Without additional preprocessing such as the marker computations below, using the watershed transform directly often results in "oversegmentation."

直接使用梯度模值图像进行分水岭算法得到的结果往往会存在过度分割的现象。因此通常需要分别对前景对象和背景对象进行标记,以获得更好的分割效果。

Step 3: Mark the Foreground Objects

第3步:标记前景对象

A variety of procedures could be applied here to find the foreground markers, which must be connected blobs of pixels inside each of the foreground objects. In this example you'll use morphological techniques called "opening-by-reconstruction" and "closing-by-reconstruction" to

"clean" up the image. These operations will create flat maxima inside each object that can be located using imregionalmax.

有多种方法可以应用在这里来获得前景标记,这些标记必须是前景对象内部的连接斑点像素。这个例子中,将使用形态学技术“基于开的重建”和“基于闭的重建”来清理图像。这些操作将会在每个对象内部创建单位极大值,使得可以使用imregionalmax来定位。

开运算和闭运算:先腐蚀后膨胀称为开;先膨胀后腐蚀称为闭。开和闭这两种运算可以除去比结构元素小的特定图像细节,同时保证不产生全局几何失真。开运算可以把比结构元素小的突刺滤掉,切断细长搭接而起到分离作用;闭运算可以把比结构元素小的缺口或孔填充上,搭接短的间隔而起到连接作用。

Opening is an erosion followed by a dilation, while opening-by-reconstruction is an erosion followed by a morphological reconstruction. Let's compare the two. First, compute the opening using imopen.

开操作是腐蚀后膨胀,基于开的重建(基于重建的开操作)是腐蚀后进行形态学重建。下面比较这两种方式。首先,用imopen做开操作。

se = strel('disk', 20);

Io = imopen(I, se);

figure('units', 'normalized', 'position', [0 0 1 1]);

subplot(1, 2, 1); imshow(I, []); title('灰度图像');

subplot(1, 2, 2); imshow(Io), title('图像开操作')

相关文档
最新文档