找不到cv2.imread标志

时间:2013-09-25 19:49:56

标签: python opencv python-2.7

我最近开始使用openCV和python,并决定分析一些示例代码,以了解事情是如何完成的。

但是,我找到的示例代码不断抛出此错误:

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'

我正在使用的代码可以在下面找到:

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

这是我的安装问题吗?我使用this website作为安装python和openCV的指南。

2 个答案:

答案 0 :(得分:35)

OpenCV 3.0附带了一些命名空间更改,这可能就是其中之一。另一个答案中给出的函数参考是针对OpenCV 2.4.11的,不幸的是有重要的重命名,包括枚举参数。

根据OpenCV 3.0 Example here,正确的参数是cv2.IMREAD_COLOR。

根据OpenCV 3.0 Reference Manual for C,CV_LOAD_IMAGE_COLOR仍然存在。

我从上面的资源和here得出结论,他们在OpenCV 3.0 python实现中改变了它。

目前,最好使用如下所示:

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

答案 1 :(得分:-1)

你试过这个吗?

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

请参阅imread,同时查看this