【问题标题】:Trying to Detect Circle with Houghcircles in OpenCV尝试在 OpenCV 中使用 Houghcircles 检测圆
【发布时间】:2016-07-20 20:40:50
【问题描述】:

我正在尝试使用 Houghcircles 方法检测 OpenCV 中的圆圈,但是当我尝试运行我的代码时出现以下错误:

Traceback (most recent call last):
  File "detect_circles.py", line 19, in <module>
    circles = cv2.cv.HoughCircles(gray, cv2.cv.CV_HOUGH_GRADIENT, 1.2, 100)
AttributeError: 'module' object has no attribute 'cv'

我目前正在学习这个网站上的教程:http://www.pyimagesearch.com/2014/07/21/detecting-circles-images-using-opencv-hough-circles/

我想知道是什么导致了这个错误以及我可以做些什么来修复它。

我正在运行这样的代码:

python detect_circles.py --image images/simple.png

这是我的代码:

# import the necessary packages
import numpy as np
import argparse
import cv2
import copy

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True, help = "Path to the image")
args = vars(ap.parse_args())

# load the image, clone it for output, and then convert it to grayscale
image = cv2.imread(args["image"])
original_img = cv2.imread(args["image"])
clone_img = copy.copy(original_img)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# detect circles in the image
circles = cv2.cv.HoughCircles(gray, cv2.cv.CV_HOUGH_GRADIENT, 1.2, 100)

# ensure at least some circles were found
if circles is not None:
    # convert the (x, y) coordinates and radius of the circles to integers
    circles = np.round(circles[0, :]).astype("int")

    # loop over the (x, y) coordinates and radius of the circles
    for (x, y, r) in circles:
        # draw the circle in the output image, then draw a rectangle
        # corresponding to the center of the circle
        cv2.circle(output, (x, y), r, (0, 255, 0), 4)
        cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)

    # show the output image
    cv2.imshow("output", np.hstack([image, output]))
    cv2.waitKey(0)

【问题讨论】:

    标签: python opencv


    【解决方案1】:
    cv2.cv.HoughCircles(gray, cv2.cv.CV_HOUGH_GRADIENT, 1.2, 100)
    

    错误消息说明了一切。您的代码中有错字,应该在没有第二个 cv 模块说明符的情况下调用该方法。

    cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1.2, 100)
    

    【讨论】:

    • 我尝试这样做并得到错误: Traceback (last recent call last): File "detect_circles.py", line 19, in circles = cv2.HoughCircles(gray, cv2.CV_HOUGH_GRADIENT , 1.2, 100) AttributeError: 'module' 对象没有属性 'CV_HOUGH_GRADIENT'
    • 我刚刚编辑了我的帖子,OpenCV3 中的常量发生了变化。
    猜你喜欢
    • 1970-01-01
    • 2017-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-02
    • 2017-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多