Mahotas - 載入影像



要對影像執行任何操作,我們首先需要將其載入到記憶體中。影像載入後,我們可以訪問其畫素並對其應用各種操作。

載入影像指的是將影像檔案從儲存裝置(如硬碟、隨身碟或網路位置)讀取到記憶體中。

在 Mahotas 中載入影像

要使用 Mahotas 載入影像,我們可以使用 **imread()** 函式,該函式讀取影像檔案並將其作為 NumPy 陣列返回。

NumPy 陣列是一個網格,其所有值都具有相同的型別,並由非負整數元組索引。對於影像而言,該陣列表示影像的畫素值。

使用 imread() 函式

imread() 函式是 Mahotas 中讀取和載入影像的核心方法。它接受檔案路徑作為輸入,並返回表示已載入影像的 NumPy 陣列。此函式可以讀取各種影像檔案格式,例如 JPEG、PNG、BMP、TIFF 等。

以下是 Mahotas 中 imread() 函式的基本語法:

mahotas.imread('image.file_format')

其中,**'image.file_format'** 是您要載入影像的實際路徑和格式。

示例

在下面的示例中,我們使用 imread() 函式從當前目錄載入名為“nature.jpeg”的影像檔案。生成的影像作為 NumPy 陣列儲存在“image”變數中:

import mahotas as mh
from pylab import imshow, show
# Loading the image using Mahotas
image = mh.imread('nature.jpeg')
# displaying the original image
imshow(image)
show()
輸出

上述程式碼的輸出如下:

Loading an Image

載入不同影像格式

影像格式指的是用於以數字方式儲存和編碼影像的不同檔案格式。每種格式都有其自身的規範、特性和壓縮方法。

Mahotas 提供各種影像格式,包括 JPEG、PNG、BMP、TIFF 和 GIF 等常用格式。我們可以將這些格式中任何一種影像的檔案路徑傳遞給 imread() 函式。

示例

在此示例中,我們透過使用 imread() 函式載入不同格式的影像來演示 Mahotas 的多功能性。每個載入的影像都儲存在單獨的變數中:

import mahotas as ms
import matplotlib.pyplot as mtplt
# Loading JPEG image
image_jpeg = ms.imread('nature.jpeg')
# Loading PNG image
image_png = ms.imread('sun.png')
# Loading BMP image
image_bmp = ms.imread('sea.bmp')
# Loading TIFF image
image_tiff = ms.imread('tree.tiff')
# Creating a figure and subplots
fig, axes = mtplt.subplots(2, 2)
# Displaying JPEG image
axes[0, 0].imshow(image_jpeg)
axes[0, 0].axis('off')
axes[0, 0].set_title('JPEG Image')
# Displaying PNG image
axes[0, 1].imshow(image_png)
axes[0, 1].axis('off')
axes[0, 1].set_title('PNG Image')
# Displaying BMP image
axes[1, 0].imshow(image_bmp)
axes[1, 0].axis('off')
axes[1, 0].set_title('BMP Image')
# Displaying TIFF image
axes[1, 1].imshow(image_tiff)
axes[1, 1].axis('off')
axes[1, 1].set_title('TIFF Image')
# Adjusting the spacing and layout
mtplt.tight_layout()
# Showing the figure
mtplt.show()

輸出

顯示的影像是:

Different Image Formats

載入彩色影像

彩色影像是我們通常看到的影像,包含各種顏色。它們由三個顏色通道組成:紅色、綠色和藍色。每個畫素的顏色由這三個通道的組合決定。彩色影像可以表示各種顏色,類似於我們用眼睛看到的內容。

在 Mahotas 中載入彩色影像意味著讀取包含顏色資訊的影像檔案。生成的影像表示為一個 3D 陣列,其中每個元素表示紅色、綠色和藍色通道中每個畫素的顏色值。

以下是 Mahotas 中載入灰度影像的基本語法:

mahotas.imread('image.file_format')

其中,**'image.file_format'** 是您要載入影像的實際路徑和格式。

示例

以下是 Mahotas 中載入彩色影像的示例:

import mahotas as ms
import matplotlib.pyplot as mtplt
# Loading colored image
colored_image = ms.imread('nature.jpeg')
# Displaying colored image
mtplt.imshow(colored_image)
mtplt.axis('off')
mtplt.show()

輸出

上述程式碼的輸出如下:

Loading Color Images

載入彩色和灰度影像

要在 mahotas 中載入灰度影像,我們需要將 'as_grey=True' 引數傳遞給 imread() 函式。

示例

在下面的示例中,我們嘗試使用 Mahotas 同時載入灰度影像和彩色影像:

import mahotas as ms
import matplotlib.pyplot as mtplt
# Loading color image
color_image = ms.imread('tree.tiff')
# Loading grayscale image
grayscale_image = ms.imread('tree.tiff', as_grey=True)
# Creating a figure and subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying colored image
axes[0].imshow(color_image)
axes[0].axis('off')
axes[0].set_title('Colored Image')
# Displaying grayscale image
axes[1].imshow(grayscale_image, cmap='gray')
axes[1].axis('off')
axes[1].set_title('Grayscaled Image')
# Adjusting the spacing and layout
mtplt.tight_layout()
# Showing the figure
mtplt.show()

輸出

輸出如下所示:

Color Grayscale image
廣告