2016-11-23 132 views
1

我正在使用以下函數來調整形狀爲(samples, 1, image_row, image_column)的圖像集的大小。我正在使用skimage庫。使用skimage python庫調整圖像大小後丟失信息

from skimage import io 
from skimage.transform import resize 
def preprocess(imgs): 
imgs_p = np.ndarray((imgs.shape[0], imgs.shape[1], img_rows, img_cols), dtype=np.uint8) 
for i in range(imgs.shape[0]): 
    imgs_p[i, 0] = resize(imgs[i, 0], (img_rows, img_cols)) 
return imgs_p 

然而,我注意到,縮放後的圖像樣變成0-1陣列。以下是一些測試結果。我們可以看到調整大小的圖像只包含0-1值。我不知道我的調整大小功能有什麼問題。

print(image[0,0].shape) 

    (420, 580) 
    print(image[0,0]) 
[[ 0 155 152 ..., 87 91 90] 
    [ 0 255 255 ..., 140 141 141] 
    [ 0 255 255 ..., 157 156 158] 
    ..., 
    [ 0 77 63 ..., 137 133 122] 
    [ 0 77 63 ..., 139 136 127] 
    [ 0 77 64 ..., 149 144 137]] 

    print(resized_image[0,0].shape) 
    (96, 128) 
    print(resized_image[0,0]) 
    [[1 1 0 ..., 0 0 0] 
    [0 0 0 ..., 0 0 0] 
    [0 0 0 ..., 0 0 0] 
    ..., 
    [0 0 0 ..., 0 0 0] 
    [0 0 0 ..., 0 0 0] 
    [0 0 0 ..., 0 0 0]] 

回答

0

調整大小時,圖像將被轉換爲浮點。有一個可選的布爾標誌transform.resize()需要:preserve_range。從source code

preserve_range:BOOL,可選的。是否保持原有的價值範圍。否則,輸入圖像按照img_as_float的慣例轉換。

將其設置爲True它應該可以解決您的問題。

0

resize輸出具有浮體的D型細胞,因此它是在0-1範圍內。您可以將圖像轉換回UINT8範圍有:

from skimage import img_as_ubyte 
image = img_as_ubyte(image) 

請參閱user guide的數據類型及其範圍的完整描述。