SciPy - ascent() 方法



SciPy ascent() 方法用於獲取 8 位灰度影像派生影像(512*512),用於演示。此方法允許使用者嘗試影像處理技術並應用多種濾波器和變換。

語法

以下是 SciPy ascent() 方法的語法 -

ascent()

引數

此方法不接受任何引數。

返回值

此方法返回一個 2D NumPy 陣列,顯示灰度影像。

示例 1

以下是 SciPy ascent() 方法,顯示預先存在的 ascent 影像。

import matplotlib.pyplot as plt
from scipy.misc import ascent

# load the ascent image
image = ascent()

# display the image using matplotlib
plt.imshow(image, cmap='gray')
plt.title('Ascent Image')
plt.axis('off')
plt.show()

輸出

以上程式碼生成以下輸出 -

scipy_ascent_method_one

示例 2

下面的示例區分了原始 ascent 影像和高斯濾波後的影像。

import matplotlib.pyplot as plt
from scipy.ndimage import gaussian_filter
from scipy.misc import ascent

# load the ascent image
image = ascent()

# apply a Gaussian filter to the image
filtered_image = gaussian_filter(image, sigma=3)

# Display the original and filtered images
fig, axes = plt.subplots(1, 2, figsize=(12, 6))
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].axis('off')

axes[1].imshow(filtered_image, cmap='gray')
axes[1].set_title('Gaussian Filtered Image')
axes[1].axis('off')

plt.show()

輸出

以上程式碼生成以下輸出 -

scipy_ascent_method_two

示例 3

此方法說明了如何使用 Sobel 濾波器對原始 ascent 影像執行邊緣檢測。該濾波器分別在 x 和 y 軸上進行應用,結果作為邊緣檢測影像與原始影像一起生成。

import matplotlib.pyplot as plt
from scipy.ndimage import sobel
from scipy.misc import ascent

# load the ascent image
image = ascent()

# apply the Sobel filter to detect edges
sobel_x = sobel(image, axis=0)
sobel_y = sobel(image, axis=1)
edges = sobel_x + sobel_y

# Display the original image and the edge-detected image
fig, axes = plt.subplots(1, 2, figsize=(12, 6))
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].axis('off')

axes[1].imshow(edges, cmap='gray')
axes[1].set_title('Edge Detection using Sobel Filter')
axes[1].axis('off')

plt.show()

輸出

以上程式碼生成以下輸出 -

scipy_ascent_method_three
scipy_reference.htm
廣告
© . All rights reserved.