【问题标题】:OpenCV Circle/Contour Detection PythonOpenCV圆/轮廓检测Python
【发布时间】:2014-07-27 21:08:01
【问题描述】:

我目前在正确检测下图中的圆圈时遇到问题(预处理),输出可能是零星的,因为它会半正确地显示圆圈(后处理)。图像是通过网络摄像头以 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]]]

【问题讨论】:

  • 我设法让它的一个版本工作并准确跟踪到下一步,即将像素数据转换为与圆的已知值的距离......我将发布工作代码接下来的 24-48 小时。
  • 你应该尝试模板匹配。
  • @ivan_a 如果相机在模板匹配时稍微移动了怎么办?
  • 视情况而定,您必须做出一些假设并限制我们的系统遵守这些假设。
  • 轻微的平移移动(非旋转)应该不会对系统的性能产生很大影响。

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


【解决方案1】:

在我看来,输出的格式是 (x,y,radius),其中 (x,y) 是每个圆的中心。

【讨论】:

  • 输出不正确,它与预处理的圆圈不对齐。是的,输出格式是 x,y,radius。我希望能够准确地跟踪圆圈,这样我就可以在它们前进时检测到偏移……即 1 套与其他套不相符或差距太大。
【解决方案2】:

您可以使用以下代码检测漏洞:

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)

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    • 1970-01-01
    • 2016-10-24
    • 2014-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多