资源简介

双边滤波 纯C++实现,支持8位灰度图像和24位真彩图像

资源截图

代码片段和文件信息


#include
#include 
#include “stdafx.h“
#include “Bilateral.h“

bool ImageFilter(unsigned char* imageint Widthint Heightint BandNumunsigned char* outimage)
{

int rgbWidth = Width;
int rgbHeight = Height;
int rgbBandnum = BandNum;


int ijk;                                   //定义循环变量
unsigned char* pRgbImg = nullptr; //用于存储原始图像数据
//开辟原始图像数据内存空间,输入为灰度图像,缺省设为1个颜色通道
pRgbImg = new unsigned char[rgbBandnum * rgbHeight * rgbWidth];     

//读取图像原始数据,并存储到定义的变量中
long lImgLength = rgbWidth * rgbHeight;

for (int k = 0; k < rgbBandnum; k++)
{
for (j = 0; j < rgbHeight; j++)
{
for (i = 0; i < rgbWidth; i++)
{
pRgbImg[(i + j * rgbWidth) + k * lImgLength] = image[i + j * rgbWidth + k * lImgLength];
}
}
}



//2.第二步:进行高斯滤波算法设计
//2.1步:定义高斯滤波相关变量
unsigned char *pImgFilter=NULL;    //存储高斯滤波后的数据
pImgFilter=new unsigned char[rgbBandnum * rgbWidth*rgbHeight];
// memset(pImgFilter0sizeof(unsigned char)*rgbWidth*rgbHeight);
//2016.9.19 9:00 zs更改,为完全消除边界黑边,先给输出图像赋值为输入图像
for (int k= 0; k < rgbBandnum; k++)
{
for(int i = 0;i < rgbHeight;++i)
{
for(int j = 0; j < rgbWidth;++j)
{
pImgFilter[i * rgbWidth + j + k * lImgLength] = image[i * rgbWidth + j + k * lImgLength];
}
}
}

int nFilterWidth = 9;   //滤波器宽度
int nFilterHeight = 9;  //滤波器高度
double dSigmaGuass = 3;      //滤波参数方差
unsigned char *pArray = new unsigned char[nFilterWidth*nFilterHeight];  //保存窗口内的像素灰度值
double *pdKernal = new double[nFilterWidth*nFilterHeight]; //高斯核数组
int nCentre_x = nFilterWidth/2;
int nCentre_y = nFilterHeight/2;
double dSum = 0.0; //求和,用于权系数归一化

//得到高斯滤波器权系数
for (i = 0; i < nFilterHeight; i++)
{
for (j = 0; j < nFilterWidth; j++)
{
int nDis_x = i-nCentre_x;
int nDis_y = j-nCentre_y;   
//pdKernal[j+i*nFilterWidth] = exp(-(1/2)*(nDis_x*nDis_x+nDis_y*nDis_y)/(dSigma*dSigma))
pdKernal[j+i*nFilterWidth] = exp(-(1./2.)*(nDis_x*nDis_x+nDis_y*nDis_y)/(dSigmaGuass*dSigmaGuass));//zs9.24 11:30改
//高斯公式的表示
dSum = dSum + pdKernal[j+i*nFilterWidth];
}
}
//权系数归一化
for (i = 0; i < nFilterHeight; i++)
{
for (j = 0; j < nFilterWidth; j++)
{

pdKernal[j+i*nFilterWidth] = pdKernal[j+i*nFilterWidth]/dSum;
// std::cout< }
// std::cout< }

// double* dPixelKernal = new double[nFilterWidth*nFilterHeight];//值域核
double* dFinalKernal = new double[nFilterWidth*nFilterHeight];//最终的核函数

int nFilter_x=nFilterWidth/2;
int nFilter_y=nFilterHeight/2;                            //用于索引窗口内的数值


for (int k = 0; k < rgbBandnum; k++)
{
for (i = nFilter_y; i < rgbHeight-nFilter_y; i++)  //一般i对应高度,j对应宽度,先是i不变,j增加
{
for (j = nFilter_x; j < rgbWidth-nFilter_x; j++)
{
GetFinalKernal(pRgbImgnFilterWidthnFilterHeightpdKernaldFinalKernalijkrgbWidthlImgLength);
GetWindowArray(ijkpRgbImgpArraynFilterHeightnFilterWidthrgbWidthlIm

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2016-11-02 21:15  BilateralFilter\
     目录           0  2016-11-02 21:13  BilateralFilter\BilateralFilter\
     文件    77791232  2016-11-02 21:15  BilateralFilter\BilateralFilter.sdf
     文件         912  2016-11-01 15:53  BilateralFilter\BilateralFilter.sln
     文件       25600  2016-11-02 21:15  BilateralFilter\BilateralFilter.v11.suo
     文件     5832054  2016-11-02 21:09  BilateralFilter\BilateralFilter\Bilateral.bmp
     文件        6675  2016-11-02 21:15  BilateralFilter\BilateralFilter\Bilateral.cpp
     文件         507  2016-11-02 20:03  BilateralFilter\BilateralFilter\Bilateral.h
     文件        3034  2016-11-02 21:15  BilateralFilter\BilateralFilter\BilateralFilter.cpp
     文件          39  2016-11-01 15:53  BilateralFilter\BilateralFilter\BilateralFilter.h
     文件        2652  2016-11-01 15:53  BilateralFilter\BilateralFilter\BilateralFilter.rc
     文件        5117  2016-11-01 16:41  BilateralFilter\BilateralFilter\BilateralFilter.vcxproj
     文件        1834  2016-11-01 16:36  BilateralFilter\BilateralFilter\BilateralFilter.vcxproj.filters
     文件         164  2016-11-01 16:39  BilateralFilter\BilateralFilter\BilateralFilter.vcxproj.user
     目录           0  2016-11-02 21:13  BilateralFilter\BilateralFilter\Debug\
     文件      105243  2016-11-02 21:13  BilateralFilter\BilateralFilter\Debug\Bilateral.obj
     文件        2888  2016-11-02 21:13  BilateralFilter\BilateralFilter\Debug\BilateralFilter.Build.CppClean.log
     文件          80  2016-11-02 21:13  BilateralFilter\BilateralFilter\Debug\BilateralFilter.lastbuildstate
     文件        2807  2016-11-02 21:13  BilateralFilter\BilateralFilter\Debug\BilateralFilter.log
     文件      108809  2016-11-02 21:13  BilateralFilter\BilateralFilter\Debug\BilateralFilter.obj
     文件    34078720  2016-11-02 21:13  BilateralFilter\BilateralFilter\Debug\BilateralFilter.pch
     文件         128  2016-11-02 21:13  BilateralFilter\BilateralFilter\Debug\BilateralFilter.res
     文件        2606  2016-11-02 21:13  BilateralFilter\BilateralFilter\Debug\cl.command.1.tlog
     文件       38376  2016-11-02 21:13  BilateralFilter\BilateralFilter\Debug\CL.read.1.tlog
     文件        1976  2016-11-02 21:13  BilateralFilter\BilateralFilter\Debug\CL.write.1.tlog
     文件           2  2016-11-02 21:13  BilateralFilter\BilateralFilter\Debug\link-cvtres.read.1.tlog
     文件           2  2016-11-02 21:13  BilateralFilter\BilateralFilter\Debug\link-cvtres.write.1.tlog
     文件           2  2016-11-02 21:13  BilateralFilter\BilateralFilter\Debug\link-mt.read.1.tlog
     文件           2  2016-11-02 21:13  BilateralFilter\BilateralFilter\Debug\link-mt.write.1.tlog
     文件           2  2016-11-02 21:13  BilateralFilter\BilateralFilter\Debug\link-rc.read.1.tlog
     文件           2  2016-11-02 21:13  BilateralFilter\BilateralFilter\Debug\link-rc.write.1.tlog
............此处省略55个文件信息

评论

共有 条评论