2016-12-24 70 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