繁体   English   中英

OpenCV圆/轮廓检测Python

[英]OpenCV Circle/Contour Detection Python

我目前在正确检测波纹管中的圆(预处理)时遇到问题,在可能半正确显示圆(后处理)的时间内,输出可能是零星的。 图像通过800 * 600分辨率的网络摄像头实时拍摄,然后通过bilateralFilter传递,该图像有助于消除一些假阴性(我尝试过使用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]]]

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

您可以使用以下代码检测孔:

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)

图像不清楚。 如果照明正确,它将起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM