OpenCV Circle / Contour Detection Python

时间:2014-06-06 14:39:02

标签: python opencv computer-vision object-detection webcam-capture

我目前正在检测正确检测图像波纹(预处理)中的圆圈的问题,输出可能是零星的,因为它会正确地显示圆圈(后处理)。图像是通过800 * 600分辨率的网络摄像头进行实时拍摄,然后通过双边过滤器,这有助于摆脱一些假阴性(我尝试过GaussianBlur,但有时会非常慢......)。 p>

之后它会变为灰色,然后通过HoughCircles函数提供所提供的输出。

我已经查看了轮廓函数,但是我没有找到很好的文档来弄清楚每个变量的含义,如果这是有道理的(至少对于python函数)。

我将非常感谢所有帮助以使其更加准确,因为最终目标是获取已知尺寸的孔并转换它以查看圆圈之间的距离是否关闭以进行质量控制测试。 (并检查是否没有移除圆圈,即不存在)。

Pre-processing

Post-Processing Color

Post-Processing B&W

CODE:

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 :(得分:0)

对我而言,输出的格式为(x,y,radius),其中(x,y)是每个圆圈的中心。

答案 1 :(得分: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)

图像不清晰。如果照明适当,它将起作用。