Hello.py:错误:需要以下参数:-i/ - 图像

发布于 2025-01-24 16:57:09 字数 1418 浏览 1 评论 0原文

我从网络上拿出了一块代码,该代码使用OpenCV在图像中查找圆圈。

# 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 = r"C:\Users\user\Desktop\Sem 8T\Mini Project\Data\Imagestraffic.jpg")
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)

但是,我一直遇到相同的错误:hello..py:错误:需要以下参数:-i/ -

我尝试过的ap.parse_args(args)而不是ap.parse_args(),但这似乎不是解决它。

I have taken a piece of code from the web which uses OpenCV to find circles in an image.

# 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 = r"C:\Users\user\Desktop\Sem 8T\Mini Project\Data\Imagestraffic.jpg")
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)

However, I keep getting the same error: hello.py: error: the following arguments are required: -i/--image

I've tried ap.parse_args(args) instead of ap.parse_args() but that doesn't seem to solve it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

菊凝晚露 2025-01-31 16:57:09

有两种方法可以做到。

ap.add_argument("-i",  r"C:\Users\user\Desktop\Sem 8T\Mini Project\Data\Imagestraffic.jpg", required=True,
    help="path to input image")

ap.add_argument("-i", "--image", required = True, help = "Path to the image")

There are two ways to do it.

ap.add_argument("-i",  r"C:\Users\user\Desktop\Sem 8T\Mini Project\Data\Imagestraffic.jpg", required=True,
    help="path to input image")

ap.add_argument("-i", "--image", required = True, help = "Path to the image")
木有鱼丸 2025-01-31 16:57:09

希望这会起作用!

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



# load the image, clone it for output, and then convert it to grayscale
image = cv2.imread(r"C:\Users\user\Desktop\Sem 8T\Mini Project\Data\Imagestraffic.jpg")
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)

Hope this will work !

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



# load the image, clone it for output, and then convert it to grayscale
image = cv2.imread(r"C:\Users\user\Desktop\Sem 8T\Mini Project\Data\Imagestraffic.jpg")
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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文