Python Pillow - 銳化蒙版濾鏡



銳化蒙版是一種廣泛應用於影像處理中的影像銳化技術。銳化蒙版的基本概念是使用影像的負像的軟化或不銳化版本來建立蒙版。然後,將此銳化蒙版與原始正像合併,從而得到原始影像的較少模糊的版本,使其更清晰。

Python pillow(PIL) 庫在其 ImageFilter 模組中提供了一個名為 UnsharpMask() 的類,用於將銳化蒙版濾鏡應用於影像。

銳化蒙版濾鏡

ImageFilter.UnsharpMask() 類表示銳化蒙版濾鏡,用於增強影像銳度。

以下是 ImageFilter.UnsharpMask() 類的語法:

class PIL.ImageFilter.UnsharpMask(radius=2, percent=150, threshold=3)

其中,

  • radius - 該引數控制模糊半徑。半徑影響要增強的邊緣的大小。較小的半徑會銳化較小的細節,而較大的半徑會在物體周圍產生光暈。調整半徑和強度會相互影響;減小一個允許另一個產生更大的影響。

  • percent - 它以百分比的形式確定銳化蒙版的強度。百分比控制銳化效果的強度或幅度。它決定在物體邊緣新增多少對比度。較高的強度會導致更明顯的銳化效果,使邊緣邊界更暗和更亮。它不會影響邊緣邊框的寬度。

  • threshold - 閾值控制需要銳化的亮度變化的最小值。它決定了相鄰色調值需要有多大的差異才能使濾鏡生效。較高的閾值可以防止平滑區域出現斑點。較低的值會銳化更多,而較高的值會保留對比度較低的區域。

示例

以下示例使用 ImageFilter.UnsharpMask() 類和 Image.filter() 方法(使用預設值)將銳化蒙版濾鏡應用於影像。

from PIL import Image, ImageFilter

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

# Apply the Unsharp Mask filter with default parameter values
sharpened_image = original_image.filter(ImageFilter.UnsharpMask())

# Display the original image
original_image.show()

# Display the sharpened image
sharpened_image.show()

輸出

輸入影像:

zinnia flower

使用預設引數值應用銳化蒙版濾鏡後的輸出影像:

imagefilter unsharpmask

示例

這是另一個示例,它使用不同的引數值將銳化蒙版濾鏡應用於影像。

from PIL import Image, ImageFilter

# Open the image using Pillow
original_image = Image.open('Images/Flower1.jpg')

# Apply the Unsharp Mask filter with custom parameters
sharpened_image = original_image.filter(ImageFilter.UnsharpMask(radius=4, percent=200, threshold=3))

# Display the original image
original_image.show()

# Display the sharpened image
sharpened_image.show()

輸出

輸入影像:

zinnia flower

使用自定義引數值應用銳化蒙版濾鏡後的輸出影像:

unsharpmask
廣告