1

我有一个这样的png图像:

在此处输入图像描述

蓝色代表透明。并且所有的圆圈都是一个像素组。所以,我想找到最大的一个,并删除所有不与最大的像素组的小像素。在这个例子中,最大的一个是红色圆圈,我会保留它。但是绿色和黄色太小了,所以我将它们删除。在那之后,我会有这样的事情:

在此处输入图像描述 有任何想法吗?谢谢。

4

2 回答 2

1

如果只考虑对象的大小,请使用以下算法:标记对象掩码图像的连接组件(所有对象像素为白色,透明像素为黑色)。然后计算连接组件的面积,并过滤它们。在这一步,您有一个标签映射和一个授权标签列表。如果具有授权标签,您可以读取标签图并通过将每个像素设置为白色来覆盖掩码图像。

OpenCV 似乎没有标记功能,但 cvFloodFill 可以通过多次调用来做同样的事情:对于每个未标记的白色像素,使用该像素作为标记调用 FloodFill。然后,您可以通过为每个新分配的像素分配其标签,将这一步的结果存储在一个数组(图像大小)中。只要您有未标记的像素,就重复此操作。

否则,您可以为二进制图像重新编码 connex 组件函数,该算法众所周知且易于实现(可能从 Matlab 开始bwlabel)。

于 2013-08-08T15:20:27.730 回答
0

The handiest way to filter objects if you have an a priori knowledge of their size is to use morphological operators. In your case, with opencv, once you've loaded your image (OpenCV supports PNG), you have to do an "openning", that is an erosion followed by a dilation.
The small objects (smaller than the size of the structuring element you chose) will disappear with erosion, while the bigger will remain and be restored with the dilation. (reference here, cv::morphologyEx).
The shape of the big object might be altered. If you're only doing detection, it is harmless, but if you want your object to avoid transformation you'll need to apply a "top hat" transform.

于 2013-08-08T12:58:38.917 回答