最近我开始使用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
这是我的安装问题吗?我使用这个网站作为安装Python和OpenCV的指南。
import cv2
之后,你能否添加print cv2.__file__
并让我们知道它显示了什么?你导入的cv2
模块可能不是你想要的那个。 - DSMcv2.__version__
显示什么?同时尝试使用1
作为标志 (1
是CV_LOAD_IMAGE_COLOR
标志的值)。 - Igonato