资源简介

双边滤波的opencv实现,效果很好,代码自己写的,很规范,希望对大家有帮助,使用需要配置opencv2.3.1.

资源截图

代码片段和文件信息

#pragma once
#include “BilateralFilter.h“

BilateralFilter::BilateralFilter()
{
this->distance_sigma = 3.0;
this->similar_sigma = 30.0;
}

void BilateralFilter::bilateral_filter_process(IplImage* img_src IplImage* img_dest)
{
/* get properties of source image */
data_src = (uchar *)img_src->imageData;
img_width = img_src->width;
img_height = img_src->height;
step_src = img_src->widthStep;

/* get properties of destination image */
data_dest = (uchar *)img_dest->imageData;
step_dest = img_dest->widthStep;

build_distance_weight_table();
build_similar_weight_table();

for(int i=0; i {
for(int j=0; j {
data_dest[i * step_dest + j] = count_pixel(i j);
}
}
}


uchar BilateralFilter::count_pixel(int row int column)
{
double sum_pixel = 0;
double sum_weight = 0;

int center_pixel_index = row * step_src + column;

for(int i=-kernel_radius; i<=kernel_radius; i++)
{
for(int j=-kernel_radius; j<=kernel_radius; j++)
{
int temp_row = row + i;
if(temp_row < 0)
temp_row = 0;
else if(temp_row > img_height - 1)
temp_row = img_height - 1;

int temp_column = column + j;
if(temp_column < 0)
temp_column = 0;
else if(temp_column > img_width - 1)
temp_column = img_width - 1;

int temp_pixel_index = temp_row *  step_src + temp_column;

double distance_weight = distance_weight_table[i + kernel_radius][j + kernel_radius];
double similar_weight = similar_weight_table[abs(data_src[center_pixel_index] - data_src[temp_pixel_index])];
double weight = distance_weight * similar_weight;  //双边滤波
// double weight = distance_weight;   //高斯滤波

sum_pixel += data_src[temp_pixel_index] * weight;
sum_weight += weight;
}
}

sum_pixel /= sum_weight;

uchar pixel = (uchar)sum_pixel;
return pixel;
}

//建立距离高斯权重表(Weight Table)
void BilateralFilter::build_distance_weight_table()
{
for(int i=-kernel_radius; i<=kernel_radius; i++)
{
for(int j=-kernel_radius; j<=kernel_radius; j++)
{
distance_weight_table[i + kernel_radius][j + kernel_radius] = exp(-(i*i + j*j)/(2 * distance_sigma * distance_sigma));
}
}
}

//建立像素灰度差值高斯权重表(Weight Table)
void BilateralFilter::build_similar_weight_table()
{
for(int i=0; i<256; i++)
{
similar_weight_table[i] = exp((-i*i)/(2 * similar_sigma * similar_sigma));
}
}


 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件    5591040  2013-06-18 15:53  Bilateral_Filter\BilateralFilter.sdf

     文件        904  2012-12-10 20:51  Bilateral_Filter\BilateralFilter.sln

    ..A..H.     16896  2013-06-18 15:53  Bilateral_Filter\BilateralFilter.suo

     文件      82416  2012-11-07 14:52  Bilateral_Filter\opencv_test\1.png

     文件     141388  2012-11-07 14:52  Bilateral_Filter\opencv_test\2.png

     文件       2458  2012-12-18 21:06  Bilateral_Filter\opencv_test\BilateralFilter.cpp

     文件        717  2012-11-07 22:18  Bilateral_Filter\opencv_test\BilateralFilter.h

     文件     104130  2012-11-12 11:14  Bilateral_Filter\opencv_test\Debug\BilateralFilter.obj

     文件       2142  2012-11-12 11:15  Bilateral_Filter\opencv_test\Debug\cl.command.1.tlog

     文件      95272  2012-11-12 11:15  Bilateral_Filter\opencv_test\Debug\CL.read.1.tlog

     文件       1752  2012-11-12 11:15  Bilateral_Filter\opencv_test\Debug\CL.write.1.tlog

     文件          2  2012-11-12 11:15  Bilateral_Filter\opencv_test\Debug\link-cvtres.read.1.tlog

     文件          2  2012-11-12 11:15  Bilateral_Filter\opencv_test\Debug\link-cvtres.write.1.tlog

     文件          2  2012-11-12 11:15  Bilateral_Filter\opencv_test\Debug\link.2260-cvtres.read.1.tlog

     文件          2  2012-11-12 11:15  Bilateral_Filter\opencv_test\Debug\link.2260-cvtres.write.1.tlog

     文件          2  2012-11-12 11:15  Bilateral_Filter\opencv_test\Debug\link.2260.read.1.tlog

     文件          2  2012-11-12 11:15  Bilateral_Filter\opencv_test\Debug\link.2260.write.1.tlog

     文件          2  2012-11-12 11:15  Bilateral_Filter\opencv_test\Debug\link.4192-cvtres.read.1.tlog

     文件          2  2012-11-12 11:15  Bilateral_Filter\opencv_test\Debug\link.4192-cvtres.write.1.tlog

     文件          2  2012-11-12 11:15  Bilateral_Filter\opencv_test\Debug\link.4192.read.1.tlog

     文件          2  2012-11-12 11:15  Bilateral_Filter\opencv_test\Debug\link.4192.write.1.tlog

     文件          2  2012-11-12 11:15  Bilateral_Filter\opencv_test\Debug\link.5004-cvtres.read.1.tlog

     文件          2  2012-11-12 11:15  Bilateral_Filter\opencv_test\Debug\link.5004-cvtres.write.1.tlog

     文件          2  2012-11-12 11:15  Bilateral_Filter\opencv_test\Debug\link.5004.read.1.tlog

     文件          2  2012-11-12 11:15  Bilateral_Filter\opencv_test\Debug\link.5004.write.1.tlog

     文件       3926  2012-11-12 11:15  Bilateral_Filter\opencv_test\Debug\link.command.1.tlog

     文件       7848  2012-11-12 11:15  Bilateral_Filter\opencv_test\Debug\link.read.1.tlog

     文件       1880  2012-11-12 11:15  Bilateral_Filter\opencv_test\Debug\link.write.1.tlog

     文件        842  2012-11-12 11:15  Bilateral_Filter\opencv_test\Debug\mt.command.1.tlog

     文件        722  2012-11-12 11:15  Bilateral_Filter\opencv_test\Debug\mt.read.1.tlog

............此处省略27个文件信息

评论

共有 条评论