OpenCV 圆形/轮廓检测 Python
我现在在处理下面这张图片时,遇到了正确检测圆形的问题(预处理)。有时候输出的结果会不太稳定,虽然偶尔能半正确地显示出圆形(后处理)。这张图片是通过网络摄像头实时拍摄的,分辨率是800*600,然后经过一个叫做双边滤波器的处理,这个处理可以帮助去掉一些错误的负检测(我试过高斯模糊,但有时候速度会非常慢……)。
接下来,图片会被转换成灰度图,然后通过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]]]
2 个回答
0
你可以用这段代码来检测空洞:
import numpy as np
import cv2
from matplotlib import pyplot as plt
plt.ion()
filteredContour = []
img = cv2.imread('circle.png')
grayImage = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
binaryImage = np.uint8((grayImage < 100) *1)
binaryForContour = binaryImage*1
contour,hierarchy=cv2.findContours(binaryForContour,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for iteration in range (0,len(contour)):
areaOfContour = cv2.contourArea(contour[iteration])
if areaOfContour >= 5000:
filteredContour.append(contour[iteration])
cv2.drawContours(img,filteredContour, -1, (0,255,0), 2)
plt.imshow(img)
不过图片不太清晰。如果光线合适的话,这个方法就能正常工作。
0
在我看来,输出的格式是 (x,y,radius),其中 (x,y) 是每个圆的中心点。