OpenCV Python 断言失败错误 imread

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

当我引用与脚本位于同一目录中的文件时,代码会打开图像。

import cv2

image = cv2.imread("art.jpg")
cv2.imshow("original", image)
cv2.waitKey(0)

每当我尝试从更深的一个目录打开同一文件时,我只会收到断言失败错误。

import cv2
import os
import random

dir = 'images'
filename = random.choice(os.listdir("images"))
print(filename)
path = os.path.join(dir, filename)
print(path)

image = cv2.imread("path")
cv2.imshow("original", image)
cv2.waitKey(0)

艺术.jpg

图片/art.jpg

Traceback (most recent call last):
  File "slideShow.py", line 12, in <module>
    cv2.imshow("original", image)
cv2.error: OpenCV(3.4.2) /io/opencv/modules/highgui/src/window.cpp:356: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'
python opencv
1个回答
0
投票

你的错误在这里:

图像 = cv2.imread(“路径”)

您的输入是一个字符串,将其更改为

图像 = cv2.imread(路径)

import cv2
import os
import random

dir = 'images'
filename = random.choice(os.listdir("images"))
print(filename)
path = os.path.join(dir, filename)
print(path)

image = cv2.imread(path)
cv2.imshow("original", image)
cv2.waitKey(0)
© www.soinside.com 2019 - 2024. All rights reserved.