
- Python Pillow 教程
- Python Pillow - 首頁
- Python Pillow - 概述
- Python Pillow - 環境設定
- 基本影像操作
- Python Pillow - 影像處理
- Python Pillow - 調整影像大小
- Python Pillow - 翻轉和旋轉影像
- Python Pillow - 裁剪影像
- Python Pillow - 為影像新增邊框
- Python Pillow - 識別影像檔案
- Python Pillow - 合併影像
- Python Pillow - 剪下和貼上影像
- Python Pillow - 滾動影像
- Python Pillow - 在影像上新增文字
- Python Pillow - ImageDraw 模組
- Python Pillow - 合併兩張影像
- Python Pillow - 建立縮圖
- Python Pillow - 建立水印
- Python Pillow - 影像序列
- Python Pillow 顏色轉換
- Python Pillow - 影像上的顏色
- Python Pillow - 建立彩色影像
- Python Pillow - 將顏色字串轉換為 RGB 顏色值
- Python Pillow - 將顏色字串轉換為灰度值
- Python Pillow - 透過更改畫素值來更改顏色
- 影像處理
- Python Pillow - 降噪
- Python Pillow - 更改影像模式
- Python Pillow - 影像合成
- Python Pillow - 使用 Alpha 通道
- Python Pillow - 應用透視變換
- 影像濾鏡
- Python Pillow - 為影像新增濾鏡
- Python Pillow - 卷積濾鏡
- Python Pillow - 模糊影像
- Python Pillow - 邊緣檢測
- Python Pillow - 浮雕影像
- Python Pillow - 增強邊緣
- Python Pillow - 銳化蒙版濾鏡
- 影像增強和校正
- Python Pillow - 增強對比度
- Python Pillow - 增強銳度
- Python Pillow - 增強色彩
- Python Pillow - 校正色彩平衡
- Python Pillow - 去噪
- 影像分析
- Python Pillow - 提取影像元資料
- Python Pillow - 識別顏色
- 高階主題
- Python Pillow - 建立動畫 GIF
- Python Pillow - 批次處理影像
- Python Pillow - 轉換影像檔案格式
- Python Pillow - 為影像新增填充
- Python Pillow - 顏色反轉
- Python Pillow - 使用 NumPy 進行機器學習
- Python Pillow 與 Tkinter BitmapImage 和 PhotoImage 物件
- Image 模組
- Python Pillow - 影像混合
- Python Pillow 有用資源
- Python Pillow - 快速指南
- Python Pillow - 函式參考
- Python Pillow - 有用資源
- Python Pillow - 討論
Python Pillow - 卷積濾鏡
在影像處理的上下文中,卷積涉及將一個小矩陣(稱為卷積核)的值應用於影像。此過程會產生各種濾鏡效果,例如模糊、銳化、浮雕和邊緣檢測。核心中的每個值代表一個權重或係數。此核心應用於影像中相應畫素的鄰域,以產生輸出影像中相應位置的輸出畫素值。
Python 的 Pillow 庫在其 ImageFilter 模組中提供了一個名為“kernel”的特定類。此類用於建立大小超出傳統 5x5 矩陣的卷積核。
建立卷積核
要建立卷積核,您可以使用 ImageFilter 模組中的 Kernel() 類。
需要注意的是,當前版本的 Pillow 支援 3×3 和 5×5 整數和浮點核心。這些核心僅適用於“L”和“RGB”模式的影像。
以下是此 ImageFilter.Kernel() 類的語法:
class PIL.ImageFilter.Kernel(size, kernel, scale=None, offset=0)
以下是類引數的詳細資訊:
size - 表示核心大小,指定為 (寬度,高度)。在當前版本中,有效大小為 (3,3) 或 (5,5)。
kernel - 包含核心權重的序列。核心在應用於影像之前會垂直翻轉。
scale - 表示比例因子。如果提供,則每個畫素的結果將除以該值。預設值為核心權重的總和。
offset - 表示偏移值。如果提供,則在除以比例因子後將此值新增到結果中。
示例
此示例演示如何使用 Image.filter() 方法將卷積核濾鏡應用於影像。
from PIL import Image, ImageFilter # Create an image object original_image = Image.open('Images/Car_2.jpg') # Apply the Kernel filter filtered_image = original_image.filter(ImageFilter.Kernel((3, 3), (0, -1, 0, -1, 5, -1, 0, -1, 0))) # Display the original image original_image.show() # Display the filtered image filtered_image.show()
輸入影像

輸出
卷積核濾鏡的輸出:

示例
這是一個將 5x5 浮雕卷積核濾鏡應用於影像的示例。
from PIL import Image, ImageFilter # Create an image object original_image = Image.open('Images/Car_2.jpg') # Define a 5x5 convolution kernel kernel_5x5 = [-2, 0, -1, 0, 0, 0, -2, -1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 1, 2, 0, 0, 0, 1, 0, 2] # Apply the 5x5 convolution kernel filter filtered_image = original_image.filter(ImageFilter.Kernel((5, 5), kernel_5x5, 1, 0)) # Display the original image original_image.show() # Display the filtered image filtered_image.show()
輸入影像

輸出影像
大小為 5X5 的卷積核濾鏡的輸出:

廣告