我目前在正确检测下面图像中的圆圈时遇到问题(预处理),输出可能是零星的,因为它将半正确地显示圆圈(后处理)。图像是通过网络摄像头以 800*600 分辨率实时拍摄的,然后通过双边过滤器,这有助于消除一些误报(我尝试了 GaussianBlur,但有时它会变得非常慢......)。
之后它变为灰色,然后通过 HoughCircles 函数提供所提供的输出。
我已经查看了轮廓函数,但我还没有找到很好的文档来弄清楚每个变量与什么有关,如果这有意义的话(至少对于 python 函数)。
我将不胜感激任何和所有帮助以使其更准确,因为最终目标是获取已知尺寸的孔并将其转换以查看圆圈之间的距离是否已关闭以进行质量控制测试。(并检查是否没有删除一个圆圈,即不存在)。
代码:
import cv2
import os
import math
import numpy
minRad = 50
maxRad = 75
b1 = 2
b2 = 5
b3 = 5
c1 = 5
c2 = 200
c3 = 50
c4 = 100
bw = 1
vc =cv2.VideoCapture(0)
if vc.isOpened():
vc.set(3,800)
vc.set(4,600)
# vc.set(10,10)
rval, frame = vc.read()
else:
rval = False
while rval:
rval, frame = vc.read()
blur = cv2.bilateralFilter(frame,b1,b2,b3)
# blur = cv2.GaussianBlur(frame,(5,5),1)
gray = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY)#frame
# edges = cv2.Canny(gray, 200, 20, apertureSize=3)#80 120 3
edges = gray
circles = cv2.HoughCircles(edges,cv2.cv.CV_HOUGH_GRADIENT,c1,c2,param1=c3,param2=c4,minRadius=minRad,maxRadius=maxRad)
print "\n\n"
print circles
if circles != None:
circles = numpy.uint16(numpy.around(circles),decimals=1)
for cir in circles[0,:]:
if bw == 1:
cv2.circle(edges,(cir[0],cir[1]),cir[2],(0,255,0),2)#frame
cv2.circle(edges,(cir[0],cir[1]),2,(0,0,255),)#frame
else:
#draw outer circle
cv2.circle(blur,(cir[0],cir[1]),cir[2],(0,255,0),2)#frame
#draw center
cv2.circle(blur,(cir[0],cir[1]),2,(0,0,255),)#frame
if bw == 1:
cv2.imwrite('/home/kasper/test/test.jpg', edges, [int(cv2.IMWRITE_JPEG_QUALITY), 90])
else:
cv2.imwrite('/home/kasper/test/test.jpg', blur, [int(cv2.IMWRITE_JPEG_QUALITY), 90])
ch = cv2.waitKey(10)
if ch != -1:
print "keypressed"
print ch
break
cv2.destroyAllWindows()
圆检测输出:
[[[ 652.5 507.5 62.45398331]
[ 282.5 522.5 57.36288071]
[ 102.5 342.5 52.84410858]
[ 462.5 327.5 67.7089386 ]
[ 697.5 242.5 52.52142334]
[ 82.5 547.5 52.50238037]
[ 307.5 167.5 63.04363632]
[ 92.5 137.5 67.79749298]]]
[[[ 287.5 522.5 52.616539 ]
[ 647.5 507.5 57.50217438]
[ 472.5 337.5 67.7089386 ]
[ 87.5 512.5 67.78273773]
[ 82.5 292.5 67.64983368]
[ 687.5 212.5 52.5594902 ]
[ 302.5 162.5 67.88593292]]]