Embedded_game/QR/K-means/004_good_good_拉普拉斯滤波和自定义锐化卷积核.py

50 lines
1.1 KiB
Python
Raw Permalink Normal View History

2025-01-02 12:48:11 +08:00
# -*- coding: utf-8 -*-
# @Time : 2024/10/16 上午9:57
# @Author : len
# @File : 004.py
# @Software: PyCharm
# @Comment :
import cv2
import numpy as np
import matplotlib.pyplot as plt
# 加载图片
image_path = '../data/img_2.png'
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
# 1. 使用高斯模糊来减少噪声 (可选)
blurred = cv2.GaussianBlur(image, (3, 3), 0)
# 2. 使用拉普拉斯滤波进行图像锐化
laplacian = cv2.Laplacian(blurred, cv2.CV_64F)
sharpened_laplacian = np.uint8(np.absolute(laplacian))
# 3. 使用自定义锐化卷积核进行卷积操作
# 自定义卷积核,用于锐化
kernel = np.array([[0, -1, 0],
[-1, 5,-1],
[0, -1, 0]])
sharpened_kernel = cv2.filter2D(blurred, -1, kernel)
# 显示结果
plt.figure(figsize=(10, 5))
plt.subplot(1, 3, 1)
plt.title('Original Image')
plt.imshow(image, cmap='gray')
plt.axis('off')
plt.subplot(1, 3, 2)
plt.title('Laplacian Sharpened')
plt.imshow(sharpened_laplacian, cmap='gray')
plt.axis('off')
plt.subplot(1, 3, 3)
plt.title('Kernel Sharpened')
plt.imshow(sharpened_kernel, cmap='gray')
plt.axis('off')
plt.tight_layout()
plt.show()