Matplotlib - AGG 濾鏡



AGG 濾鏡,即“聚合濾鏡”,用於篩選大量資料,僅顯示滿足特定條件的資訊。想象一下,你有一個大盒子裝著玩具,你只想看到紅色的玩具。AGG 濾鏡可以幫助你快速找到並挑選出盒子裡的所有紅色玩具,而忽略其他的玩具。

AGG filter

Matplotlib 中的 AGG 濾鏡

在 Matplotlib 中,AGG 濾鏡,或抗鋸齒幾何濾鏡,用於在圖形元素(如線條、標記或文字)顯示在繪圖上之前對其應用某些變換。AGG 濾鏡允許我們修改繪圖中元素的外觀,例如調整其透明度、模糊它們或應用其他視覺效果。

可以使用 "FigureCanvasAgg()" 函式在 Matplotlib 中建立 AGG 濾鏡。此函式建立一個帶有 AGG 濾鏡的畫布,允許你顯示具有改進的質量和清晰度的繪圖和影像。

使用 AGG 濾鏡進行影像平滑

在 Matplotlib 中,使用 AGG 濾鏡進行影像平滑是一種用於模糊或軟化影像以減少噪聲的技術。AGG 濾鏡是用於影像平滑的可用選項之一,它將數學演算法應用於影像畫素,對相鄰畫素進行平均以建立更平滑的外觀。此過程有助於去除不均勻的邊緣並建立更具視覺吸引力的影像。

示例

在下面的示例中,我們使用 AGG 濾鏡進行影像平滑。我們首先生成一個隨機噪聲影像,然後使用 gaussian_filter() 函式應用高斯平滑。之後,我們建立一個 Matplotlib 圖形,其中包含兩個子圖,用於並排顯示原始影像和平滑後的影像。

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

輸出

以下是上述程式碼的輸出:

Image Smoothing with AGG Filter

使用 AGG 濾鏡銳化影像

在 Matplotlib 中,使用 AGG 濾鏡銳化影像透過增加邊緣的對比度來增強影像的清晰度和細節。它涉及使用銳化核卷積影像,銳化核通常在中心具有正值,周圍環繞著負值。

示例

在這裡,我們使用 AGG 濾鏡進行影像銳化。我們首先載入影像,然後應用銳化濾鏡以增強影像的邊緣。這涉及定義銳化核並使用 scipy.ndimage.convolve() 函式將其與影像進行卷積。最後,我們顯示影像。

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

輸出

執行上述程式碼後,我們將得到以下輸出:

Sharpening Image with AGG Filter

使用 AGG 濾鏡壓印影像

在 Matplotlib 中,使用 AGG 濾鏡壓印影像透過增強相鄰畫素之間的對比度來突出影像中的邊緣,從而使其具有三維外觀。它透過使用壓印核卷積影像來實現此效果,壓印核通常包含負值和正值。壓印影像通常具有凸起或凹陷的外觀,模擬衝壓或雕刻的效果。

示例

在下面的示例中,我們使用 AGG 濾鏡來壓印影像。我們首先載入一個示例影像,然後定義一個壓印核,這是一個旨在突出邊緣的特定矩陣。使用卷積將此核應用於影像會生成壓印影像。

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

輸出

執行上述程式碼後,我們將得到以下輸出:

Embossing Image with AGG Filter
廣告