Matplotlib 简明教程

Matplotlib - AGG filter

AGG 过滤器,即 "聚合过滤器",用于分类大量数据,并仅显示满足特定条件的信息。想象一下,你有很大一盒玩具,且你只想查看红色玩具。AGG 过滤器将帮助您迅速找到并从这一盒玩具中找出所有红色玩具,同时忽略其他 −

An AGG filter, which stands for "Aggregate Filter", is used to sort through large amounts of data and only show the information that meets certain conditions. Imagine you have a big box of toys, and you only want to see the red ones. The AGG filter would help you quickly find and pick out all the red toys from the box while ignoring the others −

agg filter1

AGG filter in Matplotlib

在 Matplotlib 中,AGG 过滤器或反锯齿几何过滤器用于在图形元素(如线条、标记或文本)显示在直线上之前对它们应用特定转换。AGG 过滤器允许我们修改直线上元素的外观,例如调整它们的透明度、模糊它们或应用其他视觉效果。

In Matplotlib, an AGG filter, or Anti-Grain Geometry filter, is used to apply certain transformations to graphical elements, such as lines, markers, or text, before they are displayed on a plot. The AGG filter allows us to modify the appearance of elements in a plot, such as adjusting their transparency, blurring them, or applying other visual effects.

您可以使用 "FigureCanvasAgg()" 函数在 Matplotlib 中创建 AGG 过滤器。此函数使用 AGG 过滤器创建一张画布,允许您显示具有更佳质量和清晰度的直线和图像。

You can use the "FigureCanvasAgg()" function to create an AGG filter in Matplotlib. This function creates a canvas with the AGG filter, allowing you to show plots and images with improved quality and clarity.

Image Smoothing with AGG Filter

在 Matplotlib 中,使用 AGG 过滤器进行图像平滑是一种用于模糊或轻柔图像以减少噪声的技术。AGG 过滤器是可用于图像平滑的选项之一,它使用数学算法来处理图像像素,通过对相邻像素进行平均处理来创建更平滑的外观。这个处理过程有助于消除不平整的边缘,并创建更美观的图像。

In Matplotlib, image smoothing with the AGG filter is a technique used to blur or soften an image to reduce noise. The AGG filter is one of the options available for image smoothing, and it applies a mathematical algorithm to the image pixels, averaging neighboring pixels to create a smoother appearance. This process helps to remove uneven edges and create a more visually appealing image.

Example

在以下示例中,我们使用 AGG 过滤器执行图像平滑。我们从生成一个带有噪声的随机图像开始,然后使用 gaussian_filter() 函数应用高斯平滑。之后,我们创建了一个 Matplotlib 图形,并添加了两个子直线来并排显示原始图像和经过平滑的图像 −

In the following example, we perform image smoothing using the AGG filter. We begin by generating a random noisy image and then apply Gaussian smoothing using the gaussian_filter() function. Afterward, we create a Matplotlib figure with two subplots to display the original and smoothed images side by side −

import matplotlib.pyplot as plt
from matplotlib.backends.backend_agg import FigureCanvasAgg
import numpy as np
from scipy.ndimage import gaussian_filter

# Generating random noisy image
np.random.seed(0)
image = np.random.rand(100, 100)

# Applying Gaussian smoothing to the image
smoothed_image = gaussian_filter(image, sigma=2)

# Creating a figure and plot the original and smoothed images
fig, axs = plt.subplots(1, 2, figsize=(10, 5))

axs[0].imshow(image, cmap='gray')
axs[0].set_title('Original Image')

axs[1].imshow(smoothed_image, cmap='gray')
axs[1].set_title('Smoothed Image (AGG Filter)')

# Hiding the axes
for ax in axs:
   ax.axis('off')

# Saving the figure as an image file
canvas = FigureCanvasAgg(fig)
canvas.print_png('smoothed_image.png')

plt.show()

以下是上面代码的输出: -

Following is the output of the above code −

agg filter2

Sharpening Image with AGG Filter

在 Matplotlib 中,使用 AGG 过滤器锐化图像可以通过提升边缘之间的对比度来提升图像中的清晰度和细节。它涉及使用锐化核对图像进行卷积,而锐化核通常在中心具有正值,周围为负值。

