2016-12-24 68 views
1

我正在Python 2.7.12中使用OpenCV Canny Edge Detection。在导入matplotlib我遇到以下错误:UnicodeDecodeError while Image Processing in Python

> **File "C:\Python27\lib\site-packages\matplotlib\font_manager.py", line 398, in ttfFontProperty 
    sfnt4 = sfnt4.decode('ascii').lower() 
UnicodeDecodeError: 'ascii' codec can't decode byte 0x82 in position 0: ordinal not in range(128)** 

我已经猎杀了答案,但似乎并没有与图片的时候找到一个解决方案。这里是我的代码:

import numpy as np 
import cv2 
from matplotlib import pyplot as plt 
import sys 

#read image from file specified 
img = cv2.imread('test.tif', cv2.IMREAD_COLOR); 

#define display window name 
windowname = "Image Segmentation"; 

#check if image has loaded 
if not img is None: 
    edges = cv2.Canny(img,100,200) 
    plt.subplot(121),plt.imshow(img,cmap = 'gray') 
    plt.title('Original Image'), plt.xticks([]), plt.yticks([]) 
    plt.subplot(122),plt.imshow(edges,cmap = 'gray') 
    plt.title('Edge Image'), plt.xticks([]), plt.yticks([]) 
    plt.show() 

    #start the event loop - essential 
    #cv2.waitKey() is a keyboard binding function (argument is time in ms). 
    #if you press any key in that time, the program continues. 
    #if 0 is passed, it waits indefinitely for a key stroke 

    key = cv2.waitKey(0); 

    #It can also be set to detect specific key strokes by recording which  key is pressed 

    if (key == ord('x')): 
     cv2.destroyAllWindows(); 

else: 
    print ("No image file successfully loaded");** 
+0

Traceback的其余部分在哪里? – furas

+0

回溯时间过长,超过了字符数限制!无法发布 – singhuist

回答

2

有似乎是的.tif图像中的非ASCII属性(字体部分),其中你的matplotlib库的版本不支持。

在Python 3.4的matplotlib包,我在C:\Python34\lib\site-packages\matplotlib\font_manager.py以下代码:

if sfnt4: 
    sfnt4 = sfnt4.decode('macroman').lower() 

和解码包含非ASCII字符工作字体名称。

编辑:看来,你只需要更新您的matplotlib包:

pip install --upgrade matplotlib 

我安装了最新的matplotlib的Python 2.7(这是奉献:))和编码已经切换到macroman,所以只是升级将解决你的问题。

+0

升级没有帮助,但手动安装:) – singhuist