You are not logged in.

#1 2014-03-07 03:02:09

sterlingm
Member
Registered: 2013-12-24
Posts: 10

Using OpenCV with Python

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.

Offline

#2 2014-03-07 07:26:27

ngoonee
Forum Fellow
From: Between Thailand and Singapore
Registered: 2009-03-17
Posts: 7,355

Re: Using OpenCV with Python

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.

Offline

#3 2014-03-07 10:05:23

nourathar
Member
From: Bxl
Registered: 2013-04-26
Posts: 109

Re: Using OpenCV with Python

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.

Offline

#4 2014-03-07 14:14:25

sterlingm
Member
Registered: 2013-12-24
Posts: 10

Re: Using OpenCV with Python

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?

Offline

#5 2014-03-07 14:30:38

nourathar
Member
From: Bxl
Registered: 2013-04-26
Posts: 109

Re: Using OpenCV with Python

ah ok.
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)

Offline

#6 2014-03-07 14:40:17

sterlingm
Member
Registered: 2013-12-24
Posts: 10

Re: Using OpenCV with Python

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?

Offline

#7 2014-03-07 14:52:36

nourathar
Member
From: Bxl
Registered: 2013-04-26
Posts: 109

Re: Using OpenCV with Python

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

open('my_image.jpg', 'rb')

that might give you a more informative error message.

Last edited by nourathar (2014-03-07 14:57:52)

Offline

#8 2014-03-07 15:10:53

sterlingm
Member
Registered: 2013-12-24
Posts: 10

Re: Using OpenCV with Python

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.

Offline

#9 2014-03-07 16:15:40

nourathar
Member
From: Bxl
Registered: 2013-04-26
Posts: 109

Re: Using OpenCV with Python

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

Offline

#10 2014-03-07 22:06:09

sterlingm
Member
Registered: 2013-12-24
Posts: 10

Re: Using OpenCV with Python

It is the same for me. Great! Thanks for the help! I will find a way to deal with .jpgs.

Offline

Board footer

Powered by FluxBB