C 语言

关注公众号 jb51net

关闭
首页 > 软件编程 > C 语言 > C++双边滤波

C++图像处理之双边滤波

作者:Asimov_Liu

这篇文章主要为大家详细介绍了C++图像处理之双边滤波,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C++图像处理之双边滤波的具体代码,供大家参考,具体内容如下

1、 近期在学习双边滤波相关知识,其原理如下(以后补上):

2 、灰度图双边滤波实现C++代码如下,网上大多数是基于8位灰度图和彩色图像的。(此次代码未经优化,可去除opencv依赖):

//灰度图双边滤波
void m_bilateralFilter(cv::Mat src,cv::Mat& dst,int radius,float sigma_r,float sigma_d)
{
 if (src.empty())
  return;
 if (dst.empty())
 {
  dst = src.clone();
 }
 if (src.depth() == CV_16U){
  for (int i = radius; i < src.rows - radius; i++)
   for (int j = radius; j < src.cols - radius; j++)
   {
    float sum_1 = .0f, sum_2 = .0f;
    for (int k = 0; k < 2 * radius - 1; k++)
     for (int l = 0; l < 2 * radius - 1; l++)
     {
      int dis_x = radius - k;
      int dis_y = radius - l;
      int coord_x_image = i - radius + k;
      int coord_y_image = j - radius + l;
      float dis_spatial = dis_x*dis_x + dis_y*dis_y;
      float dis_range = (src.at<unsigned short>(i, j)
       - src.at<unsigned short>(coord_x_image, coord_y_image))*(src.at<unsigned short>(i, j)
       - src.at<unsigned short>(coord_x_image, coord_y_image));
      float c_tmp = exp(-dis_spatial /
       (2 * sigma_d * sigma_d));
      float s_tmp = exp(-dis_range /
       (2 * sigma_r * sigma_r));
      sum_1 += c_tmp*s_tmp*src.at<unsigned short>(coord_x_image, coord_y_image);
      sum_2 += c_tmp*s_tmp;
 
     }
    dst.at<unsigned short>(i, j) = sum_1 / sum_2;
   }
 }
 else if (src.depth() == CV_8U)
 {
  for (int i = radius; i < src.rows - radius; i++)
   for (int j = radius; j < src.cols - radius; j++)
   {
    float sum_1 = .0f, sum_2 = .0f;
    for (int k = 0; k < 2 * radius - 1; k++)
     for (int l = 0; l < 2 * radius - 1; l++)
     {
      int dis_x = radius - k;
      int dis_y = radius - l;
      int coord_x_image = i - radius + k;
      int coord_y_image = j - radius + l;
      float dis_spatial = dis_x*dis_x + dis_y*dis_y;
      float dis_range = (src.at<unsigned char>(i, j)
       - src.at<unsigned char>(coord_x_image, coord_y_image))*(src.at<unsigned char>(i, j)
       - src.at<unsigned char>(coord_x_image, coord_y_image));
      float c_tmp = exp(-dis_spatial /
       (2 * sigma_d * sigma_d));
      float s_tmp = exp(-dis_range /
       (2 * sigma_r * sigma_r));
      sum_1 += c_tmp*s_tmp*src.at<unsigned char>(coord_x_image, coord_y_image);
      sum_2 += c_tmp*s_tmp;
 
     }
    dst.at<unsigned char>(i, j) = sum_1 / sum_2;
   }
 }
}

3、目前是基于单通道图像,效果如下:

原图:

opencv 库的效果(cv::bilateralFilter(img_src, img_dst, 10,10 * 2, 10 / 2))

该程序的效果(m_bilateralFilter(img_src, img_dst, 5, 10 * 2, 10 / 2))

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文