未找到 cv2.imread 标志
Posted
技术标签:
【中文标题】未找到 cv2.imread 标志【英文标题】:cv2.imread flags not found 【发布时间】:2013-10-01 13:53:29 【问题描述】:我最近开始使用 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的指南。
【问题讨论】:
这对我有用。在import cv2
之后,您可以添加print cv2.__file__
并告诉我们它的内容吗?您正在导入的 cv2
模块可能不是您认为的那个。
@DSM 当我输入 print cv2.__file__ 我收到 /usr/local/lib/python2.7/dist-packages/cv2.so
@Elijah1210 看起来不错。 cv2.__version__
说什么?也可以尝试使用1
作为标志(1
是CV_LOAD_IMAGE_COLOR
标志的值)。
尝试 cv2.IMREAD_GRAYSCALE、cv2.IMREAD_UNCHANGED、cv2.IMREAD_COLOR 等。或者分别为 0,-1,1
@Igonato 我得到 3.0.0-dev。它使用 1 作为标志。
【参考方案1】:
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)
【讨论】:
为什么一个包会像那样公然破坏向后兼容性?真气【参考方案2】:你试过了吗?
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
【讨论】:
以上是关于未找到 cv2.imread 标志的主要内容,如果未能解决你的问题,请参考以下文章