Python 找不到 cv2.imread 标志
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19013961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cv2.imread flags not found
提问by Elijah1210
I recently started working with openCV and python and decided to analyze some sample code to get an idea of how things are done.
我最近开始使用 openCV 和 python 并决定分析一些示例代码以了解事情是如何完成的。
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 websiteas a guide to install python and openCV.
这是我安装的问题吗?我使用这个网站作为安装 python 和 openCV 的指南。
采纳答案by ilke444
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.
OpenCV 3.0 带来了一些命名空间更改,这可能是其中之一。另一个答案中给出的函数参考是针对 OpenCV 2.4.11 的,不幸的是有重要的重命名,包括枚举参数。
According to the OpenCV 3.0 Example here, the correct parameter is cv2.IMREAD_COLOR.
根据此处的OpenCV 3.0 示例,正确的参数是 cv2.IMREAD_COLOR。
According to the OpenCV 3.0 Reference Manual for C, CV_LOAD_IMAGE_COLOR is still there.
根据OpenCV 3.0 Reference Manual for C, CV_LOAD_IMAGE_COLOR 仍然存在。
And my conclusion from the above resources and here, they changed it in OpenCV 3.0 python implementation.
我从上述资源和这里得出的结论,他们在 OpenCV 3.0 python 实现中改变了它。
For now, the best to use seems like the following:
目前,最好的使用方法如下:
img = cv2.imread("link_to_your_file/file.jpg", cv2.IMREAD_COLOR)
回答by reggie
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