用Python的Pillow庫(PIL)做銳化操作

簡單看一下銳化的定義:

  • A sharpening filter makes the transition between the various regions present in an image more obvious rather than being smooth.
  • As an image passes through a sharpening filter the brighter pixels are boosted as relative to its neighbors.

總的來說,圖像銳化(image sharpening)是補償圖像的輪廓,增強圖像的邊緣及灰度跳變的部分,使圖像變得清晰的過程。——某度百科


接下來我們用PythonPIL來進行銳化操作:

from PIL import Image
from PIL import ImageFilter

# 打開一張圖片
imageObject = Image.open("./Einstein.jpg")             <------1】
imageObject.show()                                     <------2# 使用 sharp filter
sharpened1 = imageObject.filter(ImageFilter.SHARPEN)   <------3】
sharpened2 = sharpened1.filter(ImageFilter.SHARPEN)    <------4# 展示銳化後的圖片
sharpened1.show()
sharpened2.show()

【1】:PIL.Image.open函數用於創建圖像對象,參數傳入圖片的路徑即可
【2】:圖片對象.show()用於顯示圖片,運行一下,你就懂了
【3】:調用圖片對象.filter()方法即可進行濾波操作,參數僅傳入filter的類名即可
【4】:ImageFilter.SHARPEN創建filter對象,該對象有能用來銳化的矩陣:

-2  -2  -2
-2  32  -2
-2  -2  -2

filter()方法使用該矩陣進行濾波操作,返回銳化後的矩陣

這是原圖 Einstein 的帥照:
在這裏插入圖片描述

銳化一次的照片
在這裏插入圖片描述

銳化兩次的照片:
在這裏插入圖片描述
銳化完畢,是不是邊界清晰多了??

對比一下:

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章