【发布时间】:2018-04-13 19:47:20
【问题描述】:
我在 Python 中使用 OpenCV,以便能够仅识别图像上显示的叶子。我已经可以分割我的图像了,现在我陷入了“如何在检测到所有图像后裁剪最大的组件。下面是代码,请看一下。
-
使用scipy.ndimage,找到组件后无法前进:
def undesired_objects ( image ): components, n = ndimage.label( image ) components = skimage.morphology.remove_small_objects( components, min_size = 50 ) components, n = ndimage.label( components ) plot.imshow( components ) plot.show() -
使用 OpenCV connectedComponentsWithStats:
def undesired_objects ( image ): image = image.astype( 'uint8' ) nb_components, output, stats, centroids = cv2.connectedComponentsWithStats(image, connectivity=4) sizes = stats[1:, -1]; nb_components = nb_components - 1 min_size = 150 img2 = np.zeros(( output.shape )) for i in range(0, nb_components): if sizes[i] >= min_size: img2[output == i + 1] = 255 plot.imshow( img2 ) plot.show()
但是,在这两种方法中,我仍然得到不止一个组件。下面,您将找到二进制图像:
【问题讨论】:
-
你能上传你试图找到最大连通分量的二进制图像吗?
-
@ZdaR 已更新二进制图像!
-
你的结果有什么问题?您的代码不能保证只输出一个组件。它将选择所有大于您的
min_size参数的组件。而且由于您没有清除不同组件之间的img2,因此它们最终都会被绘制在同一个图像上。 -
是的,@Sunreef。我想删除 min_size 参数以仅选择较大的参数;但是,我坚持下去了
标签: python opencv image-segmentation