OpenCV——高斯滤波

x33g5p2x  于2021-10-07 转载在 其他  
字(1.2k)|赞(0)|评价(0)|浏览(363)

一、高斯滤波

高斯滤波是一种线性平滑滤波,适用于消除高斯噪声,广泛应用于图像处理的减噪过程。 [1] 通俗的讲,高斯滤波就是对整幅图像进行加权平均的过程,每一个像素点的值,都由其本身和邻域内的其他像素值经过加权平均后得到。高斯滤波的具体操作是:用一个模板(或称卷积、掩模)扫描图像中的每一个像素,用模板确定的邻域内像素的加权平均灰度值去替代模板中心像素点的值。

二、C++代码

  1. #include <opencv2\opencv.hpp>
  2. #include <iostream>
  3. using namespace cv;
  4. using namespace std;
  5. int main()
  6. {
  7. Mat img = imread("gauss_noise.png");
  8. if (img.empty())
  9. {
  10. cout << "请确认图像文件名称是否正确" << endl;
  11. return -1;
  12. }
  13. Mat result_5, result_9; //存放含噪声滤波的结果,后面数字代表滤波器尺寸
  14. //调用均值滤波函数blur()进行滤波
  15. GaussianBlur(img, result_5, Size(5, 5), 0, 0);
  16. GaussianBlur(img, result_9, Size(9, 9), 0, 0);
  17. //显示含有高斯噪声图像
  18. imshow("img_gauss", img);
  19. //显示去噪结果
  20. imshow("result_5gauss", result_5);
  21. imshow("result_9gauss", result_9);
  22. waitKey(0);
  23. return 0;
  24. }

三、python代码

  1. import cv2
  2. # ----------------------读取图片-----------------------------
  3. img = cv2.imread('gauss_noise.png')
  4. # ----------------------高斯滤波-----------------------------
  5. result_5 = cv2.GaussianBlur(img, (5, 5), 0) # 5x5
  6. result_9 = cv2.GaussianBlur(img, (9, 9), 0) # 9x9
  7. # ----------------------显示结果-----------------------------
  8. cv2.imshow('origion_pic', img)
  9. cv2.imshow('5x5_filtered_pic', result_5)
  10. cv2.imshow('9x9_filtered_pic', result_9)
  11. cv2.waitKey(0)

四、结果展示

1、原始图像

2、3x3卷积

3、9x9卷积

相关文章