如何使用python和Opencv计算图像中的点数? [英] How to count number of dots in an image using python and Opencv?

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

问题描述

我想计算图像中的点数.图像看起来像

我参考了这个 SOF 链接

I want to count number of dots in an image. The image looks like

I referred to this SOF link count colored dots in image But this is for colored links , So anyone here can guide me over how to handle this and count black dots out of white back.

解决方案

  1. thredhold using THRESH_BINARY_INV flag, change to black-background-white-foreground.
  2. findContours, filter the contours by area(calculated by contourArea)
  3. You get it


import cv2
gray = cv2.imread("dots.jpg", 0)

## threshold
th, threshed = cv2.threshold(gray, 100, 255,cv2.THRESH_BINARY_INV|cv2.THRESH_OTSU)

## findcontours
cnts = cv2.findContours(threshed, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[-2]


## filter by area
s1= 3
s2 = 20
xcnts = []
for cnt in cnts:
    if s1<cv2.contourArea(cnt) <s2:
        xcnts.append(cnt)

print("Dots number: {}".format(len(xcnts)))
#Dots number: 23


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

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