Matplotlib 简明教程

Matplotlib - Images

What are Images in Matplotlib?

在 Matplotlib 库中,显示和处理图像涉及使用 imshow() 函数。此函数可视化二维数组或图像。此函数特别适用于显示各种格式的图像,例如表示像素值或实际图像文件的数组。

In Matplotlib library displaying and manipulating images involves using the imshow() function. This function visualizes 2D arrays or images. This function is particularly useful for showing images in various formats such as arrays representing pixel values or actual image files.

Matplotlib 中的图像提供了一种可视化网格数据的办法,帮助解释和分析二维数组中表示的信息。这种功能对于处理图像数据的各种科学、工程和机器学习应用至关重要。

Images in Matplotlib provide a way to visualize gridded data, facilitating the interpretation and analysis of information represented in 2D arrays. This capability is crucial for various scientific, engineering and machine learning applications that deal with image data.

Use Cases for Images in Matplotlib

以下是在 Matplotlib 库中使用图像的用例。

The following are the use cases of Images in Matplotlib library.

Visualizing Gridded Data

matplotlib 库可用于显示科学数据,例如热图、地形图、卫星图像等。

The matplotlib library can be used for displaying scientific data such as heatmaps, terrain maps, satellite images etc.

Image Processing

在计算机视觉或图像识别等应用中分析和处理图像数据。

Analyzing and manipulating image data in applications such as computer vision or image recognition.

Artificial Intelligence and Machine Learning

在模型训练和评估中处理和处理图像数据。

Handling and processing image data in training and evaluation of models.

Loading and Displaying Images

要使用 Matplotlib 库加载和显示图像,我们可以使用以下代码行。

To load and display an image using Matplotlib library we can use the following lines of code.

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

# Load the image
img = mpimg.imread('Images/flowers.jpg')  # Load image file

# Display the image
plt.imshow(img)
plt.axis('off')  # Turn off axis labels and ticks (optional)
plt.show()
  1. matplotlib.image.imread() − Loads an image file and returns it as an array. The file path ('image_path') should be specified.

  2. plt.imshow() − Displays the image represented by the array.

  3. plt.axis('off') − Turns off axis labels and ticks, which is optional for purely displaying the image without axes.

matplotlib image

Customizing Image Display

我们可以根据需要通过下面提到的函数自定义图像。

We can customize the image as per the requirement by thebelow mentioned functions.

  1. Colormap − We can apply a colormap to enhance image visualization by specifying the cmap parameter in imshow().

  2. Colorbar − To add a colorbar indicating the intensity mapping we can use plt.colorbar() after imshow().

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

# Load the image
img = mpimg.imread('Images/flowers.jpg')  # Load image file

# Display the image
plt.imshow(img, cmap = 'Oranges')
plt.colorbar()

# Turn on axis labels and ticks (optional)
plt.axis('on')
plt.show()
matplotlib image1

Image Manipulation

我们可以使用以下功能对我们的图像进行处理。

We can perform manipulation for our images by using the below mentioned functions.

  1. Cropping − Select a specific portion of the image by slicing the array before passing it to imshow().

  2. Resizing − Use various image processing libraries such as Pillow, OpenCV to resize images before displaying them.

在本例中,我们通过使用上述函数对图像进行处理并显示图像。

In this example we are manipulating the image and displaying the image by using the above mentioned functions.

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import cv2

# Load the image
img = mpimg.imread('Images/flowers.jpg')

# Display the image with grayscale colormap and colorbar
plt.imshow(img, cmap='gray')
plt.colorbar()

# Display only a portion of the image (cropping)
plt.imshow(img[100:300, 200:400])

# Display a resized version of the image
resized_img = cv2.resize(img, (new_width, new_height))
plt.imshow(resized_img)
plt.show()
matplotlib image2

请记住,Matplotlib 的 imshow() 适用于基本图像显示和可视化。对于更高级的图像处理任务(例如,调整大小、过滤等),建议使用 OpenCV 或 Pillow 等专用图像处理库。

Remember Matplotlib’s imshow() is suitable for basic image display and visualization. For more advanced image processing tasks such as resizing, filtering, etc. using dedicated image processing libraries like OpenCV or Pillow is recommended.