2013-04-06 47 views
9

我想用scipy做圖像的erosiondilation。這看起來很簡單,使用scipy - >binary_erosion/dialation。但是,輸出完全不符合預期。Scipy的圖像侵蝕和膨脹

這裏是我的基本代碼:

import scipy 
from scipy import ndimage 
import matplotlib.pyplot as plt 
import numpy as np 
import Image 

#im = Image.open('flower.png') 
im = ndimage.imread('flower.png') 
im = ndimage.binary_erosion(im).astype(np.float32) 
scipy.misc.imsave('erosion.png', im) 


im2 = Image.open('flower.png') 
im2 = ndimage.binary_dilation(im2) 
scipy.misc.imsave('dilation.png', im2) 

這是輸出:

enter image description here

用於擴張的輸出僅僅是原來的 「flower.png」

全白圖像

我相信我必須指定一個更好的內核或掩碼,但我不確定爲什麼我得到一個綠色的輸出侵蝕和完全whi輸出用於擴張。

+1

二元運算符期望與您的二進制輸入不同 – theta 2013-04-06 20:19:38

回答

9

我正在使用二進制侵蝕而不是grey erosion陣列。我轉換的原始圖像通過使用flatten=true像這樣爲灰度:

im = scipy.misc.imread('flower.png', flatten=True).astype(np.uint8) 

然後叫:

im1 = ndimage.grey_erosion(im, size=(15,15)) 

,並得到了很好的侵蝕圖片,雖然它是灰度。

2

你有兩個問題:如在由@theta評論指出,二進制OPS期望輸入僅由0和1的第二個問題是在ndimagend ---你的身材(nx, ny, 3)的陣列供給。最後一個長度爲3的軸被認爲是第三個空間維度,而不是三個顏色通道。