Mahotas 简明教程

Mahotas - Displaying an Image

在加载图像并对其执行各种操作后,你需要显示最终输出图像以查看操作的结果。

Once you have loaded an image and performed various operations on it, you would need to display the final output image to view the results of your operations.

显示图像是指将图像数据以视觉方式呈现给屏幕上的用户或观众。

Displaying an image refers to visually presenting the image data to a user or viewer on a screen.

Displaying an Image in Mahotas

我们可以使用 mahotas.plotting 模块中的 imshow()show() 函数在 Mahotas 中显示图像。它允许我们在 Python 环境中显示图像。此函数在内部使用 Matplotlib 库来渲染图像。

We can display an image in Mahotas using the imshow() and show() functions from the mahotas.plotting module. It allows us to display images within a Python environment. This function internally uses the Matplotlib library to render the image.

让我们简单讨论一下 imshow() 和 show() 函数。

Let us discuss about the imshow() and show() function briefly.

Using the imshow() Function

imshow() 函数用于在单独的窗口中显示图像。它会创建一个新窗口并在其中呈现图像。此函数提供各种选项用于自定义显示,例如调整窗口大小、颜色图和颜色范围。

The imshow() function is used to display an image in a separate window. It creates a new window and renders the image within it. This function provides various options to customize the display, such as adjusting the window size, colormap, and color range.

以下是 imshow() 函数的基本语法 −

Following is the basic syntax of imshow() function −

imshow(image)

其中, 'image' 是我们想要显示的图片。

Where, 'image' is the picture we want to display.

Using the show() Function

show() 函数用于显示当前的图像或图片。它属于 matplotlib 库的 pylab 模块,而 Mahotas 用于绘制图片和可视化。

The show() function is used to display the current figure or image. It is a part of the matplotlib library from pylab module, which Mahotas uses for plotting and visualization.

当您希望在同一窗口中显示多个图像或绘图时,此函数尤其有用。

This function is particularly useful when you want to display multiple images or plots in the same window.

以下是 show() 函数的基本语法 −

Following is the basic syntax of show() function −

show()

在以下示例中,我们正在使用 imshow() 和 show() 函数从当前目录中显示一个名为“nature.jpeg”的图像文件 −

In the following example, we are using the imshow() and show() functions to display an image file named "nature.jpeg" from the current directory −

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

上述代码的输出如下:

Output of the above code is as follows −

displaying show image

Displaying Multiple Images

Mahotas 还可以允许我们同时显示多个图像。当我们想要并排比较或可视化不同的图像时,此功能非常有用。Mahotas 提供多种图像格式,包括常见格式,如 JPEG、PNG、BMP、TIFF 和 GIF。因此,我们可以使用不同的格式显示每个图像。

Mahotas also allows us to display multiple images simultaneously. This is useful when we want to compare or visualize different images side by side. Mahotas provides a wide range of image formats, including common formats like JPEG, PNG, BMP, TIFF, and GIF. Hence, we can display each image in different formats.

图像格式指用于存储和编码图像的数字文件格式。每种格式都有自己的规范、特征和压缩方法。

The image formats refer to the different file formats used to store and encode images digitally. Each format has its own specifications, characteristics, and compression methods.

Example

在此示例中,我们通过使用 imshow() 和 show() 函数以不同格式显示图像,来演示 Mahotas 的多功能性。每个加载的图像都存储在一个单独的变量中 −

In this example, we demonstrate the versatility of Mahotas by displaying images in different formats using imshow() and show() functions. Each loaded image is stored in a separate variable −

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

显示的图像如下 −

The image displayed is as follows −

displaying multiple images

Customizing Image Display

当我们讨论在 Mahotas 中自定义图像显示时,我们指的是修改图像在屏幕上或在绘图中呈现方式的各个方面的能力。这些自定义设置使我们能够增强图像的可视表示,并向查看者提供附加信息。

