Chinaunix首页 | 论坛 | 博客
  • 博客访问: 279478
  • 博文数量: 109
  • 博客积分: 2116
  • 博客等级: 大尉
  • 技术积分: 1062
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-22 15:38
文章分类

全部博文(109)

文章存档

2013年(2)

2011年(16)

2010年(90)

2009年(1)

我的朋友

分类:

2010-07-30 09:23:23

//参数说明:
//LPSTR lpDIBBits    - 指向源DIB图像指针
//LONG  lWidth       - 源图像宽度(象素数,必须是4的倍数)
//LONG  lHeight      - 源图像高度(象素数)
/
//函数说明:
//该函数用来对BlurDIB()生成的DIB图像进行复原操作。
BOOL WINAPI RestoreDIB (LPSTR lpDIBBits, LONG lWidth, LONG lHeight)
{
 // 指向源图像的指针
 LPSTR lpSrc;
 //循环变量
 long i;
 long j;
 //像素值
 unsigned char pixel;
 // 图像每行的字节数
 LONG lLineBytes;
 //用于做FFT的数组
 double *fftSrc,*fftKernel;
 double a,b,c,d;
 //二维FFT的长度和宽度
 unsigned long nn[3];
 //图像归一化因子
 double MaxNum;
 // 计算图像每行的字节数
 lLineBytes = WIDTHBYTES(lWidth * 8);
 double dPower = log((double)lLineBytes)/log(2.0);
 if(dPower != (int) dPower)
 {
  return false;
 }
 dPower = log((double)lHeight)/log(2.0);
 if(dPower != (int) dPower)
 {
  return false;
 }
 fftSrc = new double [lHeight*lLineBytes*2+1];
 fftKernel = new double [lHeight*lLineBytes*2+1];
 nn[1] = lHeight;
 nn[2] = lLineBytes;
 for (j = 0;j < lHeight ;j++)
 {
  for(i = 0;i < lLineBytes ;i++)
  {
   // 指向源图像倒数第j行,第i个象素的指针   
   lpSrc = (char *)lpDIBBits + lLineBytes * j + i;
   
   pixel = (unsigned char)*lpSrc;
   
   fftSrc[(2*lLineBytes)*j + 2*i + 1] = (double)pixel;
   fftSrc[(2*lLineBytes)*j + 2*i + 2] = 0.0;
   
   if(i < 5 && j == 0)
   {
    fftKernel[(2*lLineBytes)*j + 2*i + 1] = 1/5.0;
   }
   else
   {
    fftKernel[(2*lLineBytes)*j + 2*i + 1] = 0.0;
   }
   fftKernel[(2*lLineBytes)*j + 2*i + 2] = 0.0;
  }
 }
 
 //对源图像进行FFT
 fourn(fftSrc,nn,2,1);
 //对卷积核图像进行FFT
 fourn(fftKernel,nn,2,1);
 for (j = 0;j < lHeight ;j++)
 {
  for(i = 0;i < lLineBytes ;i++)
  {
   a = fftSrc[(2*lLineBytes)*j + 2*i + 1];
   b = fftSrc[(2*lLineBytes)*j + 2*i + 2];
   c = fftKernel[(2*lLineBytes)*j + 2*i + 1];
   d = fftKernel[(2*lLineBytes)*j + 2*i + 2];
   if (c*c + d*d > 1e-3)
   {
    fftSrc[(2*lLineBytes)*j + 2*i + 1] = ( a*c + b*d ) / ( c*c + d*d );
    fftSrc[(2*lLineBytes)*j + 2*i + 2] = ( b*c - a*d ) / ( c*c + d*d );
   }
  }
 }
 //对结果图像进行反FFT
 fourn(fftSrc,nn,2,-1);
 
 //确定归一化因子
 MaxNum = 0.0;
 for (j = 0;j < lHeight ;j++)
 {
  for(i = 0;i < lLineBytes ;i++)
  {
   fftSrc[(2*lLineBytes)*j + 2*i + 1] =
    sqrt(fftSrc[(2*lLineBytes)*j + 2*i + 1] * fftSrc[(2*lLineBytes)*j + 2*i + 1]\
    +fftSrc[(2*lLineBytes)*j + 2*i + 2] * fftSrc[(2*lLineBytes)*j + 2*i + 2]);
   if( MaxNum < fftSrc[(2*lLineBytes)*j + 2*i + 1])
    MaxNum = fftSrc[(2*lLineBytes)*j + 2*i + 1];
  }
 }
 
 //转换为图像
 for (j = 0;j < lHeight ;j++)
 {
  for(i = 0;i < lLineBytes ;i++)
  {
   // 指向源图像倒数第j行,第i个象素的指针   
   lpSrc = (char *)lpDIBBits + lLineBytes * j + i;
   
   *lpSrc = (unsigned char) (fftSrc[(2*lLineBytes)*j + 2*i + 1]*255.0/MaxNum);
  }
 }
 delete fftSrc;
 delete fftKernel;
 // 返回
 return true;
}
阅读(453) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~