OpenCV Python not opening images with imread()

狂风中的少年 提交于 2019-11-30 07:37:17

问题


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.

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.

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?

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

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

回答1:


Probably you have problem with special meaning of \ in text - like \t or \n

Use \\ in place of \

imgloc = "F:\\Kyle\\Desktop\\Coinjar\\Test images\\ten.png"

or r''

imgloc = r"F:\Kyle\Desktop\Coinjar\Test images\ten.png"

EDIT:

Some modules except even / - like in Linux path

imgloc = "F:/Kyle/Desktop/Coinjar/Test images/ten.png"



回答2:


Take care to :

  • try imread() with a reliable picture,
  • and the correct path in your context like (see Kyle772 answer). For me either //or \.

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.

Spyder screen shot

    #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)

python opencv image-loading imread




回答3:


My suggestion for everyone facing the same problem is to try this:

cv2.imshow("image", img)

The img is keyword. Never forget.




回答4:


For, cv2.imread(img_url) do not use backslash for escaping blank space (in img_url).




回答5:


When you get error like this AttributeError: 'NoneType' object has no attribute 'shape'

Try with new_image=image.copy



来源:https://stackoverflow.com/questions/24564889/opencv-python-not-opening-images-with-imread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!