如何使用python计算图像中的对象? [英] How to count objects in image using python?

查看:234
本文介绍了如何使用python计算图像中的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计算此图像中的墨滴数量以及这些墨滴所覆盖区域的覆盖率。
我试图将此图像转换为黑白图像,但这些图像的中心颜色看起来与背景太相似了。所以我只得到第二张图片。
有没有办法解决这个问题或任何更好的想法?
非常感谢。

I am trying to count the number of drops in this image and the coverage percentage of the area covered by those drops. I tried to convert this image into black and white, but the center color of those drops seems too similar to the background. So I only got something like the second picture. Is there any way to solve this problem or any better ideas? Thanks a lot.

推荐答案

我使用以下代码使用openCV和python检测图像中的轮廓数。

I used the following code to detect the number of contours in the image using openCV and python.

img = cv2.imread('ba3g0.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray,127,255,1)
contours,h = cv2.findContours(thresh,1,2)
for cnt in contours:
  cv2.drawContours(img,[cnt],0,(0,0,255),1)


为了更好地去除另一个轮廓内的轮廓,您需要遍历整个列表并比较和删除内部轮廓。之后,轮廓的大小将给你计数

For furthur removing the contours inside another contour, you need to iterate over the entire list and compare and remove the internal contours. After that the size of "contours" will give you the count

这篇关于如何使用python计算图像中的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