2017-02-27 92 views
0
k = 1 
for k in range(1,21): 
    img = caffe.io.load_image(imgpath + str(k) + '.png') 
    result = caffe.io.load_image(imgpath + str(k) + '.png') 
    patch_dim = 33 
    h = (patch_dim - 1)/2 
    for i in range(patch_dim/2, img.shape[0] - patch_dim/2): 
     for j in range(patch_dim/2, img.shape[1] - patch_dim/2): 
      net.blobs['data'].data[...] = transformer.preprocess('data', img[i-h:i+h+1, j-h:j+h+1]) 
      out = net.forward() 
      if out['prob'][0][1] >= 0.8: 
       result[i][j][0] = 1 
    result.save(resultpath + str(k) + ".png") 
    k = k + 1 

這裏IMG加載是code.I加載圖像使用caffe.io.load_img,想處理後,將其保存,但有一個錯誤:如何節省caffe.io.load_image

AttributeError: 'numpy.ndarray' object has no attribute 'save' 

如何保存它?

+0

將'result'轉換爲'uint8',然後使用'PIL'保存它。 – Shai

回答

1

您可以使用PIL來代替保存圖像。我不認爲caffe有任何公開的方法來保存圖像。

編輯 - 是的,there's no功能保存圖像。

from PIL import Image 
img = Image.fromarray(result.astype('uint8')) # convert image to uint8 
img.save(path+'.png') 
+0

我試過,但圖像似乎無法處理該數據類型。我在我的問題中粘貼錯誤。 – StalkerMuse

+0

@StalkerMuse你試過把它轉換成uint8嗎? – hashcode55

+1

非常感謝,但結果必須* 255 – StalkerMuse