使用 cv2.imread() 时出现 Python openCV 错误 [英] Python openCV error while using cv2.imread()

查看:128
本文介绍了使用 cv2.imread() 时出现 Python openCV 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import cv2
import numpy as np

#load color of an image in grayscale
img1 = cv2.imread('Tarun.jpg',0)

cv2.imshow('Hey its me Tarun yellogi',img1)

<小时>

error: C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:331: error: (-215) size.width>0 && size.height>0 in function cv::imshow

推荐答案

img1 = cv2.imread('Tarun.jpg',0)

如您所知,imread 读取指定的图像.仅使用文件名,如下所示,python 尝试从当前工作目录读取图像.如果文件不存在,则不会报错,img1 的值为 None.

As you know, imread reads in the specified image. With just the filename, as here, python tries to read the image from the current working directory. If the file does not exist, no error is reported and img1 will have a value of None.

这一行:

cv2.imshow('Hey it me Tarun yellogi',img1)

返回错误:

error: C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:331: error: (-215) size.width>0 &&函数 cv::imshow

表示 imshow 命令(即 img1)中指定的图像的宽度和/或高度为 0.错误消息的意思是imshow 函数中的图像大小必须大于零 高度大于零.

means that the image specified in the imshow command (i.e. img1) has a width and/or a height of 0. What the error message is saying is that the image in function imshow must have a size where the width is greater than zero and the height is greater than zero.

您可以使用 img1.shape 检查图像大小.在这种情况下,您可能会收到错误消息 AttributeError: 'NoneType' object has no attribute 'shape'.您可以使用 img1 == None 进一步检查 img1 是否为 None.如果您得到 True 的响应,那么您就会知道 img1 不包含图像数据.

You can check the image size with img1.shape. In this case you will probably get an error message AttributeError: 'NoneType' object has no attribute 'shape'. You can further check if img1 is None with img1 == None. If you get a response of True then you will know that img1 does not contain image data.

TL;DR

未找到文件Tarun.jpg",因此img1 == None.

The file 'Tarun.jpg' was not found thus img1 == None.

这篇关于使用 cv2.imread() 时出现 Python openCV 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