當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python skimage.exposure.rescale_intensity用法及代碼示例

用法:

skimage.exposure.rescale_intensity(image, in_range='image', out_range='dtype')

拉伸或縮小其強度級別後返回圖像。

輸入和輸出所需的強度範圍,分別為in_range和out_range,用於拉伸或縮小輸入圖像的強度範圍。請參閱下麵的示例。

參數

image數組

圖像陣列。

in_range, out_rangestr 或 2 元組,可選

輸入和輸出圖像的最小和最大強度值。下麵列舉了該參數的可能值。

‘image’

使用圖像最小/最大作為強度範圍。

‘dtype’

使用圖像 dtype 的 min/max 作為強度範圍。

dtype-name

根據所需的 dtype 使用強度範圍。必須是DTYPE_RANGE 中的有效 key 。

2元組

使用range_values 作為明確的最小/最大強度。

返回

out數組

重新調整其強度後的圖像陣列。此圖像與輸入圖像具有相同的 dtype。

注意

例子

默認情況下,輸入圖像的最小/最大強度被拉伸到圖像 dtype 允許的限製,因為 in_range 默認為 ‘image’ 而 out_range 默認為 ‘dtype’:

>>> image = np.array([51, 102, 153], dtype=np.uint8)
>>> rescale_intensity(image)
array([  0, 127, 255], dtype=uint8)

很容易意外地將圖像 dtype 從 uint8 轉換為 float:

>>> 1.0 * image
array([ 51., 102., 153.])

使用 rescale_intensity 重新縮放到 float dtypes 的正確範圍:

>>> image_float = 1.0 * image
>>> rescale_intensity(image_float)
array([0. , 0.5, 1. ])

要保持原件的低對比度,請使用 in_range 參數:

>>> rescale_intensity(image_float, in_range=(0, 255))
array([0.2, 0.4, 0.6])

如果in_range 的最小/最大值大於/小於最小/最大圖像強度,則剪切強度級別:

>>> rescale_intensity(image_float, in_range=(0, 102))
array([0.5, 1. , 1. ])

如果您有一個帶符號整數的圖像,但想要將圖像重新縮放到正範圍,請使用 out_range 參數。在這種情況下,輸出 dtype 將是浮點數:

>>> image = np.array([-10, 0, 10], dtype=np.int8)
>>> rescale_intensity(image, out_range=(0, 127))
array([  0. ,  63.5, 127. ])

要獲得具有特定 dtype 的所需範圍,請使用 .astype()

>>> rescale_intensity(image, out_range=(0, 127)).astype(np.int8)
array([  0,  63, 127], dtype=int8)

如果輸入圖像是常量,輸出會直接裁剪到輸出範圍:>>> image = np.array([130, 130, 130], dtype=np.int32) >>> rescale_intensity(image, out_range=(0, 127)).astype(np.int32) 數組([127, 127, 127], dtype=int32)

相關用法


注:本文由純淨天空篩選整理自scikit-image.org大神的英文原創作品 skimage.exposure.rescale_intensity。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。