Main Content

imfill

填充图像区域和孔

说明

示例

BW2 = imfill(BW,locations)locations 中指定的点开始,对输入二值图像 BW 的背景像素执行泛洪填充运算。

BW2 = imfill(BW,locations,conn) 填充由 locations 定义的区域,其中 conn 指定连通性。

示例

BW2 = imfill(BW,"holes") 填充输入二值图像 BW 中的孔。在此语法中,孔是无法通过从图像边缘填充背景来到达的一组背景像素。

示例

BW2 = imfill(BW,conn,"holes") 填充二值图像 BW 中的孔,其中 conn 指定连通性。

示例

I2 = imfill(I) 填充灰度图像 I 中的孔。在此语法中,孔定义为由较亮像素包围的一个暗像素区域。

示例

I2 = imfill(I,conn) 填充灰度图像 I 中的孔,其中 conn 指定连通性。

BW2 = imfill(BW) 在屏幕上显示二值图像 BW,并允许您通过用鼠标以交互方式选择点来定义要填充的区域。要使用此语法,BW 必须为二维图像。

BackspaceDelete 键删除之前选择的点。按住 Shift 键点击、右键点击或双击以选择最后一个点并开始填充运算。按 Return 键完成选择,无需添加点。

BW2 = imfill(BW,0,conn) 允许您在以交互方式指定位置时覆盖默认连通性。

[BW2, locations_out] = imfill(BW) 返回在 locations_out 中以交互方式选择的点的位置。要使用此语法,BW 必须为二维图像。

示例

全部折叠

BW1 = logical([1 0 0 0 0 0 0 0
               1 1 1 1 1 0 0 0
               1 0 0 0 1 0 1 0
               1 0 0 0 1 1 1 0
               1 1 1 1 0 1 1 1
               1 0 0 1 1 0 1 0
               1 0 0 0 1 0 1 0
               1 0 0 0 1 1 1 0]);

BW2 = imfill(BW1,[3 3],8)
BW2 = 8x8 logical array

   1   0   0   0   0   0   0   0
   1   1   1   1   1   0   0   0
   1   1   1   1   1   0   1   0
   1   1   1   1   1   1   1   0
   1   1   1   1   1   1   1   1
   1   0   0   1   1   1   1   0
   1   0   0   0   1   1   1   0
   1   0   0   0   1   1   1   0

将图像读入工作区。

I = imread('coins.png');
figure
imshow(I)
title('Original Image')

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

将图像转换为二值图像。

BW = imbinarize(I);
figure
imshow(BW)
title('Original Image Converted to Binary Image')

Figure contains an axes object. The axes object with title Original Image Converted to Binary Image contains an object of type image.

填充二值图像中的孔并显示结果。

BW2 = imfill(BW,'holes');
figure
imshow(BW2)
title('Filled Image')

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

I = imread('tire.tif');
I2 = imfill(I);
figure, imshow(I), figure, imshow(I2)

Figure contains an axes object. The axes object contains an object of type image.

Figure contains an axes object. The axes object contains an object of type image.

输入参数

全部折叠

二值图像,指定为任意维度的逻辑数组。

示例: BW = imread('text.png');

数据类型: logical

标识像素位置的线性索引,指定为正整数组成的数值向量或二维数值矩阵。如果 locations 是 p×1 向量,则它包含起始位置的线性索引。如果 locations 是 p×ndims(BW) 矩阵,则每行包含一个起始位置的数组索引。

示例: [3 3]

数据类型: double

灰度图像,指定为任意维度的数值数组。

示例: I = imread('cameraman.tif');

数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

像素连通性,指定为下表中的值之一。对于二维图像,默认连通性是 4,对于三维图像,默认连通性是 6

意义

二维连通

4

如果像素的边缘相互接触,则这些像素具有连通性。像素的邻域是水平或垂直方向上的相邻像素。

3-by-3 pixel neighborhood with four pixels connected to the center pixel

当前像素以灰色显示。

8

如果像素的边缘或角相互接触,则这些像素具有连通性。像素的邻域是水平、垂直或对角线方向上的相邻像素。

3-by-3 pixel neighborhood with 8 pixels connected to the center pixel

当前像素以灰色显示。

三维连通

6

如果像素的面接触,则这些像素具有连通性。像素的邻域是符合以下条件的相邻像素:

  • 在所列方向之一上连通:内、外、左、右、上、下

3-by-3-by-3 pixel neighborhood with 6 pixels connected to the faces of the center pixel

当前像素以灰色显示。

18

如果像素的面或边缘接触,则这些像素具有连通性。像素的邻域是符合以下条件的相邻像素:

  • 在所列方向之一上连通:内、外、左、右、上、下

  • 在两个方向的组合上连通,如右下或内上

3-by-3-by-3 pixel neighborhood with 6 pixels connected to the faces and 12 pixels connected to the edges of the center pixel

当前像素是立方体的中心。

26

如果像素的面、边缘或角接触,则这些像素具有连通性。像素的邻域是符合以下条件的相邻像素:

  • 在所列方向之一上连通:内、外、左、右、上、下

  • 在两个方向的组合上连通,如右下或内上

  • 在三个方向的组合上连通,如内右上或内左下

3-by-3-by-3 pixel neighborhood with 6 pixels connected to the faces, 12 pixels connected to the edges, and 8 pixels connected to the corners of the center pixel

当前像素是立方体的中心。

对于更高的维度,imfill 使用默认值 conndef(ndims(BW),'minimal')

也可以通过指定由 01 组成的 3×3×...×3 矩阵,以更通用的方式来定义任意维度的连通性。值为 1 的元素定义相对于 conn 的中心元素的邻域位置。请注意,conn 必须关于其中心元素对称。有关详细信息,请参阅指定自定义连通性

数据类型: double | logical

输出参数

全部折叠

填充的二值图像,以逻辑数组形式返回。

像素位置的线性索引,以数值向量或矩阵形式返回。

填充的灰度图像,以数值数组形式返回。

算法

imfill 使用基于形态学重新构造的算法 [1]

参考

[1] Soille, P., Morphological Image Analysis: Principles and Applications, Springer-Verlag, 1999, pp. 173–174.

扩展功能

版本历史记录

在 R2006a 之前推出

全部展开