2016-07-20 12 views
0

にHoughcirclesでサークルを検出しよう:私は現在、次のい私は方法Houghcirclesを使用してOpenCVの中の円を検出しようとしていますが、私は自分のコードを実行しようとしていたとき、私は次のエラーを取得する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) 

答えて

1
cv2.cv.HoughCircles(gray, cv2.cv.CV_HOUGH_GRADIENT, 1.2, 100) 

エラーメッセージはそれをすべて言います。あなたのコードにタイプミスがあり、メソッドは2番目のcvモジュール指定子なしで呼び出されるべきです。

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

私はこれをやってみましたし、エラーを得た:トレースバック(最新の呼び出しの最後): ファイル "detect_circles.py"、ライン19を、 円=のcv2.HoughCircles(グレー、cv2.CV_HOUGH_GRADIENT、1.2に、100) AttributeError: 'module'オブジェクトに 'CV_HOUGH_GRADIENT'属性がありません –

+1

私は自分の投稿を編集しましたが、定数はOpenCV3で変更されました。 – s1h

+0

ありがとうございました! –

関連する問題