简体   繁体   中英

How to count number of dots in an image using python and Opencv?

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

在此处输入图片说明

您指向的答案是将彩色图像转换为灰度图像,因此您应该对图像应用HoughCircles()并找出结果数组的长度。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM