读入OpenCV Python断言失败错误

时间:2018-11-19 00:09:16

标签: python opencv

当我在与脚本相同的目录中引用文件时,代码将打开图像。

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)

art.jpg

images / art.jpg

回溯(最近通话最近):   文件“ slideShow.py”,第12行     cv2.imshow(“原始图片”,图片) cv2.error:OpenCV(3.4.2)/io/opencv/modules/highgui/src/window.cpp:356:错误:(-215:断言失败)size.width> 0 && size.height> 0 in function'展示”

1 个答案:

答案 0 :(得分:0)

您的错误在这里:

  

image = cv2.imread(“ path”)

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

  

image = cv2.imread(path)

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)