python 圆形检测,在Python中使用CV2的填充圆检测?

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?

解决方案

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):

02cbe20ec5cb00f82999e4ba03e4761b.png

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值