Mahotas 简明教程

Mahotas - Displaying Shape of an Image

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

When working with image data, there are scenarios when we need to display the shape of an image.

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

Displaying the shape of an image refers to revealing the dimensions and characteristics of the image, such as its width, height, and color channels; where height corresponds to the number of rows, width corresponds to the number of columns, and channels indicate the number of color channels in the image (e.g., 3 for RGB images).

Displaying the Shape of an Image in Mahotas

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

In mahotas, we can display the shape of an image using the shape attribute of a NumPy array, which represents the image. By accessing this attribute, we can obtain the dimensions of the image and determine the appropriate operations to perform based on its shape.

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

Let us discuss different steps and functions offered by Mahotas to extract and visualize the shape information, along with practical examples.

Step1: Importing and Loading Images

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

To begin, first we need to import the Mahotas library and load the image we want to analyze. Once Mahotas is installed, we can start working with image shape analysis.

Step 2: Displaying the shape of an image

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

To display the shape of an image in mahotas, we can use the shape attribute of a NumPy array.

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

The shape attribute returns a tuple of integers representing the dimensions of the array. In the case of an image, it will provide information about its width, height, and channels.

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

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

This will print the shape of the loaded image in the format (height, width, channels).

Step 3: Extracting Individual Dimensions

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

When we talk about extracting individual dimensions of the shape in Mahotas, we are referring to obtaining specific information about the size and color components of an image.

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

In simpler terms, an image has different properties like its height, width, and the number of color channels (such as red, green, and blue). Extracting individual dimensions means isolating and getting these specific pieces of information separately.

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

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

By executing this code, we will access the dimensions of the image using indexing where,

  1. The first index corresponds to the height,

  2. The second index corresponds to the width

  3. The third index corresponds to the number of channels.

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

This will give the individual dimensions of the image.

Step 4: Checking for Grayscale Images

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

The Grayscale images are black and white images, where each pixel represents the intensity or brightness of that particular point. It does not have any color information. Think of it as a black and white photograph.

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

Sometimes, we encounter grayscale images, which have only one channel instead of the usual three channels for color images (Red, Green, and Blue). To determine if an image is grayscale, we can check if the number of channels is equal to 1.

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

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

By executing this code, you will find out whether the loaded image is grayscale or not. Based on the result, we can proceed with the appropriate analysis.

Step 5: Displaying the Shape Information on the Image

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

Now, let’s explore how to display shape information on the image itself. We can draw shapes or add text overlays to highlight specific shape characteristics. This can be useful when presenting or saving the image with annotated shape information.

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

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

When we will execute this code, it will display the image with the shape information overlaid on it. The shape information will be positioned at the specified coordinates, and the text will be displayed in white color on a black bounding box, making it more visible on the image.

Complete Example

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

Now, let us look at the complete code that encompasses all the steps discussed above −

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

在执行上述代码后,我们得到以下输出 −

After executing the above code, we get the following output−

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