空间滤波
-
- 空域滤波基础
– 离散卷积的边缘效应
- 空域滤波基础
-
- 平滑空间滤波器
- 平滑空间滤波器
# -*- coding: utf-8 -*-
# Author: Huazhong Yang
# Email: cjdxyhz@163.com
# @Time : 2024/3/7 20:26
import cv2
import numpy as np
# 读取图像
image = cv2.imread('a1.png')
# 应用高斯滤波
# 第二个参数是高斯核的大小,(0, 0) 让OpenCV自动选择,但它必须是正数和奇数。
# 第三个参数是X方向的标准差,第四个参数是Y方向的标准差。
# 如果只指定X方向的标准差,Y方向也会使用相同的值。如果两者都是0,那么它们将从内核大小计算得出。
gaussian_blurred = cv2.GaussianBlur(image, (1, 1), 0)
# 保存结果
cv2.imwrite('gaussian_blurred.jpg', gaussian_blurred)
# 显示原始和处理后的图像
cv2.imshow('Original', image)
cv2.imshow('Gaussian Blurred', gaussian_blurred)
cv2.waitKey(0)
cv2.destroyAllWindows()
- 阈值处理
# -*- coding: utf-8 -*-
# Author: Huazhong Yang
# Email: cjdxyhz@163.com
# @Time : 2024/3/8 10:10
import cv2
import numpy as np
# 读取图像
image = cv2.imread('a2.png')
# 应用高斯滤波
gaussian_blurred = cv2.GaussianBlur(image, (3, 3), 0)
# 将图像转换为灰度图
gray_image = cv2.cvtColor(gaussian_blurred, cv2.COLOR_BGR2GRAY)
print(gray_image)
# 将图像强度缩放到[0,1]范围
normalized_image = gray_image / 255.0
# 应用阈值处理
# 设置阈值
threshold = 0.6 # 这个阈值是在[0, 1]范围内
_, thresholded_image = cv2.threshold(normalized_image, threshold, 5, cv2.THRESH_BINARY)
# 显示结果
# 注意: 为了使用cv2.imshow显示归一化的图像,我们需要将其转换回[0, 255]范围并转换为uint8
display_image = np.uint8(thresholded_image * 255)
cv2.imshow('Thresholded Image', display_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 如果需要,可以保存结果图像
cv2.imwrite('thresholded_image.jpg', display_image)
# -*- coding: utf-8 -*-
# Author: Huazhong Yang
# Email: cjdxyhz@163.com
# @Time : 2024/3/8 10:23
import cv2
import numpy as np
def add_salt_and_pepper_noise(image, salt_pepper_ratio=0.5, amount=0.02):
"""
向图像添加盐和胡椒噪声
:param image: 原始图像
:param salt_pepper_ratio: 盐与胡椒的比例
:param amount: 噪声比例
:return: 添加噪声后的图像
"""
noisy_image = np.copy(image)
num_salt = np.ceil(amount * image.size * salt_pepper_ratio)
num_pepper = np.ceil(amount * image.size * (1.0 - salt_pepper_ratio))
# 添加盐噪声
coords = [np.random.randint(0, i - 1, int(num_salt))
for i in image.shape]
noisy_image[coords[0], coords[1]] = 1
# 添加胡椒噪声
coords = [np.random.randint(0, i - 1, int(num_pepper))
for i in image.shape]
noisy_image[coords[0], coords[1]] = 0
return noisy_image
def gaussian_lowpass_filter(image, kernel_size=(5, 5), sigma=3):
"""
使用高斯低通滤波器进行噪声降低
:param image: 输入图像
:param kernel_size: 高斯核的大小
:param sigma: 高斯核的标准差
:return: 滤波后的图像
"""
return cv2.GaussianBlur(image, kernel_size, sigma)
def median_filter(image, kernel_size=7):
"""
使用中值滤波器进行噪声降低
:param image: 输入图像
:param kernel_size: 核的大小
:return: 滤波后的图像
"""
return cv2.medianBlur(image, kernel_size)
# 读取图像
original_image = cv2.imread('img_1.png', cv2.IMREAD_GRAYSCALE)
# 向图像添加盐和胡椒噪声
noisy_image = add_salt_and_pepper_noise(original_image)
# 使用高斯滤波器降噪
gaussian_filtered_image = gaussian_lowpass_filter(noisy_image)
# 使用中值滤波器降噪
median_filtered_image = median_filter(noisy_image)
# 显示结果
cv2.imshow('Original Image', original_image)
cv2.imshow('Noisy Image', noisy_image)
cv2.imshow('Gaussian Filtered Image', gaussian_filtered_image)
cv2.imshow('Median Filtered Image', median_filtered_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
-
- 锐化空间滤波器
- 锐化空间滤波器
import cv2
import numpy as np
# 读取原始图像
original_image = cv2.imread('path_to_your_image.jpg')
# 确保图像大小为 600x259
original_image = cv2.resize(original_image, (600, 259))
# (b) 使用 31x31 的高斯低通滤波器应用高斯模糊,σ = 5
blurred_image = cv2.GaussianBlur(original_image, (31, 31), 5)
# (c) 计算掩码:原始图像 - 模糊图像
mask = cv2.subtract(original_image, blurred_image)
# (d) 锐化掩蔽:原始图像 + 掩码 (k=1)
unsharp_masking = cv2.add(original_image, mask)
# (e) 高提升滤波:原始图像 + k * 掩码 (k=4.5)
k = 4.5
highboost_filtering = cv2.add(original_image, (mask * k).astype(np.uint8))
# 显示结果或保存结果
cv2.imshow('Original Image', original_image)
cv2.imshow('Blurred Image', blurred_image)
cv2.imshow('Mask', mask)
cv2.imshow('Unsharp Masking', unsharp_masking)
cv2.imshow('Highboost Filtering', highboost_filtering)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 如果需要保存图片
# cv2.imwrite('blurred_image.jpg', blurred_image)
# cv2.imwrite('mask.jpg', mask)
# cv2.imwrite('unsharp_masking.jpg', unsharp_masking)
# cv2.imwrite('highboost_filtering.jpg', highboost_filtering)
import cv2
import numpy as np
# 读取图像
image = cv2.imread('your_image_path.jpg')
# 转换为灰度图
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 计算X方向上的Sobel梯度
grad_x = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3)
# 计算Y方向上的Sobel梯度
grad_y = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=3)
# 计算合成梯度的绝对值
abs_grad_x = cv2.convertScaleAbs(grad_x)
abs_grad_y = cv2.convertScaleAbs(grad_y)
# 合成最终的梯度
gradient = cv2.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0)
# 显示结果
cv2.imshow('Original', image)
cv2.imshow('Gradient', gradient)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 保存结果图像
cv2.imwrite('edge_enhancement.jpg', gradient)
# -*- coding: utf-8 -*-
# Author: Huazhong Yang
# Email: cjdxyhz@163.com
# @Time : 2024/3/8 11:12
import cv2
import numpy as np
def apply_dft(image_path):
# 读取图像并转换为灰度图
img = cv2.imread(image_path, 0)
dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)
# 获取图像中心
rows, cols = img.shape
crow, ccol = rows // 2, cols // 2
# (a) 实现低通滤波器
mask_low = np.zeros((rows, cols, 2), np.uint8)
mask_low[crow-30:crow+30, ccol-30:ccol+30] = 1
# (b) 实现高通滤波器
mask_high = np.ones((rows, cols, 2), np.uint8)
mask_high[crow-30:crow+30, ccol-30:ccol+30] = 0
# (c) 实现带阻滤波器
mask_bandreject = np.ones((rows, cols, 2), np.uint8)
mask_bandreject[crow-60:crow-30, ccol-60:ccol-30] = 0
mask_bandreject[crow+30:crow+60, ccol+30:ccol+60] = 0
# (d) 实现带通滤波器
mask_bandpass = np.zeros((rows, cols, 2), np.uint8)
mask_bandpass[crow-60:crow-30, ccol-60:ccol-30] = 1
mask_bandpass[crow+30:crow+60, ccol+30:ccol+60] = 1
# 应用滤波器
fshift_low = dft_shift * mask_low
fshift_high = dft_shift * mask_high
fshift_bandreject = dft_shift * mask_bandreject
fshift_bandpass = dft_shift * mask_bandpass
# 逆DFT
img_back_low = cv2.idft(np.fft.ifftshift(fshift_low))
img_back_low = cv2.magnitude(img_back_low[:, :, 0], img_back_low[:, :, 1])
img_back_high = cv2.idft(np.fft.ifftshift(fshift_high))
img_back_high = cv2.magnitude(img_back_high[:, :, 0], img_back_high[:, :, 1])
img_back_bandreject = cv2.idft(np.fft.ifftshift(fshift_bandreject))
img_back_bandreject = cv2.magnitude(img_back_bandreject[:, :, 0], img_back_bandreject[:, :, 1])
img_back_bandpass = cv2.idft(np.fft.ifftshift(fshift_bandpass))
img_back_bandpass = cv2.magnitude(img_back_bandpass[:, :, 0], img_back_bandpass[:, :, 1])
# 结果
cv2.imshow("Original", img)
cv2.imshow("Low Pass", img_back_low)
cv2.imshow("High Pass", img_back_high)
cv2.imshow("Band Reject", img_back_bandreject)
cv2.imshow("Band Pass", img_back_bandpass)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 应用DFT和滤波器
apply_dft('a1.png')