OpenCV(4.2.0)错误:(-215:声明失败)size.width> 0 && size.height> 0在函数'cv :: imshow'中

问题描述 投票:0回答:1

我一直收到此错误。我已经从This website复制了代码并进行了更改,以使代码真正起作用。代码:

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", help="Image Path")
args = vars(ap.parse_args())
img_path = args['image']

#Reading image with opencv
img = cv2.imread(img_path)

#Reading csv file with pandas and giving names to each column
index=["color","color_name","hex","R","G","B"]

csv = pd.read_csv('colors.csv', names=index, header=None)
cv2.namedWindow('image')
cv2.setMouseCallback('image', draw_function)

while(1): 
    #This is error line
    cv2.imshow("image",img)
    [...]

完整的回溯:

Traceback (most recent call last):
  File "C:\Users\someone\Documents\python\____The Useless Installer____\PY\color_detection.py", line 39, in <module>
    cv2.imshow("image",img)
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'

我看过herehere,但是我不知道如何解决这个问题,我也不认为这与我的问题有太大关系。如果这很明显,我很抱歉。我是一个完全的新手。谢谢

python opencv argparse cv2 imshow
1个回答
1
投票

基本上,此错误告诉您您正在尝试显示 / 不存在图像。请检查:

  • 路径:我认为问题出在cv2.imread()如果路径不正确,则img变量将为空

您尝试的方式读取图像几乎是正确的:

img = cv2.imread(C:\Users\someone\Documents\python\____The Useless Installer____\PY\colorpic)

应该这样

  • 双反斜杠用于转义在编程语言中具有特殊含义的"\"字符
  • 您确实需要输入图片的格式(jpegpng等。]。>
  • 您需要将此参数作为'string'"string"传递

  • 因此请尝试

img = cv2.imread("C:\\Users\\someone\\Documents\\python\\____The Useless Installer____\\PY\\colorpic.jpg")
© www.soinside.com 2019 - 2024. All rights reserved.