当前位置:文档之家› 标记分水岭分割算法

标记分水岭分割算法

标记分水岭分割算法Separating touching objects in an image is one of the more difficult image processing operations. The watershed transform is often applied to this problem. The watershed transform finds "catchment basins"(集水盆) and "watershed ridge lines"(山脊线) in an image by treating it as a surface where light pixels are high and dark pixels are low.如果图像中的目标物体是连接在一起的,则分割起来会更困难,分水岭分割算法经常用于处理这类问题,通常会取得比较好的效果。

分水岭分割算法把图像看成一幅“地形图”,其中亮度比较强的区域像素值较大,而比较暗的区域像素值较小,通过寻找“汇水盆地”和“分水岭界限”,对图像进行分割。

Segmentation using the watershed transform works better if you can identify, or "mark," foreground objects and background locations. Marker‐controlled watershed segmentation follows this basic procedure:直接应用分水岭分割算法的效果往往并不好,如果在图像中对前景对象和背景对象进行标注区别,再应用分水岭算法会取得较好的分割效果。

基于标记控制的分水岭分割方法有以下基本步骤:1. Compute a segmentation function. This is an image whose dark regions are the objects you are trying to segment.1.计算分割函数。

图像中较暗的区域是要分割的对象。

2. Compute foreground markers. These are connected blobs of pixels within each of the objects.2.计算前景标志。

这些是每个对象内部连接的斑点像素。

3. Compute background markers. These are pixels that are not part of any object.3.计算背景标志。

这些是不属于任何对象的像素。

4. Modify the segmentation function so that it only has minima at the foreground and background marker locations.4.修改分割函数,使其仅在前景和后景标记位置有极小值。

5. Compute the watershed transform of the modified segmentation function.5.对修改后的分割函数做分水岭变换计算。

Use by Matlab Image Processing Toolbox使用MATLAB图像处理工具箱注:期间用到了很多图像处理工具箱的函数,例如fspecial、imfilter、watershed、label2rgb、imopen、imclose、imreconstruct、imcomplement、imregionalmax、bwareaopen、graythresh和imimposemin函数等。

Step 1: Read in the Color Image and Convert it to Grayscale第一步:读入彩色图像,将其转化成灰度图像clc; clear all; close all;rgb = imread('pears.png');if ndims(rgb) == 3I = rgb2gray(rgb);elseI = rgb;endfigure('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('图像开操作')Next compute the opening‐by‐reconstruction using imerode and imreconstruct.接下来,通过腐蚀后重建来做基于开的重建计算。

相关主题