【问题标题】:OpenCV Crop Hough Circles Python not workingOpenCV Crop Hough Circles Python不工作
【发布时间】:2018-06-26 06:26:57
【问题描述】:

你好呀, 我正在尝试通过将 OpenCV 与 Python 结合使用来构建模拟量规阅读器。我使用霍夫圆来减少编码。代码转载如下:

import cv2
import numpy as np

img = cv2.imread('gauge.jpg', 0)
img = cv2.medianBlur(img,5)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)

height,width = img.shape
mask = np.zeros((height,width), np.uint8)

counter = 0

circles = cv2.HoughCircles(img,cv2.cv.CV_HOUGH_GRADIENT,1,20,
                            param1=200,param2=100,minRadius=0,maxRadius=0)

circles = np.uint16(np.around(circles))
for i in circles[0,:]:
    # draw the outer circle
    cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)

    # draw the center of the circle
    cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)

    # Draw on mask
    cv2.circle(mask,(i[0],i[1]),i[2],(255,255,255),-1)
    masked_data = cv2.bitwise_and(cimg, cimg, mask=mask)    

    # Apply Threshold
    _,thresh = cv2.threshold(mask,1,255,cv2.THRESH_BINARY)

    # Find Contour
    cnt = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[0]

    #print len(contours)
    x,y,w,h = cv2.boundingRect(cnt[0])

    # Crop masked_data
    crop = masked_data[y:y+h,x:x+w]

    # Write Files
    cv2.imwrite("output/crop"+str(counter)+".jpg", crop)

    counter +=1

print counter

cv2.imshow('detected circles',cimg)
cv2.imwrite("output/circled_img.jpg", cimg)
cv2.waitKey(0)
cv2.destroyAllWindows()

我的问题如下:

  1. 我没有得到单独的 Dials,但在每个图像中,“crop0.jpg”有 1 个,但“crop1.jpg”有 2 个,“crop3.jpg”有 4 个。我需要的是单个文件中的单个圈子,其中然后我可以运行批量模板匹配算法。

  2. 计数器的总结果为 5,请注意。

【问题讨论】:

  • 您可能希望先平滑图像,然后再使用 HoughCircles 找到圆圈。此外,由于圆的大小或多或少相同,您还可以尝试为 minRadiusmaxRadius 设置不同的值

标签: python opencv computer-vision opencv-contour


【解决方案1】:

在我看来,你在这里加班。在这一行

circles = cv2.HoughCircles(img,cv2.cv.CV_HOUGH_GRADIENT,1,20, param1=200,param2=100,minRadius=0,maxRadius=0)

您实际上找到了您正在寻找的圈子,但出于某种原因,您尝试在随后的循环中再次找到它们。

您可以使用获得的坐标简单地获取裁剪后的图像。由于每个圆都有一个中心和一个半径,您可以获得一个包含您的圆的边界框,然后(可能)对其应用蒙版。
我猜这样的事情会起作用:

for c in circles[0, :]:
    c = c.astype(int)
    # get the actual cropped images here
    crop = img_copy[c[1]-c[2]:c[1]+c[2], c[0]-c[2]:c[0]+c[2]]
    # create a mask and add each circle in it
    mask = np.zeros(crop.shape)
    mask = cv2.circle(mask, (c[2], c[2]), c[2], (255, 255, 255), -1)
    final_im = mask * crop

只需在之前添加此内容即可在过滤之前获得图像的副本:

img = cv2.imread('/home/gorfanidis/misc/gauge2.jpg', 0)
img_copy = img.copy()  # <- add this to have a copy of your original image

编辑:

如果由于某种原因您没有得到结果(返回类型是 None)或得到零结果(中心和半径是 0),那么您可以检查任何一种情况:

if circles is not None:  # checks that something actually was returned
    for c in circles[0, :]:
        c = c.astype(int)
        if not c[2]:  # just checks that radius is not zero to proceed
            continue
        ...

【讨论】:

  • 感谢您的回答,但我收到“final_im = mask * crop”
  • 这意味着我猜你可能没有得到任何圈子。之前检查一下
  • [[174 173 171 ..., 164 170 175] [172 172 171 ..., 164 167 174] [170 170 172 ..., 169 164 169] ..., [ 159 159 158 ..., 156 157 158] [157 157 157 ..., 156 156 157] [157 157 156 ..., 151 151 152]] [[172 168 167 ..., 168 170 166] [ 175 167 167 ..., 166 168 165] [174 168 167 ..., 159 155 164] ...,
  • 打印裁剪结果5套如上所述
  • c = i.astype(int) # 在这里获取实际裁剪的图像crop = img_copy[c[1]-c[2]:c[1]+c[2], c[0 ]-c[2]:c[0]+c[2]] print crop # 创建一个蒙版并在其中添加每个圆圈 mask = np.zeros(crop.shape) mask = cv2.circle(mask, (c[ 2], c[2]), c[2], (255, 255, 255), -1) final_im = 掩码 * 裁剪
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-07
  • 1970-01-01
  • 1970-01-01
  • 2019-03-19
  • 2021-04-14
  • 1970-01-01
  • 2018-07-18
相关资源
最近更新 更多