Filled circle detection using CV2 in Python?

馋奶兔 提交于 2019-12-07 13:10:18

问题


I am trying to detect all the circles in images like this.

I have many different images like this but in all the circles will be black (or almost black) and of the same size (+/- a couple of pixels). I believe there are exactly 2943 circles in each image. These conditions never change. I may not be able to control the sizes of the circles across images (usually ranges between 15-45 pixels in radii - the example image provided above has radii of 20-21 pixels).

I need to be able to detect the exact locations of the centers of these circles as accurately and precisely as possible (if possible, the radii too).

I tried using the cv2.HoughCircles function to do so, but got very inconsistent and unreliable results. Here is the code that I used:

from pylab import *
import sys, cv2

filename = sys.argv[1];
img = cv2.imread(filename,0);
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR);
circles = cv2.HoughCircles(img,cv2.cv.CV_HOUGH_GRADIENT,2,15,param1=100,param2=30,minRadius=15,maxRadius=25);
circles = np.uint16(np.around(circles));
for i in circles[0,:]:
    cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),1);
    cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3);
cv2.imwrite('1-%s'%(filename),cimg)
print "%d circles found."%(len(circles[0]));

The result was this image and this output: 2806 circles found.

There are many false circles and many of the true circles have been missed/ignored.

I am starting to believe that the HoughCircle method is not the most optimal way to go if all my circles are identical in a single image, and there might be some better object detection method available.

What do you suggest I use to detect every circle precisely and accurately across thousands of images if I can control the properties of the circles tightly enough?


回答1:


I came up with this code, it is tuned to the exact image you have supplied, finds 2943 circles with the radius estimate as well, assuming all circles have the same radius. This is what it produces (cropped, original was too big):

You can see that its not completely ideal (the corner circle is a bit off).

It is based around thresholding and then contour operations rather than hough circles.

import cv2
import numpy as np

original = cv2.imread("test.jpg", cv2.CV_LOAD_IMAGE_GRAYSCALE)
retval, image = cv2.threshold(original, 50, 255, cv2.cv.CV_THRESH_BINARY)

el = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
image = cv2.dilate(image, el, iterations=6)

cv2.imwrite("dilated.png", image)

contours, hierarchy = cv2.findContours(
    image,
    cv2.cv.CV_RETR_LIST,
    cv2.cv.CV_CHAIN_APPROX_SIMPLE
)

drawing = cv2.imread("test.jpg")

centers = []
radii = []
for contour in contours:
    area = cv2.contourArea(contour)

    # there is one contour that contains all others, filter it out
    if area > 500:
        continue

    br = cv2.boundingRect(contour)
    radii.append(br[2])

    m = cv2.moments(contour)
    center = (int(m['m10'] / m['m00']), int(m['m01'] / m['m00']))
    centers.append(center)

print("There are {} circles".format(len(centers)))

radius = int(np.average(radii)) + 5

for center in centers:
    cv2.circle(drawing, center, 3, (255, 0, 0), -1)
    cv2.circle(drawing, center, radius, (0, 255, 0), 1)

cv2.imwrite("drawing.png", drawing)

Hope it helps



来源:https://stackoverflow.com/questions/21612258/filled-circle-detection-using-cv2-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!