我正在使用Python中的OpenCV来识别图像上呈现的叶子。我已经能够分割图像,但现在我卡在了“如何在检测到所有组件后裁剪最大组件”的问题上。以下是代码,请查看。
Using scipy.ndimage, I was unable to advance after find the components:
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()
Using 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()
min_size
参数的组件。而且,由于你没有在不同的组件之间清除你的img2
,它们最终都会被绘制在同一张图像上。 - Sunreef