英文:
How to detect leaves from an image using cv2.Canny edges?
问题
我想要做的是从图像中提取出所有的叶子。然而,我卡在了如何获取这些形状上。我尝试过使用轮廓,并找到最大面积的轮廓,但事实证明这些轮廓都不相连,所以这是不可能的。我是否能够检测出这张图片上哪些部分有最大面积的黑色斑点,从而显示出叶子(我可以在原始图像上使用颜色过滤器来去除手和标签)?
英文:
I have an image, which you can view here:
What I want to do is extract all the leaves from the image. However, I am stuck on how to get these shapes. I've tried using contours, and finding the contours with the largest areas, but it turns out none of these contours are connected, making that impossible. Would I be able to detect which parts of this image have the biggest splotches of black, essentially showing me the leaves (I could use a color filter on the original image to remove the hand and label)?
答案1
得分: 1
如果白线中的缝隙是您能够使用“contours”的唯一障碍,您可以执行以下操作:您有二进制图像(仅包含黑白像素),将所有白色像素替换为白色像素块(例如,缺少角落的5x5正方形)。对整个图像重复此操作多次。在生成的图像中,对黑色像素执行相同大小的块替换相同次数的操作。您最终将得到一个图像,其中包含所有原始白色像素以及原本位于白色像素之间小缝隙中的额外白色像素。该方法仅适用于较小的缝隙,但随着块替换数量的增加,会略微失真。
简而言之:多次膨胀白色像素,然后以相同次数膨胀黑色像素。可能有适用于二进制图像的“膨胀”滤镜。
英文:
If the gaps in the white lines are your only obstacle to being able to use contours
you can do the following: You have your binary image (only black and white pixels) Replace all white pixels with patches of white pixels (e.g. 5x5 squares with missing corners). Repeat this several times for the whole image. In the resulting image do the same thing to the black pixels with the same patch-size for the same number of times. You end up with an image with all the original white pixels plus additional white pixels in originally small gaps between white pixels. The method only works well for small gaps but distorts somewhat with growing number of patch-replacements.
So in short: inflate white pixels several times, then inflate black pixels same number of times. There may by filters for binary images witch do the 'inflation'.
答案2
得分: 1
我之前做过类似的事情,通过分离绿色并使用一个斑点计数算法来计算叶子的数量。
图片需要更简单一些,只有叶子是绿色的,至少要有可以区分的绿色,并且以相对高的分辨率拍摄,以便在叶子聚集在一起时也能捕捉到它们。
只是我的一点建议,但这种方法有效。当你找到解决方案时,请分享出来,因为我会非常感兴趣。
英文:
I've done something similar to this by isolating the green colour and then used a blob counting algorithm to count the number of leaves.
The picture needs to be less complex, only having the leaves green, at least a distinguishable green, and captured in reasonably high resolution to capture the leaves when they're in a bunch.
Just my two cents but it worked. When you come up with a solution, please share it because I'd be super interested.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论