主要内容

Marker-Controlled Watershed Segmentation

This example shows how to use watershed segmentation to separate touching objects in an image. 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.

如果您可以识别或“标记”前景对象和背景位置,则使用流域变换进行分割更好。标记控制的流域细分遵循此基本过程:

  1. Compute a segmentation function. This is an image whose dark regions are the objects you are trying to segment.

  2. Compute foreground markers. These are connected blobs of pixels within each of the objects.

  3. Compute background markers. These are pixels that are not part of any object.

  4. Modify the segmentation function so that it only has minima at the foreground and background marker locations.

  5. Compute the watershed transform of the modified segmentation function.

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

rgb = imread('pears.png'); I = rgb2gray(rgb); imshow(I) text(732,501,'Image courtesy of Corel(R)',。。。'FontSize'7'HorizontalAlignment','正确的')

图包含一个轴对象。The axes object contains 2 objects of type image, text.

圣ep 2: Use the Gradient Magnitude as the Segmentation Function

计算梯度幅度。梯度在对象的边界和对象内部的低(大部分)。

gmag = imgradient(I); imshow(gmag,[]) title('Gradient Magnitude')

图包含一个轴对象。The axes object with title Gradient Magnitude contains an object of type image.

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

L = watershed(gmag); Lrgb = label2rgb(L); imshow(Lrgb) title(“梯度幅度的分水岭变换”)

图包含一个轴对象。The axes object with title Watershed Transform of Gradient Magnitude contains an object of type image.

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

步骤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 usingimregionalmax

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 usingimopen

se = strel('disk',20); Io = imopen(I,se); imshow(Io) title('Opening')

图包含一个轴对象。带标题开口的轴对象包含类型图像的对象。

Next compute the opening-by-reconstruction usingimerodeimreconstruct

Ie = imerode(I,se); Iobr = imreconstruct(Ie,I); imshow(Iobr) title('Opening-by-Reconstruction')

图包含一个轴对象。The axes object with title Opening-by-Reconstruction contains an object of type image.

Following the opening with a closing can remove the dark spots and stem marks. Compare a regular morphological closing with a closing-by-reconstruction. First tryimclose:

IOC= imclose(Io,se); imshow(Ioc) title('Opening-Closing')

图包含一个轴对象。The axes object with title Opening-Closing contains an object of type image.

Now useimdilatefollowed byimreconstruct。注意您必须补充图像输入和输出imreconstruct

Iobrd = imdilate(Iobr,se); Iobrcbr = imreconstruct(imcomplement(Iobrd),imcomplement(Iobr)); Iobrcbr = imcomplement(Iobrcbr); imshow(Iobrcbr) title(“通过重建开放闭合”)

图包含一个轴对象。The axes object with title Opening-Closing by Reconstruction contains an object of type image.

您可以通过比较IobrcbrwithIOC, 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 ofIobrcbrto obtain good foreground markers.

fgm = imregionalmax(Iobrcbr); imshow(fgm) title('Regional Maxima of Opening-Closing by Reconstruction')

图包含一个轴对象。The axes object with title Regional Maxima of Opening-Closing by Reconstruction contains an object of type image.

To help interpret the result, superimpose the foreground marker image on the original image.

I2 = labeloverlay(I,fgm); imshow(I2) title(“区域最大值叠加在原始图像上”)

图包含一个轴对象。在原始图像上叠加的带有标题区域最大值的轴对象包含类型图像的对象。

Notice that some of the mostly-occluded and shadowed objects are not marked, which means that these objects will not be segmented properly in the end result. Also, the foreground markers in some objects go right up to the objects' edge. That means you should clean the edges of the marker blobs and then shrink them a bit. You can do this by a closing followed by an erosion.

se2 = strel(ones(5,5)); fgm2 = imclose(fgm,se2); fgm3 = imerode(fgm2,se2);

This procedure tends to leave some stray isolated pixels that must be removed. You can do this usingbwareaopen, which removes all blobs that have fewer than a certain number of pixels.

FGM4 = Bwareaopen(FGM3,20);i3 = labeloverlay(i,fgm4);imshow(i3)标题('Modified Regional Maxima Superimposed on Original Image')

图包含一个轴对象。The axes object with title Modified Regional Maxima Superimposed on Original Image contains an object of type image.

圣ep 4: Compute Background Markers

现在您需要标记背景。在清理图像中,Iobrcbr, the dark pixels belong to the background, so you could start with a thresholding operation.

bw = imbinarize(Iobrcbr); imshow(bw) title('Thresholded Opening-Closing by Reconstruction')

图包含一个轴对象。The axes object with title Thresholded Opening-Closing by Reconstruction contains an object of type image.

The background pixels are in black, but ideally we don't want the background markers to be too close to the edges of the objects we are trying to segment. We'll "thin" the background by computing the "skeleton by influence zones", or SKIZ, of the foreground ofbw。这可以通过计算距离转换的流域变换来完成bw, and then looking for the watershed ridge lines (DL == 0) of the result.

d = bwdist(bw);dl =流域(d);bgm = dl == 0;imshow(BGM)标题('Watershed Ridge Lines')

图包含一个轴对象。The axes object with title Watershed Ridge Lines contains an object of type image.

圣ep 5: Compute the Watershed Transform of the Segmentation Function.

功能imimposemincan be used to modify an image so that it has regional minima only in certain desired locations. Here you can useimimposeminto modify the gradient magnitude image so that its only regional minima occur at foreground and background marker pixels.

gmag2 = imimposemin(gmag,bgm | fgm4);

Finally, compute the watershed-based segmentation.

l =流域(gmag2);

圣ep 6: Visualize the Result

One visualization technique is to superimpose the foreground markers, background markers, and segmented object boundaries on the original image. You can use dilation as needed to make certain aspects, such as the object boundaries, more visible. Object boundaries are located whereL == 0。The binary foreground and background markers are scaled to different integer values so that they are assigned different labels.

labels = imdilate(L==0,ones(3,3)) + 2*bgm + 3*fgm4; I4 = labeloverlay(I,labels); imshow(I4) title('Markers and Object Boundaries Superimposed on Original Image')

图包含一个轴对象。The axes object with title Markers and Object Boundaries Superimposed on Original Image contains an object of type image.

This visualization illustrates how the locations of the foreground and background markers affect the result. In a couple of locations, partially occluded darker objects were merged with their brighter neighbor objects because the occluded objects did not have foreground markers.

另一个有用的可视化技术是将标签矩阵显示为颜色图像。标签矩阵,例如由分水岭bwlabel,可以使用使用Label2RGB

Lrgb = label2rgb(L,'jet','w','shuffle'); imshow(Lrgb) title('Colored Watershed Label Matrix')

图包含一个轴对象。The axes object with title Colored Watershed Label Matrix contains an object of type image.

You can use transparency to superimpose this pseudo-color label matrix on top of the original intensity image.

figure imshow(I) holdhimage = imshow (Lrgb);himage。AlphaData = 0.3; title('Colored Labels Superimposed Transparently on Original Image')

图包含一个轴对象。带有标题彩色标签的轴对象在原始图像上透明地叠加叠加,包含2个类型图像的对象。

See Also

||||||||||||