[英]Python openCV error while using cv2.imread()
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)
As you know, imread
reads in the specified image.如您所知,
imread
读取指定的图像。 With just the filename, as here, python tries to read the image from the current working directory.仅使用文件名,如下所示,python 尝试从当前工作目录读取图像。 If the file does not exist, no error is reported and
img1
will have a value of None
.如果该文件不存在,则不会报告错误并且
img1
的值为None
。
That this line:这行:
cv2.imshow('Hey its me Tarun yellogi',img1)
returns an error of:返回错误:
error: C:\\projects\\opencv-python\\opencv\\modules\\highgui\\src\\window.cpp:331: error: (-215) size.width>0 && size.height>0 in function cv::imshow
means that the image specified in the imshow
command (ie 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.表示
imshow
命令中指定的图像(即img1
)的宽度和/或高度为 0。错误消息是说函数imshow
中的图像必须具有宽度大于零的大小,并且高度大于零。
You can check the image size with img1.shape
.您可以使用
img1.shape
检查图像大小。 In this case you will probably get an error message AttributeError: 'NoneType' object has no attribute 'shape'
.在这种情况下,您可能会收到错误消息
AttributeError: 'NoneType' object has no attribute 'shape'
。 You can further check if img1
is None
with img1 == None
.您可以进一步检查
img1
是None
与img1 == None
。 If you get a response of True
then you will know that img1
does not contain image data.如果您得到
True
响应,那么您就会知道img1
不包含图像数据。
TL;DR TL; 博士
The file 'Tarun.jpg' was not found thus img1 == None
.未找到文件“Tarun.jpg”,因此
img1 == None
。
os
module to make the path work in any other machine instead of hard-coding the absolute pathos
模块使路径在任何其他机器上工作,而不是硬编码绝对路径imshow()
is not getting the right image to displayimshow()
没有得到正确的图像来显示None
: print(img1.shape)
and print(img1)
None
: print(img1.shape)
和print(img1)
cv2.imread()
returns None
cv2.imread()
返回None
I think you can try this in reading the pictures in a loop to prevent the pause by Nonetype error!我认为您可以尝试在循环中读取图片以防止因 Nonetype 错误而暂停! It helps me in my case(Some jpg files is Nonetype in my case)
它对我有帮助(在我的情况下,一些 jpg 文件是 Nonetype)
if img is None:
continue
If the image is not in the current directory then you have to mention the absolute path to the image.如果图像不在当前目录中,则必须提及图像的绝对路径。
Use os
module to make the path work in any other machine instead of hard-coding the absolute path.使用
os
模块使路径在任何其他机器上工作,而不是硬编码绝对路径。
But this error message means that imshow()
is not getting the right image to display.但是这个错误信息意味着
imshow()
没有得到正确的图像来显示。 I would check the shape and the values of the image to make sure that the image is not corrupt.我会检查图像的形状和值以确保图像没有损坏。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.