1. 程式人生 > 其它 >【code基礎】skimage.transform.resize的一些理解

【code基礎】skimage.transform.resize的一些理解

skimage.transform.resize(order, preserve_range) order: 插值的方法0-5:0-最近鄰;1-雙線性; https://blog.csdn.net/qq_34798326/article/details/84976243 skimage在讀使用io.imread讀取灰度影象時(as_grey=True / as_gray=True)會做歸一化處理資料型別轉化為float64;

影象縮放transform.resize同樣會將uint8的影象轉化為float64型別,這裡注意的是!!!!!!!如果已經歸一化,但是型別依然是uint8的影象,在縮放之後影象的範圍將不再是(0-1)。
主要在resize方面,cv2.resize就是單純調整尺寸,而skimage.transform.resize會順便把圖片的畫素歸一化縮放到(0,1)區間內;
preserve_range : bool, optional
        Whether to keep the original range of values. Otherwise, the input
        image is converted according to the conventions of `img_as_float`.
通過在程式碼裡設定preserve_range=True就可以保持原來的模式了。 https://scikit-image.org/docs/dev/api/skimage.transform.html?highlight=resize#skimage.transform.resize   發現使用skimage.transform.resize之後,語義分割標籤影象的數值發生了改變,資料型別也發生了改變,最關鍵的是數值發生也發生了改變;仔細查閱官方文件,新增anti_aliasing=False選項即可,因為預設是進行高斯濾波的;
from skimage import io, transform, color
import numpy as np print('skimage: ', skimage.__version__) a=np.zeros((20, 20)) a[2:8, 1:3]=1 a[1:3, 4:9]=2 a[3:9, 6:8]=2 print(a) print(a.shape) print(a.dtype) print(np.unique(a)) b = skimage.transform.resize(a,(10, 10),mode='constant', order=0, anti_aliasing=False, preserve_range=True) print(b) print(b.shape)
print(b.dtype) print(np.unique(b)) label = skimage.io.imread('sample800/label/0705_1.png') print(label.shape) #(800, 800) print(label.dtype) print(np.unique(label)) lbl = skimage.transform.resize(label,(512, 512),mode='edge', order=0, anti_aliasing=False, preserve_range=True) print(lbl.shape) print(lbl.dtype) print(np.unique(lbl)) label_show = np.zeros((512, 512, 3), dtype=np.uint8) COLORS = [(0, 0, 0), (0, 255, 0), (0, 0, 255), (238, 18, 137), (162, 205, 90), (70, 130, 180), (238, 238, 0), (255, 69, 0), (205, 145, 158), (238, 92, 66), (144, 238, 144), (124, 205, 124), (0, 229, 238), (151, 255, 255), (205, 190, 112)] for i in range(1, 9): label_show[lbl==i]=COLORS[i] import cv2 cv2.imshow("label_img", label_show) cv2.waitKey(1)
View Code

description:

anti_aliasingbool, optional
Whether to apply a Gaussian filter to smooth the image prior to downsampling. It is crucial to filter when downsampling the image to avoid aliasing artifacts. If not specified, it is set to True when downsampling an image whose data type is not bool.

 不知道為什麼 preserve_range 不起作用?????

 

參考

1. 踩坑:skimage中對影象做的歸一化處理

2. skimage.transform.resize

3. skimage_github_issue