Python Pillow - 邊緣增強



邊緣增強是影像處理和計算機視覺中常用的技術,用於改進影像的解釋和分析。它指的是強調或突出影像中不同物體或區域之間邊界的過程。其目標是提高邊緣的可見性,使它們更加清晰和突出。

Python Pillow 庫提供了 ImageFilter 模組,其中包含一組預定義的濾鏡,可以使用 Image.filter() 方法應用於影像。該庫的當前版本提供了兩個預定義的影像增強濾鏡,即 EDGE_ENHANCE 和 EDGE_ENHANCE_MORE,用於增強影像中的邊緣。

使用 ImageFilter.EDGE_ENHANCE 卷積核增強邊緣

此 ImageFilter.EDGE_ENHANCE 卷積核濾鏡旨在增強影像中的邊緣。

以下是增強影像邊緣的步驟:

  • 使用 Image.open() 函式載入輸入影像。

  • 將 filter() 函式應用於載入的 Image 物件,並將 ImageFilter.EDGE_ENHANCE 作為引數提供給 Image.filter() 函式,然後它將返回一個帶有增強邊緣的 PIL.Image.Image 物件。

示例

以下示例演示如何使用 ImageFilter.EDGE_ENHANCE 濾鏡卷積核來增強影像中的邊緣。

from PIL import Image, ImageFilter

# Open the input image
image = Image.open('Images/Flower1.jpg')

# Apply edge enhancement filter
enhanced_image = image.filter(ImageFilter.EDGE_ENHANCE)

# Display the original and enhanced images
image.show()
enhanced_image.show()

輸入影像

pink_flower.jpg

輸出影像

具有增強邊緣的輸出影像:

imagefilter edge enhance

使用 EDGE_ENHANCE_MORE 卷積核增強邊緣

此方法與 EDGE_ENHANCE 濾鏡類似,但它對邊緣應用更強的增強效果。

示例

以下示例演示如何使用 EDGE_ENHANCE_MORE 濾鏡卷積核更積極地增強影像中的邊緣。

from PIL import Image, ImageFilter

# Open the input image
image = Image.open('Images/Flower1.jpg')

# Apply the EDGE_ENHANCE_MORE filter to the image
enhanced_image = image.filter(ImageFilter.EDGE_ENHANCE_MORE)

# Display the original and enhanced images
image.show()
enhanced_image.show()

輸入影像

pink_flower.jpg

輸出影像

將 EDGE_ENHANCE_MORE 濾鏡應用於輸入影像後的輸出影像:

edge enhance more
廣告