- 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 讀取影像
讀取影像是在執行諸如裁剪、調整大小、旋轉或使用影像處理工具應用濾鏡等操作時的基本步驟。讀取影像的過程包括捕獲影像檔案中的畫素值和元資料,並將其表示為合適的 資料結構,例如 NumPy 陣列或矩陣。
在 Scikit-image 庫中,io 模組提供了一個 imread() 函式,用於將影像檔案載入到記憶體中作為 NumPy 陣列。一旦影像作為 NumPy 陣列載入到記憶體中,我們就可以訪問 Scikit-image 中提供的各種影像處理函式來執行諸如濾波、分割、特徵提取等任務。
imread() 方法
Scikit-image 中的 **io.imread()** 方法能夠讀取各種格式的影像,包括 JPEG、PNG、TIFF、BMP 等。該函式內部根據具體的影像格式使用不同的庫,例如 imageio、PIL 或 tifffile。
語法
以下是此方法的語法和引數:
skimage.io.imread(fname, as_gray=False, plugin=None, **plugin_args)
- **Fname** - 表示影像檔名或路徑或 URL 的字串。
- **as_gray (可選)** - 如果設定為 True,則將彩色影像轉換為表示為 64 位浮點數的灰度影像。已經是灰度格式的影像不會被轉換。
- **plugin (可選)** - 指定用於讀取影像的外掛的字串。如果未提供 plugin 引數,則該函式將自動嘗試不同的外掛,從 imageio 開始,直到找到合適的外掛。但是,如果檔名 (fname) 具有“.tiff”副檔名,則預設情況下將使用 tifffile 外掛。
- **plugin_args (可選)** - 傳遞給指定外掛的其他關鍵字引數。
返回值
該方法返回一個表示影像的 NumPy 陣列。該陣列包含畫素值,其形狀對應於影像的尺寸。影像的顏色帶或通道儲存在陣列的第三維中。
例如,灰度影像的尺寸為 **MxN**,RGB 影像的尺寸為 **MxNx3**,RGBA 影像的尺寸為 **MxNx4**。
示例 1
以下示例演示如何使用檔名載入影像。
import skimage
from skimage import io
# Read an image
image = io.imread('Images/logo.png')
# Display image properties from the image array
print('The following are the properties of the loaded image:')
print("Image shape:", image.shape)
print("Image data type:", image.dtype)
print("Number of color channels:", image.shape[2])
輸入影像
輸出
The following are the properties of the loaded image: Image shape: (225, 225, 4) Image data type: uint8 Number of color channels: 4
示例 2
以下示例演示如何使用影像 URL 載入影像。
import skimage
from skimage import io
# Read an image
image = io.imread('https://tutorialspoint.tw/images/logo.png')
# Display image properties from the image array
print('The following are the properties of the loaded image:')
print("Image shape:", image.shape)
print("Image data type:", image.dtype)
print("Number of color channels:", image.shape[2])
輸入影像
輸出
The following are the properties of the loaded image: Image shape: (140, 568, 3) Image data type: uint8 Number of color channels: 3
示例 3
以下示例演示如何透過使用 imread() 方法的 as_gray 引數將彩色影像轉換為灰度影像。
import skimage
from skimage import io
# Read an image
image = io.imread('https://tutorialspoint.tw/images/logo.png', as_gray=True)
# Display image properties from the image array
print('The following are the properties of the loaded image:')
print("Image shape:", image.shape)
print("Image data type:", image.dtype)
輸出
The following are the properties of the loaded image: Image shape: (140, 568) Image data type: float64
廣告