2012-11-11 56 views

回答

26

要創建與存儲器中緩衝液(StringIO的),我們可以使用的OpenCV API imdecode的OpenCV的圖像對象,請參閱以下代碼:

import cv2 
import numpy as np 
from urllib2 import urlopen 
from cStringIO import StringIO 

def create_opencv_image_from_stringio(img_stream, cv2_img_flag=0): 
    img_stream.seek(0) 
    img_array = np.asarray(bytearray(img_stream.read()), dtype=np.uint8) 
    return cv2.imdecode(img_array, cv2_img_flag) 

def create_opencv_image_from_url(url, cv2_img_flag=0): 
    request = urlopen(url) 
    img_array = np.asarray(bytearray(request.read()), dtype=np.uint8) 
    return cv2.imdecode(img_array, cv2_img_flag) 
+0

返回的是無如果圖像不能被解碼。我必須添加從字符串開頭刪除'data:image/png; base64,'這是防止opencv解碼。 –

+0

第二個答案我已經看到這樣但它不適合我。我不能相信它可以是一個這樣的問題加載一個URL從一個圖像打開cv – CashCow

+0

imread給我一個TypeError:期望的字符串或Unicode對象,numpy.ndarray發現錯誤。 – raincrash

相關問題