23

I recently started working with openCV and python and decided to analyze some sample code to get an idea of how things are done.

However, the sample code I found, keeps throwing this error:

Traceback (most recent call last):
File "test.py", line 9, in <module>
img = cv2.imread(sys.argv[1],cv2.CV_LOAD_IMAGE_COLOR) ## Read image file
AttributeError: 'module' object has no attribute 'CV_LOAD_IMAGE_COLOR'

The code I was using can be found below:

import cv2
import sys
import numpy as np

if len(sys.argv) != 2: ## Check for error in usage syntax
    print "Usage : python display_image.py <image_file>"

else:
    img = cv2.imread(sys.argv[1], cv2.CV_LOAD_IMAGE_COLOR) ## Read image file

if img == None: ## Check for invalid input
    print "Could not open or find the image"
else:
    cv2.namedWindow('Display Window') ## create window for display
    cv2.imshow('Display Window', img) ## Show image in the window
    print "size of image: ", img.shape ## print size of image
    cv2.waitKey(0) ## Wait for keystroke
    cv2.destroyAllWindows() ## Destroy all windows

Is this a problem with my installation? I used this website as a guide to install python and openCV.

Igonato
  • 10,175
  • 3
  • 35
  • 64
Elijah1210
  • 329
  • 1
  • 2
  • 10
  • 1
    That works for me. After `import cv2`, could you add `print cv2.__file__` and let us know what it says? It's possible that the `cv2` module you're importing isn't the one you think it is. – DSM Sep 25 '13 at 20:24
  • @DSM When I type in print cv2.__file__ I receive /usr/local/lib/python2.7/dist-packages/cv2.so – Elijah1210 Sep 25 '13 at 21:28
  • 1
    @Elijah1210 That looks fine. What does `cv2.__version__` say? Also try this using `1` as a flag (`1` is the value of the `CV_LOAD_IMAGE_COLOR` flag). – Igonato Sep 26 '13 at 05:12
  • 6
    Try cv2.IMREAD_GRAYSCALE, cv2.IMREAD_UNCHANGED, cv2.IMREAD_COLOR etc. Or 0,-1,1 respectively – Abid Rahman K Sep 26 '13 at 12:58
  • @Igonato I get 3.0.0-dev. It works with 1 as a flag. – Elijah1210 Sep 26 '13 at 16:19
  • @AbidRahmanK It works with both cv2.IMREAD_COLOR and 1 as flag. – Elijah1210 Sep 26 '13 at 16:19

2 Answers2

42

OpenCV 3.0 came with some namespace changes, and this might be one of them. The function reference given in the other answer is for OpenCV 2.4.11, and unfortunately there are significant renamings, including enumerated parameters.

According to the OpenCV 3.0 Example here, the correct parameter is cv2.IMREAD_COLOR.

According to the OpenCV 3.0 Reference Manual for C, CV_LOAD_IMAGE_COLOR is still there.

And my conclusion from the above resources and here, they changed it in OpenCV 3.0 python implementation.

For now, the best to use seems like the following:

img = cv2.imread("link_to_your_file/file.jpg", cv2.IMREAD_COLOR) 
ilke444
  • 2,641
  • 1
  • 17
  • 31
-3

have you tried this?

import cv2
import sys
import numpy as np


cv2.CV_LOAD_IMAGE_COLOR = 1 # set flag to 1 to give colour image
#cv2.CV_LOAD_IMAGE_COLOR = 0 # set flag to 0 to give a grayscale one


img = cv2.imread("link_to_your_file/file.jpg", cv2.CV_LOAD_IMAGE_COLOR) 


cv2.namedWindow('Display Window') ## create window for display
cv2.imshow('Display Window', img) ## Show image in the window
print ("size of image: "), img.shape ## print size of image
cv2.waitKey(0) ## Wait for keystroke
cv2.destroyAllWindows() ## Destroy all windows

see imread also have a look at this

reggie
  • 181
  • 1
  • 9