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

[复制链接]
发表于 2025-11-10 15:47:13 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
  1. import cv2
  2. import numpy as np
  3. # 定义 Gabor 滤波器的参数
  4. kSize = 31  # 滤波器核的大小
  5. g_sigma = 3.0  # 高斯包络的标准差
  6. g_theta = np.pi / 4  # Gabor 函数的方向
  7. g_lambda = 10.0  # 正弦波的波长
  8. g_gamma = 0.5  # 空间纵横比
  9. g_psi = np.pi / 2  # 相位偏移
  10. # 生成 Gabor 滤波器核
  11. kernel = cv2.getGaborKernel((kSize, kSize), g_sigma, g_theta, g_lambda, g_gamma, g_psi, ktype=cv2.CV_32F)
  12. # 读取多波段图像
  13. image = cv2.imread('1.png', cv2.IMREAD_UNCHANGED)
  14. # 获取图像的波段数
  15. num_bands = image.shape[2] if len(image.shape) == 3 else 1
  16. # 初始化处理后的多波段图像
  17. filtered_image = np.zeros_like(image, dtype=np.float32)
  18. # 遍历每个波段
  19. for band in range(num_bands):
  20.     # 提取当前波段
  21.     band_image = image[:, :, band] if len(image.shape) == 3 else image
  22.     # 应用 Gabor 滤波器
  23.     filtered_band_image = cv2.filter2D(band_image, cv2.CV_32F, kernel)
  24.     # 将处理后的波段放回结果图像中
  25.     if len(image.shape) == 3:
  26.         filtered_image[:, :, band] = filtered_band_image
  27.     else:
  28.         filtered_image = filtered_band_image
  29. # 将处理后的图像转换为合适的数据类型
  30. filtered_image = np.clip(filtered_image, 0, 255).astype(np.uint8)
  31. # 显示结果
  32. if num_bands == 3:
  33.     cv2.imshow('Original Image', image)
  34.     cv2.imshow('Filtered Image', filtered_image)
  35. else:
  36.     for band in range(num_bands):
  37.         cv2.imshow(f'Original Band {band + 1}', image[:, :, band] if len(image.shape) == 3 else image)
  38.         cv2.imshow(f'Filtered Band {band + 1}', filtered_image[:, :, band] if len(image.shape) == 3 else filtered_image)
  39. # 保存结果
  40. cv2.imwrite('filtered_multiband_image.png', filtered_image)
  41. # 等待按键,然后关闭所有窗口
  42. cv2.waitKey(0)
  43. cv2.destroyAllWindows()
复制代码


回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表