本文实例为大家分享了c++图像处理之双边滤波的具体代码,供大家参考,具体内容如下 1、 近期在学习双边滤波相关知识,其原理如下(以后补上): 2 、灰度图双边滤波实现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))
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。
--结束END--
本文标题: C++图像处理之双边滤波
本文链接: https://lsjlt.com/news/137725.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0