Mahotas - 影像處理



Mahotas是一個流行的Python包,用於處理影像並在其上執行各種操作。它可以對影像執行許多操作,例如將影像分割成不同的部分、查詢邊緣以及識別影像中的物件。

使用Mahotas,我們可以找到一組圖片中所有的人臉,或在一組影像中識別不同型別的花卉。我們還可以找到圖片中物體的邊緣,或者使用Mahotas使模糊的圖片更清晰。

在本教程中,我們將簡要概述如何使用Mahotas處理影像,包括如何讀取、寫入和顯示影像。我們還將學習如何執行常見的影像處理任務,例如濾波、分割和特徵提取。

使用Mahotas進行影像處理

Mahotas的關鍵特性之一是它能夠處理各種格式的影像,例如JPEG、PNG和BMP。Mahotas為我們提供了讀取和寫入影像以及在不同格式之間轉換影像的功能。

讓我們學習如何在Mahotas中讀取影像。

讀取影像

在Mahotas中讀取影像指的是從檔案中載入影像資料的過程。要在Mahotas中讀取影像,我們可以使用imread()函式。此函式從檔案讀取影像並返回一個表示影像的NumPy陣列。

示例

在下面的程式碼中,我們嘗試讀取一個名為“nature.jpeg”的JPEG影像 -

import mahotas as ms
image = ms.imread('nature.jpeg')
print ("The image is read.")

輸出

這將從“nature.jpeg”檔案讀取影像並將其儲存在變數“image”中 -

The image is read.

我們還可以像下面討論的那樣,在讀取影像後顯示它。

顯示影像

讀取影像後,我們可以使用matplotlib庫來顯示它。matplotlib庫用於資料視覺化和繪圖。

示例

讓我們使用matplotlib庫顯示“nature.jpeg”影像,如下所示 -

import matplotlib.pyplot as plt
import mahotas as ms
image = ms.imread('nature.jpeg')
plt.imshow(image)
plt.show()

輸出

以下是執行上述程式碼後獲得的影像 -

Displaying an Image

寫入影像

在Mahotas中寫入影像指的是將影像資料儲存到特定影像格式(例如PNG、JPEG、BMP、TIFF等)的檔案中。我們可以使用imsave()函式在Mahotas中寫入影像。此函式是Mahotas中影像輸入/輸出(IO)模組的一部分。

示例

在下面的程式碼中,我們嘗試儲存一個名為“nature.jpeg”的影像 -

import mahotas as ms
image = ms.imread('nature.jpeg')
print ("The image is read.")
# Write the image to a file
ms.imsave('writing.jpeg', image)
print ("The image data is saved.")

輸出

這將影像儲存到“writing.jpeg”檔案中 -

The image is read.
The image data is saved.

使用Mahotas進行影像處理

影像處理指的是一組用於對影像執行多個操作的技術。這些技術有助於提高影像的視覺質量、提取有用的資訊或準備影像以進行分析。

一些常見的影像處理任務包括濾波、分割和特徵提取。在本節中,我們將探討Mahotas提供的一些關鍵影像處理函式。

濾波

在Mahotas中,濾波指的是修改影像的外觀或從中提取有用的資訊。這是透過對影像應用數學運算來完成的。濾波過程有助於去除噪聲、平滑影像、增強邊緣或執行其他有助於提高影像質量或可解釋性的操作。

示例

在下面的示例中,我們嘗試使用高斯濾波器平滑影像。高斯濾波器會模糊影像,這可以用來減少噪聲或平滑影像 -

import mahotas as ms
import numpy as np
import matplotlib.pyplot as mtplt
# Loading an image and converting it to grayscale
image = ms.imread('nature.jpeg', as_grey=True)
# Applying a Gaussian filter
# Standard deviation
sigma = 15
gaussian_filtered = ms.gaussian_filter(image, sigma)
# Displaying the original image
fig, axes = mtplt.subplots(1, 2, figsize=(9, 4))
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].axis('off')
# Displaying the filtered image
axes[1].imshow(gaussian_filtered, cmap='gray')
axes[1].set_title('Gaussian Filtered')
axes[1].axis('off')
mtplt.show()
輸出

這將使用標準差為15的高斯濾波器平滑原始影像,如下所示 -

Image Filtering

分割

在Mahotas中,分割指的是根據某些特徵或標準將影像劃分為有意義的區域或分割的過程。這些分割可以表示不同的物件、感興趣區域或影像內的不同區域。

示例

現在,讓我們透過使用Mahotas進行閾值化的基本影像分割示例。閾值化用於根據畫素強度值將物件與背景分離。它透過將影像轉換為二值影像來簡化影像,其中每個畫素都被分類為前景(物件)或背景 -

import mahotas as ms
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = ms.imread('nature.jpeg', as_grey=False)
# Performing thresholding
# Calculating threshold value by taking mean of image
threshold = np.mean(image)
# creating binary image
# comparing each pixel value with the threshold
binary_image = image > threshold
# Perform connected component analysis
# assigns a unique label to each connected region in the binary image
labeled_image, num_objects = ms.label(binary_image)
# Displaying the original image
fig, ax = mtplt.subplots(1, 2, figsize=(9, 4))
ax[0].imshow(image, cmap='gray')
ax[0].set_title('Original Image')
# Displaying the segmented image
ax[1].imshow(labeled_image, cmap='rainbow')
ax[1].set_title('Segmented Image ({} objects)'.format(num_objects))
mtplt.show()
輸出

上述程式碼的輸出如下所示 -

Original Segmentation Image

特徵提取

在Mahotas中,特徵提取指的是從影像中提取有意義和資訊豐富的特徵的過程。這些特徵可以表示影像的各個方面,例如紋理、形狀或顏色,並且可以用來描述和區分影像中的物件或區域。

示例

現在,讓我們看看如何使用Mahotas計算Zernike矩的示例。Zernike矩是一組描述物件或影像內區域形狀的數值。它們透過捕獲物件的輪廓和空間特徵來提供物件形狀的緊湊表示 -

import mahotas as ms
from mahotas.features import zernike_moments
# Loading the image
image = ms.imread('nature.jpeg', as_grey=True)
# Normalizing the image
normalized_image = ms.stretch(image)
# Specifying the radius for Zernike moments calculation
radius = 30
# Calculating Zernike moments
moments = zernike_moments(normalized_image, radius)
# Printing the resulting moments
print(moments)
輸出

執行上述程式碼後,我們得到一組描述影像形狀特徵的數值,如下所示 -

[0.31830989 0.01252728 0.00854837 0.00677556 0.00239216 0.00742469
0.00553204 0.00306795 0.01058855 0.00368774 0.00456233 0.01123103
0.00336877 0.00349998 0.00387494 0.0108442 0.00294575 0.00490895
0.00243368 0.01113736 0.00229705 0.00511743 0.00668574 0.0083512
0.00767699]
廣告