3DGabor滤波器实现人脸特征提取

python 复制代码
import cv2
import numpy as np

# 定义 Gabor 滤波器的参数
kSize = 31  # 滤波器核的大小
g_sigma = 3.0  # 高斯包络的标准差
g_theta = np.pi / 4  # Gabor 函数的方向
g_lambda = 10.0  # 正弦波的波长
g_gamma = 0.5  # 空间纵横比
g_psi = np.pi / 2  # 相位偏移

# 生成 Gabor 滤波器核
kernel = cv2.getGaborKernel((kSize, kSize), g_sigma, g_theta, g_lambda, g_gamma, g_psi, ktype=cv2.CV_32F)

# 读取多波段图像
image = cv2.imread('1.png', cv2.IMREAD_UNCHANGED)

# 获取图像的波段数
num_bands = image.shape[2] if len(image.shape) == 3 else 1

# 初始化处理后的多波段图像
filtered_image = np.zeros_like(image, dtype=np.float32)

# 遍历每个波段
for band in range(num_bands):
    # 提取当前波段
    band_image = image[:, :, band] if len(image.shape) == 3 else image

    # 应用 Gabor 滤波器
    filtered_band_image = cv2.filter2D(band_image, cv2.CV_32F, kernel)

    # 将处理后的波段放回结果图像中
    if len(image.shape) == 3:
        filtered_image[:, :, band] = filtered_band_image
    else:
        filtered_image = filtered_band_image

# 将处理后的图像转换为合适的数据类型
filtered_image = np.clip(filtered_image, 0, 255).astype(np.uint8)

# 显示结果
if num_bands == 3:
    cv2.imshow('Original Image', image)
    cv2.imshow('Filtered Image', filtered_image)
else:
    for band in range(num_bands):
        cv2.imshow(f'Original Band {band + 1}', image[:, :, band] if len(image.shape) == 3 else image)
        cv2.imshow(f'Filtered Band {band + 1}', filtered_image[:, :, band] if len(image.shape) == 3 else filtered_image)

# 保存结果
cv2.imwrite('filtered_multiband_image.png', filtered_image)

# 等待按键,然后关闭所有窗口
cv2.waitKey(0)
cv2.destroyAllWindows()


相关推荐
云天徽上9 天前
【目标检测】图像处理基础:像素、分辨率与图像格式解析
图像处理·人工智能·目标检测·计算机视觉·数据可视化
Vertira9 天前
PyTorch中的permute, transpose, view, reshape和flatten函数详解(已解决)
人工智能·pytorch·python
heimeiyingwang9 天前
【深度学习加速探秘】Winograd 卷积算法:让计算效率 “飞” 起来
人工智能·深度学习·算法
lsd&xql9 天前
AI大模型(四)openAI应用实战
人工智能
飞哥数智坊9 天前
AI编程实战:使用Cursor,65分钟轻松打造番茄时钟应用
人工智能
匿名的魔术师9 天前
实验问题记录:PyTorch Tensor 也会出现 a = b 赋值后,修改 a 会影响 b 的情况
人工智能·pytorch·python
Ven%9 天前
PyTorch 张量(Tensors)全面指南:从基础到实战
人工智能·pytorch·python
说私域9 天前
云零售新中枢:定制化“开源AI智能名片+S2B2C商城小程序”驱动的沉浸式触点进化论
人工智能·小程序·开源·零售
明似水9 天前
ChatGPT:人工智能对话革命的里程碑与未来展望
人工智能·chatgpt
小白菜3336669 天前
DAY 37 早停策略和模型权重的保存
人工智能·深度学习·算法