?? diproc.cpp
字號:
// Diproc.cpp: implementation of the CDiproc class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "wavelet.h"
#include "Diproc.h"
#include"WaveletTrans.h"
#include<math.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CDiproc::CDiproc()
{
}
CDiproc::~CDiproc()
{
}
void CDiproc::Band_Enhance(float **fpNormGradient, float FilterCoeff, int Scan_y, int Scan_x, int End_y, int End_x)
{
for(int y = Scan_y; y < End_y; y ++)
for(int x = Scan_x; x < End_x; x ++)
fpNormGradient[y][x] *= (float)(255.0 * FilterCoeff);
}
void CDiproc::DIP_ConsEnhance(short **spData, int nHeight, int nWidth, float *NormWvltRng)
{
short **spOriginData, **spTransData, **spWvltData;
float **fpNormGradient , filtCoeff[10], *fWvltRng;
int iHeight = nHeight, iWidth = nWidth;
int iHeight_H = nHeight / 2, iWidth_H = nWidth / 2;
int x, y;
fWvltRng = NormWvltRng;
//為圖像處理分配內(nèi)存空間
spOriginData = spData;
spTransData = new short * [nHeight];
spWvltData = new short * [nHeight];
fpNormGradient = new float * [nHeight];
for(int i = 0; i < nHeight; i ++)
{
spTransData[i] = new short [nWidth];
spWvltData[i] = new short [nWidth];
fpNormGradient[i] = new float [nWidth];
}
//完成一次圖像小波變換
CWaveletTrans *pTrans;
pTrans->DWT_Three(spOriginData, spTransData, spWvltData, iHeight, iHeight_H, iWidth, iWidth_H, 3, 1.414);
//小波系數(shù)的正則化處理
//正則化處理后spData存放非正則化的小波系數(shù),spWvltData存放正則化后的小波系數(shù)
Wvlt_Normalize(spWvltData, iHeight, iWidth, fWvltRng);
//計算小波系數(shù)的梯度信息,將其存放在spTransData中
//設定顯示設備的顏色灰度范圍是0~255
for(y = 0; y < nHeight; y ++)
{
for(x = 0; x < nWidth; x ++)
{
fpNormGradient[y][x] = (float) spWvltData[y][x] / 255;
}
}
//選擇線性濾波器gj(x) = x, 且v =kj gj(u)
//統(tǒng)計出不同頻帶小波系數(shù)的極大值
for(y = 0; y < 3; y ++)
{
if(y == 0)
{
filtCoeff[4*y] = Search_BandMax(spWvltData, 0, 0, iHeight / 8, iWidth / 8);
}
filtCoeff[3*y + 1] = Search_BandMax(spWvltData, 0, (int)(pow(2,y) * iWidth / 8), (int)(pow(2,y)* iHeight / 8), (int)(pow(2,y)*iWidth / 4));
filtCoeff[3*y + 2] = Search_BandMax(spWvltData, (int)(pow(2,y) * iHeight / 8), 0, (int)(pow(2,y)* iHeight / 4), (int)(pow(2,y)*iWidth / 8));
filtCoeff[3*y + 3] = Search_BandMax(spWvltData, (int)(pow(2,y) * iHeight / 8), (int)(pow(2,y) * iWidth / 8), (int)(pow(2,y)* iHeight / 4), (int)(pow(2,y)*iWidth / 4));
}
//計算得到各頻帶濾波器的濾波系數(shù)
for(y = 0; y < 10; y++)
{
filtCoeff[y] = (float) 255.0 / filtCoeff[y];
filtCoeff[0] += (float)sqrt(filtCoeff[y]);
}
filtCoeff[0] /= 10;
//正則化后小波信息的梯度信息濾波處理
for(y = 0; y < 3; y ++)
{
if(y == 0)
{
Band_Enhance(fpNormGradient, filtCoeff[3*y], 0, 0, iHeight / 8, iWidth / 8);
}
Band_Enhance(fpNormGradient, (float)sqrt(filtCoeff[3*y + 1]) /2, 0, (int)(pow(2,y) * iWidth / 8), (int)(pow(2,y)* iHeight / 8), (int)(pow(2,y)*iWidth / 4));
Band_Enhance(fpNormGradient, (float)sqrt(filtCoeff[3*y + 2]) /2, (int)(pow(2,y) * iHeight / 8), 0, (int)(pow(2,y)* iHeight / 4), (int)(pow(2,y)*iWidth / 8));
Band_Enhance(fpNormGradient, (float)sqrt(filtCoeff[3*y + 3]) /2, (int)(pow(2,y) * iHeight / 8), (int)(pow(2,y) * iWidth / 8), (int)(pow(2,y)* iHeight / 4), (int)(pow(2,y)*iWidth / 4));
}
//還原出濾波增強后的小波系數(shù)
for(y = 0; y< iHeight; y ++)
{
for(x = 0; x < iWidth; x++)
{
fpNormGradient[y][x] *= (float) (fWvltRng[1] - fWvltRng[0]);
fpNormGradient[y][x] /= 255.0;
spWvltData[y][x] = (short) fpNormGradient[y][x] + fWvltRng[0];
}
}
//復原增強后的小波系數(shù)
DIP_WvltRevers(spOriginData, spTransData, spWvltData, iHeight, iHeight_H, iWidth, iWidth_H, 3, 1.414);
//將復原的圖像數(shù)據(jù)進行正則化
Wvlt_Normalize(spOriginData, iHeight, iWidth, fWvltRng);
//釋放臨時的數(shù)據(jù)空間
delete spTransData;
delete spWvltData;
delete fpNormGradient;
}
void CDiproc::DIP_ImageFusion(short **spImgData0, short **spImgData1, int nHeight, int nWidth)
{
//獲取圖像的屬性參數(shù)
int iHeight = nHeight, iWidth = nWidth;
//圖像融合所用到的數(shù)據(jù)空間及數(shù)據(jù)指針
short **spOriginData, **spTransData, **spWvltData0, **spWvltData1;
//分配數(shù)據(jù)空間
spTransData = new short *[iHeight];
spWvltData0 = new short *[iHeight];
spWvltData1 = new short *[iHeight];
for(int i = 0; i < iWidth; i ++)
{
spTransData[i] = new short [iWidth];
spWvltData0[i] = new short [iWidth];
spWvltData1[i] = new short [iWidth];
}
//創(chuàng)建小波變換類,完成圖像的小波變換
CWaveletTrans *pTrans;
//獲得圖像數(shù)據(jù)空間的指針,完成小波變換
spOriginData = spImgData0;
//三層小波變換
pTrans->DWT_Three(spOriginData, spTransData, spWvltData0, iHeight, iHeight / 2, iWidth, iWidth / 2, 1, 1.414);
//獲得圖像數(shù)據(jù)空間的指針,完成另一幅圖像的小波變換
spOriginData = spImgData1;
//三層小波變換
pTrans->DWT_Three(spOriginData, spTransData, spWvltData1, iHeight,iHeight / 2, iWidth, iWidth / 2, 1, 1.414);
//小波系數(shù)的融合處理:頻帶有LL3,LH3,HL3,HH3,LH2,HL2,HH2,LH1,HL1,HH1
//融合處理將分頻帶進行,處理方法采用的是3*3的窗口
//LL3頻帶小波系數(shù)的融合
Window_WvltFusion(spWvltData0, spWvltData1, 0, 0, iHeight / 8, iWidth / 8);
//HL3頻帶小波系數(shù)的融合
Window_WvltFusion(spWvltData0, spWvltData1, 0, iWidth / 8, iHeight / 8, iWidth / 4);
//LH3頻帶小波系數(shù)的融合
Window_WvltFusion(spWvltData0, spWvltData1, iHeight / 8, 0, iHeight / 4, iWidth / 8);
//HH3頻帶小波系數(shù)的融合
Window_WvltFusion(spWvltData0, spWvltData1, iHeight / 8, iWidth / 8, iHeight / 4, iWidth / 4);
//HL2頻帶小波系數(shù)的融合
Window_WvltFusion(spWvltData0, spWvltData1, 0, iWidth / 4, iHeight / 4, iWidth / 2);
//LH2頻帶小波系數(shù)的融合
Window_WvltFusion(spWvltData0, spWvltData1, iHeight / 4, 0, iHeight / 2, iWidth / 4);
//HH2頻帶小波系數(shù)的融合
Window_WvltFusion(spWvltData0, spWvltData1, iHeight / 4, iWidth / 4, iHeight / 2, iWidth / 2);
//HL1頻帶小波系數(shù)的融合
Window_WvltFusion(spWvltData0, spWvltData1, 0, iWidth / 2, iHeight / 2, iWidth);
//LH1頻帶小波系數(shù)的融合
Window_WvltFusion(spWvltData0, spWvltData1, iHeight / 2, 0, iHeight, iWidth / 2);
//HH1頻帶小波系數(shù)的融合
Window_WvltFusion(spWvltData0, spWvltData1, iHeight / 2, iWidth / 2, iHeight, iWidth);
//將融合后的小波系數(shù)復原,完成圖像的融合
DIP_WvltRevers(spOriginData, spTransData, spWvltData0, iHeight, iHeight / 2, iWidth, iWidth / 2, 1, 1.414);
//釋放臨時的數(shù)據(jù)空間
delete spTransData;
delete spWvltData0;
delete spWvltData1;
}
void CDiproc::DIP_WvltRevers(short **spData, short **spTransData0, short **spTransData1, int nHeight, int nHeight_H, int nWidth, int nWidth_H, int layer, float fRadius)
{
short **spOriginData, **spTransData, **spWvltData;
int iHeight = (int)nHeight /pow(2,layer-1), iWidth =(int)nWidth / pow(2,layer-1);
int iHeight_H =(int) nHeight_H / pow(2,layer-1), iWidth_H = (int)nWidth_H/ pow(2,layer-1);
//分配圖像復原所需的內(nèi)存空間
spOriginData = spData;
spTransData = spTransData0;
spWvltData = spTransData1;
//完成圖像小波變換的逆變換
CWaveletTrans *WTrans;
for(int i = layer; i >= 1; i--)
{
WTrans->DWTi_Once(spOriginData, spTransData, spWvltData, iHeight, iHeight_H, iWidth, iWidth_H, i, 1.414);
iHeight <<= 1; iWidth <<= 1;
iHeight_H <<= 1; iWidth_H <<= 1;
}
}
float CDiproc::Search_BandMax(short **spWvltData, int Scan_y, int Scan_x, int End_y, int End_x)
{
int x, y;
float Band_max;
Band_max = 0;
for(y = Scan_y; y < End_y; y ++)
{
for(x = Scan_x; x < End_x; x ++)
{
if(Band_max < spWvltData[y][x])
Band_max = spWvltData[y][x];
}
}
return Band_max;
}
void CDiproc::Window_WvltFusion(short **spWvltData0, short **spWvltData1, int Scan_y, int Scan_x, int End_y, int End_x)
{
int y,x;
short WndSum0, WndSum1;
for(y = Scan_y; y < End_y; y ++)
{
for(x = Scan_x; x < End_x; x ++)
{
//初始化窗口中小波系數(shù)的和
WndSum0 = 0; WndSum1 = 0;
//計算窗口中小波系數(shù)的和
for(int i = -1; i <= 1; i++)
{
for(int j = -1; j <= 1; j++)
{
if( (y+i) < Scan_y || (x+j) < Scan_x || (y+i) >= End_y || (x+j) >= End_x)
{
WndSum0 += 0;
WndSum1 += 0;
}
else
{
WndSum0 += abs((int)spWvltData0[y + i][x + j]);
WndSum1 += abs((int)spWvltData1[y + i][x + j]);
}
}
}
if(WndSum0 < WndSum1)
spWvltData0[y][x] = spWvltData1[y][x];
}
}
}
void CDiproc::Wvlt_Normalize(short **spWvltNormData, int nHeight, int nWidth, float *nWvltRng)
{
int MaxPixVal, MinPixVal, Diff;
int x, y;
float NormCoeff;
MaxPixVal=spWvltNormData[0][0];
MinPixVal=spWvltNormData[0][0];
for( y=0; y < nHeight; y++)
{
for( x=0; x < nWidth; x++)
{
if(MaxPixVal<spWvltNormData[y][x])
MaxPixVal=spWvltNormData[y][x];
if(MinPixVal>spWvltNormData[y][x])
MinPixVal=spWvltNormData[y][x];
//spWvltData[y][x] = spWvltNormData[y][x];
}
}
Diff=MaxPixVal-MinPixVal;
nWvltRng[1] = MaxPixVal;
nWvltRng[0] = MinPixVal;
for(y=0; y < nHeight; y++)
{
for(x=0; x < nWidth; x++)
{
//因為小波變換后的小波系數(shù)有可能超過255甚至更多,那么就將
//小波系數(shù)的范圍映射到0~255區(qū)間內(nèi),以后出現(xiàn)類似的處理,目的都是一樣的
NormCoeff = spWvltNormData[y][x];
NormCoeff -= MinPixVal;
NormCoeff *= 255;
NormCoeff /= (float) Diff;
spWvltNormData[y][x] = NormCoeff;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -