Hi, I am trying to use OpenCV with Python. I am simply trying to imread a picture in and then imshow the picture. The problem is that every time I try to imread, it returns an empty matrix. I have to following lines:
import cv2 img = imread('myfile.jpg',0) cv2.imshow('my_picture',img)
It crashes on imshow with the following error:
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /home/sterlingm/builds/opencv/modules/highgui/src/window.cpp, line 266 Traceback (most recent call last): File "./test_cv.py", line 7, in <module> cv2.imshow('wfatwf',img) cv2.error: /home/sterlingm/builds/opencv/modules/highgui/src/window.cpp:266: error: (-215) size.width>0 && size.height>0 in function imshow
If I try to "print img", it always returns "None". If I try to do "img.shape", it crashes with:
Traceback (most recent call last): File "./test_cv.py", line 8, in <module> img.shape AttributeError: 'NoneType' object has no attribute 'shape'
Initially, I installed OpenCV through the official repositories (https://www.archlinux.org/packages/extra/x86_64/opencv/). After I ran into these problems, I uninstalled it and installed it from source. I installed the dependencies listed for the Fedora install from http://docs.opencv.org/trunk/doc/py_tut … edora.html that I could find in the Arch repositories. I added the path to cv2.so to my PYTHONPATH variable. However, I still get the same errors. What should I be looking for to solve the problem?
I wasn't sure whether to post this on a programming site (e.g. stackoverflow) or if it is an Arch Linux issue. Hopefully this hybrid forum will be able to help me. Please let me know if you need any other information and thanks for any help.
Shouldn't it be cv2.imread?
Allan-Volunteer on the (topic being discussed) mailn lists. You never get the people who matters attention on the forums.
jasonwryan-Installing Arch is a measure of your literacy. Maintaining Arch is a measure of your diligence. Contributing to Arch is a measure of your competence.
Griemak-Bleeding edge, not bleeding flat. Edge denotes falls will occur from time to time. Bring your own parachute.
As ngoonee already said, it should be cv2.imread(), so if you do not get a NameError in that line, that means that you have another imread() function that is being called.
You could try and see what imread.__module__ tells you: if imread is a function, it might tell you from which module it is. I believe there's 'imread' functions in SciPy and in Matplotlib, but it might be something you defined ?
I would advise you to revert to the OpenCv version from the official repositories; that will prevent problems with updating later and I see no reason that should not work for you.
Oh, actually I just forgot that cv2. So it looks like:
#!/usr/bin/env python2.7 import cv2 img = cv2.imread('myfile.jpg',0) cv2.imshow('my_picture',img)
I reverted back to the version in the official repositories. I still can't read in the image though. Could it be an issue with python2.7 vs python3? I specify python2.7 at the top, but maybe it requires more than that?
For me the reading works fine on Python 2.7, using OpenCV from the repositories etc., but I noticed that imread does not complain if the file does not exist. It will return None, so that is consistent with the behaviour you see.
Are you sure the path to your image is right ? DId you try with a full path ? How are you calling/starting your python code ?
Last edited by nourathar (2014-03-07 14:31:50)
According to http://docs.opencv.org/trunk/doc/py_tut … splay.html imread will not complain, it will just return an empty matrix. If I print out img, it prints "". If I do type(img), it returns 'None'. If I try to do img.shape, it crashes saying that 'NoneType object has no attribute shape'.
Right now I just have those lines in a .py and I run the script with "./the_script". I tried both the local and full paths of the image. I have full permissions on the image file. Does running that exact code work for you? And can you imshow?
If it can't read the file it returns 'None', (which is a special object that is not the same as empty matrix, since it has no shape attribute for instance).
I don't think it has anything to do with your code or your installation, but with your path or your image.
That exact code works for me (using the full path to my file),
img.shape gives me the dimensions of my image.
img.show() works too, but I need to do
cv2.img.show('blabla', img) cv2.waitKey()
otherwise the window closes instantly, but that might be related to me using i3, which sometimes behaves differently from non-tiling window managers.
what happens if you try to open your file with
that might give you a more informative error message.
Last edited by nourathar (2014-03-07 14:57:52)
I see. I tried to simply open it with
f = open('my_image.jpg', 'rb') print f
and had no errors. Printing f shows:
<open file '/home/sterlingm/unaware.jpg', mode 'rb' at 0x12dd780>
Just to be thorough, I changed the file name to something that doesn't exist and I got an error with open. Thanks for helping so far. Could it be some kind of path issue? My PYTHONPATH is "/usr/lib/python2.7/site-packages". This has the cv2.so and cv.py files.
ok, it turns out to be installation-related after all:
before I tried with a .png file and that works fine, but with a .jpg file it doesn't.
So apparently it can not find a compatible jpeg-library. I do not know how to solve this.
but options could be:
- read pngs or other file formats that are supported
- read your jpgs through another python module, such as PIL
It is the same for me. Great! Thanks for the help! I will find a way to deal with .jpgs.