繁体   English   中英

在RaspberryPi上使用opencv 3和Python进行霍夫圆检测

[英]Hough circle detection using opencv 3 and Python on RaspberryPi

我试图在具有opencv3的树莓派上运行此代码以进行hough检测,因为cv2已安装并在虚拟环境包装器中运行

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

# 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"])
output = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# detect circles in the image
circles = cv2.HoughCircles(gray, cv2.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) 

但是当我尝试运行此代码时,出现以下错误:

Traceback (most recent call last):
  File "hough.py", line 12, in <module>
    output = image.copy()
AttributeError: 'NoneType' object has no attribute 'copy'

我正在使用一个简单的png图像,并从pyimagesearch.com中获取了示例代码。

这是什么产生错误?

这是因为未读取您的图像,要进行交叉检查,请在命令窗口中键入:image。 如果您将输出作为numpy数组获得,则问题可能出在其他方面。 您如何运行此程序? 它应该通过输入以下命令通过命令行或终端运行:

python program_name.py --image 'Path_of_image_on_which_you_want_the_program_to_operate'

如果您在ubuntu或Linux或MacOs上的python3上运行,请输入以下命令:

python3 program_name.py --image 'Path_of_image_on_which_you_want_the_program_to_operate'

如果您使用的是Windows,则可以使用:

py3 program_name.py --image 'Path_of_image_on_which_you_want_the_program_to_operate'

确保在命令行(CMD)或终端而不是在python的Shell中键入它,因为该程序应该由argparser执行。

暂无
暂无

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

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