//code written by Kay(zhang_xiaoli@163.com) 2011/03/17
BOOL WienerFilter(WORD *pData, int iWid, int iHgh, int ihoodW, int ihoodH)
{
if (pData == NULL)
{
return FALSE;
}
int iSize = iWid*iHgh;
float *pKernel=new float[ihoodW*ihoodH];
for ( int i = 0 ; i < ihoodW*ihoodH ; i ++ ) { pKernel[i] = (float)1/(ihoodW*ihoodH);}
float *pMean=new float[iSize];
float *pVal =new float[iSize];
for ( i = 0 ; i < iSize ; i ++ )
{ pMean[i] = (float)(pData[i]*pData[i]);}
Filter2(pVal,pMean,iWid,iHgh,pKernel,ihoodW,ihoodH);
Filter2(pMean,pData,iWid,iHgh,pKernel,ihoodW,ihoodH);
for ( i = 0 ; i < iSize ; i ++ )
{ pVal[i] = pVal[i]-(pMean[i]*pMean[i]);}
//temp for mean
double dMean=0;
for ( i = 0 ; i < iSize ; i ++ )
{
dMean+=(double)pVal[i]/iHgh;
}
dMean=dMean/iWid;
for ( i = 0 ; i < iSize ; i ++ )
{
pVal[i]=(pVal[i]-(float)dMean)>0?(pVal[i]-(float)dMean):0;
float ftemp = (pData[i]-pMean[i])*pVal[i]/(pVal[i]+(float)dMean)+pMean[i];
if (ftemp<0) {ftemp = 0; }
if (ftemp > (GRAYLEVEL-1)) {ftemp = GRAYLEVEL-1;}
pData[i] = (WORD)ftemp;
}
delete pKernel;
delete pMean;
delete pVal;
return TRUE;
}
void Filter2(float *pRes,WORD *pData, int iWid, int iHgh, float *pKernel, int iKWid, int iKHgh)
{
float ftemp=0;
for ( int j = 0 ; j < iHgh ; j ++ )
{
for ( int i = 0 ; i < iWid ; i ++ )
{
ftemp=0;
for ( int x = 0 ; x < iKWid ; x ++ )
{
for ( int y = 0 ; y < iKHgh ; y ++ )
{
int ix=(x-iKWid/2+i)<0?0:(x-iKWid/2+i);
int iy=(y-iKHgh/2+j)<0?0:(y-iKHgh/2+j);
if ((ix+1)>iWid) {ix=iWid-1;}
if ((iy+1)>iHgh) {iy=iHgh-1;}
ftemp+=pData[ix+iy*iWid]*pKernel[x+y*iKWid];
}
}
pRes[j*iWid+i] = ftemp;
}
}
}
void Filter2(float *pRes,float *pData, int iWid, int iHgh, float *pKernel, int iKWid, int iKHgh)
{
float ftemp=0;
for ( int j = 0 ; j < iHgh ; j ++ )
{
for ( int i = 0 ; i < iWid ; i ++ )
{
ftemp=0;
for ( int x = 0 ; x < iKWid ; x ++ )
{
for ( int y = 0 ; y < iKHgh ; y ++ )
{
int ix=(x-iKWid/2+i)<0?0:(x-iKWid/2+i);
int iy=(y-iKHgh/2+j)<0?0:(y-iKHgh/2+j);
if ((ix+1)>iWid) {ix=iWid-1;}
if ((iy+1)>iHgh) {iy=iHgh-1;}
ftemp+=pData[ix+iy*iWid]*pKernel[x+y*iKWid];
}
}
pRes[j*iWid+i] = ftemp;
}
}
}
Related posts:


最新评论
博客不错 加油
学习LZ好榜样哟
如果再给我一次机会我会说我爱
过来学习下·······
博主的文章很棒,支持! ht
文章很不错 值得学习 呵呵
好文章!值得一看
很不错的,顶一下下
很少关注