Main Content

使用边缘检测和形态学检测细胞

此示例说明如何使用边缘检测和基本形态学检测细胞。如果某对象与背景有足够的对比度,则可以在图像中轻松检测到该对象。

步骤 1:读取图像

cell.tif 图像中读取,这是一个前列腺癌细胞的图像。此图像中存在两个细胞,但只有一个细胞完整显示。目标是检测或分割完整显示的细胞。

I = imread('cell.tif');
imshow(I)
title('Original Image');
text(size(I,2),size(I,1)+15, ...
    'Image courtesy of Alan Partin', ...
    'FontSize',7,'HorizontalAlignment','right');
text(size(I,2),size(I,1)+25, ....
    'Johns Hopkins University', ...
    'FontSize',7,'HorizontalAlignment','right');

Figure contains an axes object. The axes object with title Original Image contains 3 objects of type image, text.

步骤 2:检测整个细胞

要分割的对象与背景图像的对比度相差很大。计算图像梯度的算子可以检测到对比度的变化。要创建包含分割后的细胞的二值掩膜,请计算梯度图像并应用一个阈值。

使用 edge 和 Sobel 算子计算阈值。调整阈值,再次使用 edge 获得包含分割后的细胞的二值掩膜。

[~,threshold] = edge(I,'sobel');
fudgeFactor = 0.5;
BWs = edge(I,'sobel',threshold * fudgeFactor);

显示生成的二元梯度掩膜。

imshow(BWs)
title('Binary Gradient Mask')

Figure contains an axes object. The axes object with title Binary Gradient Mask contains an object of type image.

步骤 3:膨胀图像

二元梯度掩膜显示图像中高对比度的线条。这些线条没有很好地描绘出感兴趣的对象的轮廓。与原始图像相比,梯度掩膜中对象周围的线条有间隙。如果使用线性结构元素膨胀 Sobel 图像,这些线性间隙将消失。使用 strel 函数创建两个垂直线性结构元素。

se90 = strel('line',3,90);
se0 = strel('line',3,0);

先后使用垂直结构元素和水平结构元素,来膨胀二元梯度掩膜。使用 imdilate 函数膨胀图像。

BWsdil = imdilate(BWs,[se90 se0]);
imshow(BWsdil)
title('Dilated Gradient Mask')

Figure contains an axes object. The axes object with title Dilated Gradient Mask contains an object of type image.

步骤 4:填补内部间隙

膨胀的梯度掩膜很好地显示了细胞的轮廓,但细胞内部仍有小孔。要填充这些孔洞,请使用 imfill 函数。

BWdfill = imfill(BWsdil,'holes');
imshow(BWdfill)
title('Binary Image with Filled Holes')

Figure contains an axes object. The axes object with title Binary Image with Filled Holes contains an object of type image.

步骤 5:删除边界上的连通对象

感兴趣的细胞已成功分割,但它不是被发现的唯一对象。可以使用 imclearborder 函数删除任何与图像边界连通的对象。要删除对角线连通,请将 imclearborder 函数中的连通性设置为 4

BWnobord = imclearborder(BWdfill,4);
imshow(BWnobord)
title('Cleared Border Image')

Figure contains an axes object. The axes object with title Cleared Border Image contains an object of type image.

步骤 6:平滑处理对象

最后,为了使分割后的对象看起来自然,用菱形结构元素对图像腐蚀两次来平滑处理对象。使用 strel 函数创建菱形结构元素。

seD = strel('diamond',1);
BWfinal = imerode(BWnobord,seD);
BWfinal = imerode(BWfinal,seD);
imshow(BWfinal)
title('Segmented Image');

Figure contains an axes object. The axes object with title Segmented Image contains an object of type image.

步骤 7:可视化分割

您可以使用 labeloverlay 函数在原始图像上显示掩膜。

imshow(labeloverlay(I,BWfinal))
title('Mask Over Original Image')

Figure contains an axes object. The axes object with title Mask Over Original Image contains an object of type image.

显示分割后的对象的另一种方法是在分割的细胞周围绘制轮廓。使用 bwperim 函数绘制轮廓。

BWoutline = bwperim(BWfinal);
Segout = I; 
Segout(BWoutline) = 255; 
imshow(Segout)
title('Outlined Original Image')

Figure contains an axes object. The axes object with title Outlined Original Image contains an object of type image.

另请参阅

| | | | | |

相关主题