OpenCV Python 不使用 imread() 打开图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24564889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OpenCV Python not opening images with imread()
提问by Kyle772
I'm not entirely sure why this is happening but I am in the process of making a program and I am having tons of issues trying to get opencv to open images using imread. I keep getting errors saying that the image is 0px wide by 0px high. This isn't making much sense to me so I searched around on here and I'm not getting any answers from SO either.
我不完全确定为什么会发生这种情况,但我正在制作一个程序,并且在尝试让 opencv 使用 imread 打开图像时遇到了很多问题。我不断收到错误消息,说图像宽 0px,高 0px。这对我来说没有多大意义,所以我在这里搜索,我也没有从 SO 那里得到任何答案。
I have taken about 20 pictures and they are all using the same device. Probably 8 of them actually open and work correctly, the rest don't. They aren't corrupted either because they open in other programs. I have triple checked the paths and they are using full paths.
我拍了大约 20 张照片,它们都使用相同的设备。可能其中 8 个实际上打开并正常工作,其余的则没有。它们也没有损坏,因为它们在其他程序中打开。我已经三重检查了路径,他们使用的是完整路径。
Is anyone else having issues like this? All of my files are .jpgs and I am not seeing any problems on my end. Is this a bug or am I doing something wrong?
还有其他人有这样的问题吗?我的所有文件都是 .jpg,我没有看到任何问题。这是一个错误还是我做错了什么?
Here is a snippet of the code that I am using that is reproducing the error on my end.
这是我正在使用的代码片段,它在我结束时重现了错误。
imgloc = "F:\Kyle\Desktop\Coinjar\Test images\ten.png"
img = cv2.imread(imgloc)
cv2.imshow('img',img)
When I change the file I just adjust the name of the file itself the entire path doesn't change it just refuses to accept some of my images which are essentially the same ones.
当我更改文件时,我只是调整文件本身的名称,整个路径不会改变,它只是拒绝接受我的一些本质上相同的图像。
I am getting this error from a later part of the code where I try to use img.shape
我在尝试使用 img.shape 的代码的后面部分收到此错误
Traceback (most recent call last):
File "F:\Kyle\Desktop\Coinjar\CoinJar Test2.py", line 14, in <module>
height, width, depth = img.shape
AttributeError: 'NoneType' object has no attribute 'shape'
and I am getting this error when I try to show a window from the code snippet above.
当我尝试从上面的代码片段中显示一个窗口时,我收到了这个错误。
Traceback (most recent call last):
File "F:\Kyle\Desktop\Coinjar\CoinJar Test2.py", line 11, in <module>
cv2.imshow('img',img)
error: ..\..\..\..\opencv\modules\highgui\src\window.cpp:261: error: (-215) size.width>0 && size.height>0 in function cv::imshow
采纳答案by furas
Probably you have problem with special meaning of \
in text - like \t
or \n
可能您对\
文本中的特殊含义有疑问-例如\t
或\n
Use \\
in place of \
\\
代替使用\
imgloc = "F:\Kyle\Desktop\Coinjar\Test images\ten.png"
or r''
或者 r''
imgloc = r"F:\Kyle\Desktop\Coinjar\Test images\ten.png"
EDIT:
编辑:
Some modules except even /
- like in Linux path
一些模块除了 even /
- 比如在 Linux 路径中
imgloc = "F:/Kyle/Desktop/Coinjar/Test images/ten.png"
回答by Cyril ROUDOT
Take care to :
注意:
- try
imread()
with a reliable picture, - and the correct path in your context like (see Kyle772 answer). For me either
//
or\
.
- 尝试
imread()
可靠的图片, - 以及您上下文中的正确路径(参见 Kyle772 答案)。对我来说,无论是
//
或\
。
I lost a couple of hours trying with 2 images saved from a left click in a browser. As soon as I took a personal camera image, it works fine.
我花了几个小时尝试使用浏览器左键单击保存的 2 个图像。一旦我拍摄了个人相机图像,它就可以正常工作。
#context windows10 / anaconda / python 3.2.0
import cv2
print(cv2.__version__) # 3.2.0
imgloc = "D:/violettes/Software/Central/test.jpg" #this path works fine.
# imgloc = "D:\violettes\Software\Central\test.jpg" this path works fine also.
#imgloc = "D:\violettes\Software\Central\test.jpg" #this path fails.
img = cv2.imread(imgloc)
height, width, channels = img.shape
print (height, width, channels)
回答by Abdullah Khilji
Use cv2.imread()
with a proper picture. Also use either //
or \
appropriately.
For Example:
使用cv2.imread()
适当的图片。也可以适当地使用//
或\
使用。例如:
img = cv2.imread(location_to_image_with_appropriate_file_delimitation)
h, w, c = img.shape
print(h, w, c)
回答by Jivan Kandel
When you get error like this AttributeError: 'NoneType' object has no attribute 'shape'
当你得到这样的错误时 AttributeError: 'NoneType' object has no attribute 'shape'
Try with new_image=image.copy
尝试使用 new_image=image.copy
回答by forrealism
My suggestion for everyone facing the same problem is to try this:
我对每个面临同样问题的人的建议是试试这个:
cv2.imshow("image", img)
The img
is keyword. Never forget.
的img
是关键字。永远不要忘记。
回答by Elegant Code
From my experience, file paths that are too long (OS dependent) can also cause cv2.imread()
to fail.
根据我的经验,太长(依赖于操作系统)的文件路径也会导致cv2.imread()
失败。
Also, when it does fail, it often fails silently, so it is hard to even realize that it failed, and usually something further the the code will be what sparks the error.
此外,当它确实失败时,它通常会默默地失败,所以甚至很难意识到它失败了,通常代码更进一步的东西会引发错误。
Hope this helps.
希望这可以帮助。