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.
imread , then the likely cause of the error is an invalid file path supplied to cv2. imread . The cv2. imread function does not explicitly throw an error message if you give it an invalid file path (i.e., a path to a nonexistent file).
The flags parameter of imread() method (IMREAD_XXX) filename − It accepts an argument (filename), a variable of the String type representing the path of the file that is to be read. flags − An integer value representing a predefined flag value.
cv2. imread() method loads an image from the specified file. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format) then this method returns an empty matrix. Syntax: cv2.imread(path, flag)
Typically, the cv2. imread function will return None if the path to the input image is invalid, so be sure to double-check and triple-check your input image paths! The function will also fail for unsupported image file types, although OpenCV can read a wide variety of them, including JPEG, PNG, TIFF, and GIF.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With