Mahotas 简明教程

Mahotas - Displaying Shape of an Image

在处理图像数据时,有时我们需要显示图像的形状。

显示图像的形状是指揭示图像的尺寸和特征,例如其宽度、高度和颜色通道;其中高度对应于行数,宽度对应于列数,通道表示图像中的颜色通道数(例如,RGB 图像为 3)。

Displaying the Shape of an Image in Mahotas

在 mahotas 中,我们可以使用 NumPy 数组(它表示图像)的 shape 属性来显示图像的形状。通过访问此属性,我们可以获取图像的尺寸并根据其形状确定要执行的适当操作。

让我们讨论 Mahotas 提供的用于提取和可视化形状信息的各个步骤和函数,以及实际示例。

Step1: Importing and Loading Images

首先,我们需要导入 Mahotas 库并加载我们要分析的图像。安装 Mahotas 后,我们可以开始进行图像形状分析。

Step 2: Displaying the shape of an image

要在 mahotas 中显示图像的形状,我们可以使用 NumPy 数组的 shape 属性。

shape 属性返回一个表示数组维度的整数元组。对于图像,它将提供有关其宽度、高度和通道的信息。

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

这将以 (高度,宽度,通道) 的格式打印加载的图像的形状。

Step 3: Extracting Individual Dimensions

当我们讨论在 Mahotas 中提取形状的单个维度时,我们指的是获取有关图像尺寸和颜色分量的具体信息。

用更简单的术语来说,图像具有诸如其高度、宽度和颜色通道数量(如红色、绿色和蓝色)的不同属性。提取单个维度意味着隔离和单独获取这些具体信息片段。

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

通过执行此代码,我们将使用索引访问图像的维度,其中,

  1. 第一个索引对应于高度,

  2. 第二个索引对应于宽度

  3. 第三个索引对应于通道数。

这将给出图像的各个维度。

Step 4: Checking for Grayscale Images

灰度图像为黑白图像,其中每个像素表示该特定点的强度或亮度。它没有任何颜色信息。可以将其视为黑白照片。

有时,我们会遇到灰度图像,它们只有单个通道,而不是彩色图像通常使用的三个通道(红色、绿色和蓝色)。要确定一个图像是否是灰度,我们可以检查通道数是否等于 1。

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

通过执行此代码,您将找出加载的图像是否是灰度的。根据结果,我们可以进行适当的分析。

Step 5: Displaying the Shape Information on the Image

现在,让我们探讨如何在图像本身上显示形状信息。我们可以绘制形状或添加文本叠层以突出显示特定的形状特征。这在使用带注释的形状信息呈现或保存图像时很有用。

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()

当我们执行此代码时,它将显示覆盖了形状信息的图像。形状信息将定位在指定坐标上,并且文本将以白色显示在黑色边界框上,使其在图像上更显着。

Complete Example

现在,让我们看看包含上面讨论的所有步骤的完整代码 −

# 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