- Scikit Image 教程
- Scikit Image - 簡介
- Scikit Image - 影像處理
- Scikit Image - NumPy影像
- Scikit Image - 影像資料型別
- Scikit Image - 使用外掛
- Scikit Image - 影像處理
- Scikit Image - 讀取影像
- Scikit Image - 寫入影像
- Scikit Image - 顯示影像
- Scikit Image - 影像集合
- Scikit Image - 影像堆疊
- Scikit Image - 多影像
Scikit Image - 影像資料型別
在計算機程式設計的上下文中,資料型別是指根據資料的屬性及其可以執行的操作對資料進行分類或歸類。它決定了計算機如何解釋、儲存和處理資料。
不同的程式語言有自己的一套資料型別,每種資料型別都有其屬性。常見的資料型別包括整數、浮點數、字元、字串、布林值和陣列。
Python Scikit Image 中的資料型別
在 scikit-image 中,影像表示為 NumPy 陣列,並支援多種資料型別,也稱為“dtypes”。庫為 dtype 設定了某些範圍,以避免影像強度失真。以下是 scikit-image 中常用的 dtypes 及其相應的範圍:
- uint8 − 無符號 8 位整數,範圍從 0 到 255。
- uint16 − 無符號 16 位整數,範圍從 0 到 65535。
- uint32 − 無符號 32 位整數,範圍從 0 到 2^32 - 1。
- float − 浮點數,通常範圍從 -1 到 1 或 0 到 1。
- int8 − 有符號 8 位整數,範圍從 -128 到 127。
- int16 − 有符號 16 位整數,範圍從 -32768 到 32767。
- int32 − 有符號 32 位整數,範圍從 -2^31 到 2^31 - 1。
請注意,即使浮點型 dtype 本身可以超過此範圍,浮點影像通常應限制在 -1 到 1 的範圍內。另一方面,整數 dtype 可以跨越其各自資料型別的整個範圍。務必堅持這些範圍,以避免資料丟失或畫素強度解釋錯誤。
影像資料型別轉換函式
在 scikit-image 中,skimage.util 模組中有一些可用的函式可以轉換影像資料型別並確保影像強度的正確重新縮放。這些函式旨在處理轉換和重新縮放,同時保留影像的資料範圍。以下是 scikit-image 中的影像資料型別轉換函式:
- Img_as_float
- Img_as_ubyte
- Img_as_uint
- Img_as_int
這些函式提供了一種方便的方法來將影像轉換為所需的資料型別,同時保持正確的強度範圍。此外,重要的是避免直接在影像上使用 astype 函式,因為它可能會違反關於 dtype 範圍的假設。相反,您可以使用上述轉換函式來確保正確的 dtype 轉換和強度重新縮放。
示例 1
以下示例演示了在 scikit-image 中使用astype()方法和img_as_float()函式轉換影像陣列的資料型別之間的區別。
from skimage import util
import numpy as np
# Create an image with 8-bit unsigned integers
image = np.random.randint(0, 256, size=(1, 4), dtype=np.uint8)
print("Image array:", image)
# Convert the image to float using astype()
print('Converted to float using astype :',image.astype(float)) # These float values are out of range.
# Convert the image to float using img_as_float()
print("Converted to float using img_as_float:",util.img_as_float(image))
輸出
Image array: [[173 104 167 25]] Converted to float using astype : [[173. 104. 167. 25.]] Converted to float using img_as_float: [[0.67843137 0.40784314 0.65490196 0.09803922]]
透過使用img_as_float()函式,影像陣列被正確轉換為浮點資料型別,強度值在有效範圍內正確縮放。
這確保了正確的資料型別轉換和強度重新縮放。
示例 2
以下示例演示瞭如何使用skimage.util模組中的img_as_ubyte()函式將浮點影像陣列轉換為 8 位無符號整數表示。
from skimage import util
import numpy as np
# Create an image with floating point numbers
image = np.array([0, 0.1, 0, 0.8, 0.3, 1], dtype=float)
print("Image array:", image)
# Convert the image data to 8-bit Unsigned integers
print("Converted to 8-bit uint using img_as_ubyte:",util.img_as_ubyte(image))
輸出
Image array: [0. 0.1 0. 0.8 0.3 1.] Converted to 8-bit uint using img_as_ubyte: [0 26 0 204 76 255]