Mahotas - 顯示影像形狀



在處理影像資料時,有些情況下我們需要顯示影像的形狀。

顯示影像的形狀是指揭示影像的維度和特徵,例如影像的寬度、高度和顏色通道;其中高度對應於行數,寬度對應於列數,通道表示影像中顏色通道的數量(例如,RGB 影像為 3)。

在 Mahotas 中顯示影像形狀

在 mahotas 中,我們可以使用表示影像的 NumPy 陣列的 shape 屬性來顯示影像的形狀。透過訪問此屬性,我們可以獲得影像的維度,並根據其形狀確定要執行的操作。

讓我們討論 Mahotas 提供的不同步驟和函式來提取和視覺化形狀資訊,以及一些實際示例。

步驟 1:匯入和載入影像

首先,我們需要匯入 Mahotas 庫並載入我們要分析的影像。Mahotas 安裝完成後,我們就可以開始進行影像形狀分析了。

步驟 2:顯示影像的形狀

要在 mahotas 中顯示影像的形狀,我們可以使用 NumPy 陣列的 shape 屬性。

shape 屬性返回一個表示陣列維度的整數元組。對於影像,它將提供有關其寬度、高度和通道的資訊。

image_shape = image.shape
print("Image Shape:", image_shape)

這將以 (高度,寬度,通道) 的格式列印載入影像的形狀。

步驟 3:提取單個維度

當我們談論在 Mahotas 中提取形狀的單個維度時,指的是獲取有關影像大小和顏色分量的特定資訊。

簡單來說,影像具有不同的屬性,例如高度、寬度和顏色通道數(例如紅色、綠色和藍色)。提取單個維度意味著分別隔離和獲取這些特定的資訊片段。

height = image_shape[0]
width = image_shape[1]
channels = image_shape[2]
print("Height:", height)
print("Width:", width)
print("Channels:", channels)

透過執行此程式碼,我們將使用索引訪問影像的維度,其中:

  • 第一個索引對應於高度,

  • 第二個索引對應於寬度,

  • 第三個索引對應於通道數。

這將給出影像的單個維度。

步驟 4:檢查灰度影像

灰度影像是黑白影像,其中每個畫素表示該特定點的強度或亮度。它沒有任何顏色資訊。可以把它想象成黑白照片。

有時,我們會遇到灰度影像,它們只有一個通道,而不是彩色影像通常的三個通道(紅色、綠色和藍色)。要確定影像是否是灰度影像,我們可以檢查通道數是否等於 1。

is_grayscale = channels == 1
if is_grayscale:
   print("The image is grayscale.")
else:
   print("The image is not grayscale.")

透過執行此程式碼,您可以確定載入的影像是否為灰度影像。根據結果,我們可以繼續進行相應的分析。

步驟 5:在影像上顯示形狀資訊

現在,讓我們探索如何在影像本身顯示形狀資訊。我們可以繪製形狀或新增文字疊加來突出顯示特定的形狀特徵。這在以帶註釋的形狀資訊顯示或儲存影像時非常有用。

import matplotlib.pyplot as plt
# Create a figure and axes
fig, ax = plt.subplots()
# Display the image
ax.imshow(image)
# Add text for shape information
ax.text(10, 20, f"Shape: {image_shape}", color='white', fontsize=10,
bbox=dict(facecolor='black'))
# Remove axis ticks
ax.set_xticks([])
ax.set_yticks([])
# Show the figure
plt.show()

當我們執行此程式碼時,它將顯示影像,並在其上疊加形狀資訊。形狀資訊將位於指定座標處,文字將以白色顯示在黑色邊框上,使其在影像上更易於檢視。

完整示例

現在,讓我們看看包含上面討論的所有步驟的完整程式碼 -

# Installing the library
import mahotas as ms
import matplotlib.pyplot as mtplt
# Loading the image
image = ms.imread('sun.png')
# Displaying the shape of an image
image_shape = image.shape
print("Image Shape:", image_shape)
# Extracting individual dimensions
height = image_shape[0]
width = image_shape[1]
channels = image_shape[2]
print("Height:", height)
print("Width:", width)
print("Channels:", channels)
# Checking if the image is grayscale
is_grayscale = channels == 1
if is_grayscale:
   print("The image is grayscale.")
else:
   print("The image is not grayscale.")
# Create a figure and axis
fig, ax = mtplt.subplots()
# Display the image
ax.imshow(image)
# Add text for shape information
ax.text(350, 200, f"Shape: {image_shape}", color='white', fontsize=8,
bbox=dict(facecolor='green'))
# Remove axis ticks
ax.set_xticks([])
ax.set_yticks([])
# Display the image
ax.imshow(image)
# Add text overlay for dimensions
text = f"Dimensions: {width}x{height}x{channels}" if not is_grayscale else
f"Dimensions: {width}x{height} (Grayscale)"
ax.text(18, 100, text, color='red', fontsize=12, fontweight='bold')
# Remove axis ticks and labels
ax.axis('off')
# Show the image with shape information
mtplt.show()

輸出

執行上述程式碼後,我們將獲得以下輸出 -

Image Shape: (1280, 843, 3)
Height: 1280
Width: 843
Channels: 3
The image is not grayscale.
Displaying shape image
廣告