2013-07-30 117 views
1

我使用puthon 2.7,windows 7和opencv 2.4.6。我尝试运行下面的代码:图像解码时出错(imdecode)

https://github.com/kyatou/python-opencv_tutorial/blob/master/08_image_encode_decode.py

#import opencv library 
import cv2 
import sys 
import numpy 

argvs=sys.argv 
if (len(argvs) != 2): 
print 'Usage: # python %s imagefilename' % argvs[0] 
quit() 

imagefilename = argvs[1] 
try: 
    img=cv2.imread(imagefilename, 1) 
except: 
    print 'faild to load %s' % imagefilename 
    quit() 

#encode to jpeg format 
#encode param image quality 0 to 100. default:95 
#if you want to shrink data size, choose low image quality. 
encode_param=[int(cv2.IMWRITE_JPEG_QUALITY),90] 
result,encimg=cv2.imencode('.jpg',img,encode_param) 
if False==result: 
    print 'could not encode image!' 
    quit() 

#decode from jpeg format 
decimg=cv2.imdecode(encimg,1) 

cv2.imshow('Source Image',img) 
cv2.imshow('Decoded image',decimg) 
cv2.waitKey(0) 
cv2.destroyAllWindows() 

我不断收到以下错误:

encode_param=[int(cv2.IMWRITE_JPEG_QUALITY), 90] 
AttributeError: 'module' object has no attribute 'IMWRITE_JPEG_QUALITY' 

我已经尝试了很多事情:重新安装OpenCV的,转换CV2到CV代码和搜索不同的论坛,但我不断收到此错误。我错过了什么吗?有没有人可以运行此代码而不会出现错误?

BTW:其他OpenCV的代码(从摄像头拍照)运行没有问题....

此刻,我将图像保存到一个临时JPG文件。使用imencode函数我想在内存中创建jpg文件。

在此先感谢和问候。

回答

1

这个问题不在你的代码中,它应该可以工作,但它与你的OpenCV Python包一样。我不能告诉你为什么提出这个错误,但你可以通过这个改变encode_param报关行避免:

encode_param=[1, 90] 
+0

感谢您的解决方案。所以第一个参数是一个枚举索引?我直接得到下一个错误:result,encimg = cv2.imencode('。jpg',img,[1,90])TypeError:'bool'对象不可迭代 –