When we talk about customizing image display in Mahotas, we are referring to the ability to modify various aspects of how the image is presented on the screen or in a plot. These customizations allow us to enhance the visual representation of the image and provide additional information to the viewer.

Mahotas 提供了几种用于自定义图像显示的选项。例如,我们可以调整颜色图、添加标题并修改显示大小。

Mahotas provides several options for customizing the image display. For instance, we can adjust the colormap, add titles, and modify the display size.

让我们逐个讨论自定义图像显示的每个选项。

Let us discuss each options for customizing the image display one by one.

Applying Colormap

Mahotas 支持不同的颜色映射,这些映射可以应用于灰度或单通道图像,以增强其对比度和视觉效果。颜色映射决定了像素值如何映射到颜色。

Mahotas supports different color maps that can be applied to a grayscale or single−channel images to enhance their contrasts and visual appearance. Color maps determine how pixel values are mapped to colors.

例如,“灰色”颜色映射通常用于灰度图像,而“暖色”或“喷射”颜色映射可用于强调图像中的强度变化。通过选择合适的颜色映射。

For example, the 'gray' color map is commonly used for grayscale images, while 'hot' or 'jet' color maps can be used to emphasize intensity variations in an image. By choosing an appropriate color map.

若要更改用于显示图像的颜色映射,我们可以将 cmap 参数传递给 imshow() 函数。cmap 参数接受一个字符串,该字符串表示所需颜色映射的名称。

To change the colormap used for displaying an image, we can pass the cmap argument to the imshow() function. The cmap argument accepts a string representing the name of the desired colormap.

在下面的示例中,我们向 imshow() 函数传递 cmap 参数,并将其参数设置为“灰色”,以使用灰度色彩映射来显示灰度图像 −

In the example below, we are passing the cmap argument to the imshow() function and setting it argument to 'gray' to display the grayscale image using a grayscale colormap −

import mahotas as ms
import matplotlib.pyplot as mtplt
# Loading grayscale image
grayscale_image = ms.imread('nature.jpeg', as_grey=True)
# Displaying grayscale image
mtplt.imshow(grayscale_image, cmap='gray')
mtplt.axis('off')
mtplt.show()

执行以上代码后,我们得到如下所示的输出 −

After executing the above code, we get the output as shown below −

applying colormap

Adding a Title

我们还可以使用传递给 imshow() 函数的 title 参数,为我们的图像添加一个标题或说明。

We can also add a title or a caption to our image using the title argument passed to the imshow() function.

在下面的代码中,我们为显示的图像添加了标题“Nature Image” −

In the code below, we are adding the title Nature Image' to the displayed image −

import mahotas as ms
import matplotlib.pyplot as mtplt
# Loading the image
image = ms.imread('nature.jpeg')
# Displaying the image
mtplt.imshow(image)
mtplt.axis('off')
mtplt.title('Nature Image')
mtplt.show()

上述代码的输出如下:

Output of the above code is as follows −

adding a title

Figure Size

在 mahotas 中的图形大小指的是图像将显示在其上的图形或图像显示区域的大小。

Figure size in mahotas refers to the size of the plot or image display area where the image will be shown.

我们还可以通过指定 imshow() 函数的 figsize 参数来控制显示的图像的大小。figsize 参数期望一个表示图形宽度和高度(以英寸为单位)的元组。

We can also control the size of the displayed image by specifying the figsize argument of the imshow() function. The figsize argument expects a tuple representing the width and height of the figure in inches.

在下面的示例中,我们将图形大小设置为 (8, 6) 英寸 −

In the example below, we are setting the figure size to (8, 6) inches −

import mahotas as ms
import matplotlib.pyplot as mtplt
# Loading the image
image = ms.imread('nature.jpeg')
# Set the figure size using Matplotlib
# Specify the width and height of the figure in inches
fig = mtplt.figure(figsize=(8, 6))
# Displaying the image
mtplt.imshow(image)
mtplt.show()

上述代码的输出如下:

Output of the above code is as follows −

figure size