In Matplotlib, sharpening an image with the AGG filter enhances the clarity and detail in an image by increasing the contrast along edges. It involves convolving the image with a sharpening kernel, which generally has positive values at the center surrounded by negative values.

Example

在这里,我们使用 AGG 过滤器执行图像锐化。我们首先加载图像,然后应用锐化过滤器来提升图像的边缘。其中包括定义一个锐化核,并使用 scipy.ndimage.convolve() 函数对图像用其进行卷积。最终,我们显示图像 −

In here, we perform image sharpening using the AGG filter. We start by loading an image, then apply a sharpening filter to enhance the image’s edges. This involves defining a sharpening kernel and convolving it with the image using the scipy.ndimage.convolve() function. Finally we display the image −

import matplotlib.pyplot as plt
from matplotlib.backends.backend_agg import FigureCanvasAgg
import numpy as np
from scipy.ndimage import convolve

# Loading an example image
image = plt.imread('sun.jpg')

# Converting to grayscale if image has multiple channels
if len(image.shape) > 2:
   image = image.mean(axis=2)

# Defining a sharpening kernel
kernel = np.array([[-1, -1, -1],
   [-1,  9, -1],
   [-1, -1, -1]])

# Applying the kernel to the image
sharpened_image = np.clip(convolve(image, kernel, mode='constant', cval=0.0), 0, 1)

# Creating a figure and plot the original and sharpened images
fig, axs = plt.subplots(1, 2, figsize=(10, 5))

axs[0].imshow(image, cmap='gray')
axs[0].set_title('Original Image')

axs[1].imshow(sharpened_image, cmap='gray')
axs[1].set_title('Sharpened Image (AGG Filter)')

# Hiding the axes
for ax in axs:
   ax.axis('off')

# Saving the figure as an image file
canvas = FigureCanvasAgg(fig)
canvas.print_png('sharpened_image.png')

plt.show()

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

On executing the above code we will get the following output −

agg filter3

Embossing Image with AGG Filter

在 Matplotlib 中,使用 AGG 过滤器对图像进行浮雕可以强调图像中的边缘,并通过提升相邻像素之间的对比度为它赋予一个三维的外观。它通过对图像使用浮雕核来实现这一效果,而浮雕核通常包括负值和正值。浮雕图像通常具有凸起或凹陷的外观,模拟了冲压或雕刻效果。

In Matplotlib, embossing an image with the AGG filter emphasizes the edges in an image, giving it a three-dimensional appearance by enhancing the contrast between neighboring pixels. It achieves this effect by convolving the image with an embossing kernel, which generally includes negative and positive values. Embossed images often have a raised or recessed appearance, simulating the effect of stamping or carving.

Example

在下面的示例中,我们使用 AGG 过滤器对图像进行浮雕。我们首先加载一张示例图像,然后定义一个浮雕核,一个专门用于强调边缘的特定矩阵。使用卷积将这一核应用于图像将生成浮雕图像 −

In the example below, we use AGG filter to emboss an image. We start by loading an example image, then define an embossing kernel, a specific matrix designed to emphasize edges. Applying this kernel to the image using convolution generates the embossed image −

import matplotlib.pyplot as plt
from matplotlib.backends.backend_agg import FigureCanvasAgg
import numpy as np
from scipy.ndimage import convolve

# Loading an example image
image = plt.imread('sun.jpg')

# Converting to grayscale if image has multiple channels
if len(image.shape) > 2:
   image = image.mean(axis=2)

# Defining an embossing kernel
kernel = np.array([[-2, -1, 0],
   [-1,  1, 1],
   [0, 1, 2]])

# Applying the kernel to the image
embossed_image = np.clip(convolve(image, kernel, mode='constant', cval=0.0), 0, 1)

# Creating a figure and plot the original and embossed images
fig, axs = plt.subplots(1, 2, figsize=(10, 5))

axs[0].imshow(image, cmap='gray')
axs[0].set_title('Original Image')

axs[1].imshow(embossed_image, cmap='gray')
axs[1].set_title('Embossed Image (AGG Filter)')

# Hiding the axes
for ax in axs:
   ax.axis('off')

# Saving the figure as an image file
canvas = FigureCanvasAgg(fig)
canvas.print_png('embossed_image.png')

plt.show()

执行上面的代码后,我们得到以下输出: -

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

agg filter4