⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testview.cpp

📁 方向自适应的小波变换程序(5/3) 本人自己编写
💻 CPP
📖 第 1 页 / 共 5 页
字号:
	double ratio=10 * log10((double)(temp/difference));

	::GlobalUnlock (hDIBSource);
*/   

	// 解除锁定
	::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());	
	
}

void CTestView::ADLwavelet_transform_overlap(int **dst, const int width, const int height, const int levels)
{
	int l;
	int* tmp_lastrow=(int*)malloc((width+height+height)*sizeof(int));
	int* tmp_column_in=tmp_lastrow+width;
	int* tmp_column_out=tmp_lastrow+width+height;


	for(l=0;l<levels;l++)
	{  int w=width>>l;
	   int h=height>>l;
	   int i;

	   //首先是行变换
	   for(i=0;i<h;i++)
	   { 
		  wavelet_row(tmp_lastrow,dst[i],w);
		  memcpy(dst[i],tmp_lastrow,w*sizeof(int));
	   }
	   
       //然后是列变换
       for(i=0;i<w;i++)
	   {  int j;
	      
	      for(j=0;j<h;j++)
			  tmp_column_in[j]=dst[j][i];  
		  
		  wavelet_row(tmp_column_out,tmp_column_in,h);

          for(j=0;j<h;j++)
			  dst[j][i]=tmp_column_out[j];
	   }  
	}
	free(tmp_lastrow);
}


void CTestView::inverse_Horizontal_Update(int levels)
{
	int i,j,k;
	
	int temp,temp1,temp2;
	int x,y;
	int *ttt;
	 ttt=new int[MyHeight];

	for(i=0;i<MyWidth*4;i++)
	{
		for(j=0;j<MyHeight;j++)
		{ttt[j]=ori_QuartPixel_interp[j][i];}
		for(j=0;j<MyHeight/2;j++)
		{
			ori_QuartPixel_interp[2*j][i]=ttt[j];
			ori_QuartPixel_interp[2*j+1][i]=ttt[j+MyHeight/2];
		}
	}
	for(j=0;j<MyWidth;j++) //第1行更新
		{ 
			k=total_preDrection[levels-1][int(j/N)];
			//k=Predict_direc[0][int(j/N)];
			temp=ori_QuartPixel_interp[0][4*j];
			x=4*j+k;
			y=4*j-k;
			if(x<0)
			{x=-x;}
			if(y<0)
			{y=-y;}
			if(x>4*MyWidth-1)
			{x=8*(MyWidth-1)-x;}
			if(y>4*MyWidth-1)
			{y=8*(MyWidth-1)-y;}
			temp1=ori_QuartPixel_interp[1][x];
			temp2=ori_QuartPixel_interp[1][y];
			ori_QuartPixel_interp[0][4*j]=temp-(temp1+temp2)/4;
		}
	for(j=0;j<MyWidth;j++)//后MyHeight-1行
		{
			for(i=2;i<MyHeight;i+=2)
			{
				k=total_preDrection[levels-1][((int)i/M)*(MyWidth/N)+int(j/N)];
				//k=Predict_direc[(int)i/M][int(j/N)];
				temp=ori_QuartPixel_interp[i][4*j];
				x=4*j+k;
				y=4*j-k;
				if(x<0)
				{x=-x;}
				if(y<0)
				{y=-y;}
				if(x>4*MyWidth-1)
				{x=8*(MyWidth-1)-x;}
				if(y>4*MyWidth-1)
				{y=8*(MyWidth-1)-y;}
				temp1=ori_QuartPixel_interp[i-1][x];
				temp2=ori_QuartPixel_interp[i+1][y];
				ori_QuartPixel_interp[i][4*j]=temp-(temp1+temp2)/4;
			}
		}
	for(i=0;i<MyHeight;i++)
       for(j=0;j<MyWidth;j++)
 	  {  origin[i][j]=ori_QuartPixel_interp[i][4*j] ;}

	delete[] ttt;
	ttt=NULL;
}


void CTestView::inverse_Vertical_Update(int levels)
{
	int i,j,k;
	
	int temp,temp1,temp2;
	int x,y;
	int *ttt;
	 ttt=new int[MyWidth];

	for(i=0;i<MyHeight*4;i++)
	{
		for(j=0;j<MyWidth;j++)
		{ttt[j]=ori_QuartPixel_interp1[i][j];}
		for(j=0;j<MyWidth/2;j++)
		{
			ori_QuartPixel_interp1[i][2*j]=ttt[j];
			ori_QuartPixel_interp1[i][2*j+1]=ttt[j+MyWidth/2];
		}
	}
	for(j=0;j<MyHeight;j++) //第1列逆更新
		{
			k=total_preDrection1[levels-1][(int(j/M))*(MyWidth/N)];
			//k=Predict_direc1[int(j/M)][0];
			temp=ori_QuartPixel_interp1[4*j][0];
			x=4*j+k;
			y=4*j-k;
			if(x<0)
			{x=-x;}
			if(y<0)
			{y=-y;}
			if(x>4*MyHeight-1)
			{x=8*(MyHeight-1)-x;}
			if(y>4*MyHeight-1)
			{y=8*(MyHeight-1)-y;}
			temp1=ori_QuartPixel_interp1[x][1];
			temp2=ori_QuartPixel_interp1[y][1];
			ori_QuartPixel_interp1[4*j][0]=temp-(temp1+temp2)/4;
		}
	for(j=0;j<MyHeight;j++)//后MyWidth-1列
		{
			for(i=2;i<MyWidth;i+=2)
			{
				k=total_preDrection1[levels-1][(int(j/M))*(MyWidth/N)+int(i/N)];
				//k=Predict_direc1[(int)j/M][int(i/N)];
				temp=ori_QuartPixel_interp1[4*j][i];
				x=4*j+k;
				y=4*j-k;
				if(x<0)
				{x=-x;}
				if(y<0)
				{y=-y;}
				if(x>4*MyHeight-1)
				{x=8*(MyHeight-1)-x;}
				if(y>4*MyHeight-1)
				{y=8*(MyHeight-1)-y;}
				temp1=ori_QuartPixel_interp1[x][i-1];
				temp2=ori_QuartPixel_interp1[y][i+1];
				ori_QuartPixel_interp1[4*j][i]=temp-(temp1+temp2)/4;
			}
		}
	 for(i=0;i<MyHeight;i++)
       for(j=0;j<MyWidth;j++)
 	  {  origin[i][j]=ori_QuartPixel_interp1[4*i][j] ;}
	   
	 delete[] ttt;
	 ttt=NULL;
}

void CTestView::inverse_Vertical_Prediction(int levels)
{
	int i,j,k;
	int temp,temp1,temp2;
	int x,y;
	

	for(j=1;j<MyWidth-1;j+=2)//前MyWidth-1列
		{
			for(i=0;i<MyHeight;i++)
			{
				k=total_preDrection1[levels-1][(int(i/M))*(MyWidth/N)+int(j/N)];
				//k=Predict_direc1[(int)i/M][int(j/N)];
				temp=ori_QuartPixel_interp1[4*i][j];
				x=4*i+k;
				y=4*i-k;
				if(x<0)
				{x=-x;}
				if(y<0)
				{y=-y;}
				if(x>4*MyHeight-1)
				{x=8*(MyHeight-1)-x;}
				if(y>4*MyHeight-1)
				{y=8*(MyHeight-1)-y;}
				temp1=ori_QuartPixel_interp1[x][j-1];
				temp2=ori_QuartPixel_interp1[y][j+1];
				ori_QuartPixel_interp1[4*i][j]=temp+(temp1+temp2)/2;
			}
		}
		for(j=0;j<MyHeight;j++) //第MyWidth-1列
		{
			k=total_preDrection1[levels-1][(int(j/M))*(MyWidth/N)+MyWidth/N-1];
			//k=Predict_direc1[int(j/M)][MyWidth/N-1];
			temp=ori_QuartPixel_interp1[4*j][MyWidth-1];
			x=4*j+k;
			y=4*j-k;
			if(x<0)
			{x=-x;}
			if(y<0)
			{y=-y;}
			if(x>4*MyHeight-1)
			{x=8*(MyHeight-1)-x;}
			if(y>4*MyHeight-1)
			{y=8*(MyHeight-1)-y;}
			temp1=ori_QuartPixel_interp1[x][MyWidth-2];
			temp2=ori_QuartPixel_interp1[y][MyWidth-2];
			ori_QuartPixel_interp1[4*j][MyWidth-1]=temp+(temp1+temp2)/2;
		}
	for(i=0;i<MyHeight;i++)
		for(j=0;j<MyWidth;j++)
		{origin[i][j]=ori_QuartPixel_interp1[4*i][j];}
}

void CTestView::inverse_Horizontal_Prediction(int levels)
{
	int i,j,k;
	int x,y;
	int temp,temp1,temp2;

	for(j=0;j<MyWidth;j++)
		{
			for(i=1;i<MyHeight-1;i+=2)//前MyHeight-1行
			{	
				k=total_preDrection[levels-1][(int(i/M))*(MyWidth/N)+int(j/N)];
				//k=Predict_direc[(int)i/M][int(j/N)];
				temp=ori_QuartPixel_interp[i][4*j];
				x=4*j+k;
				y=4*j-k;
				if(x<0)
				{x=-x;}
				if(y<0)
				{y=-y;}
				if(x>4*MyWidth-1)
				{x=8*(MyWidth-1)-x;}
				if(y>4*MyWidth-1)
				{y=8*(MyWidth-1)-y;}
				temp1=ori_QuartPixel_interp[i-1][x];
				temp2=ori_QuartPixel_interp[i+1][y];
				ori_QuartPixel_interp[i][4*j]=temp+(temp1+temp2)/2;
			}
		}
		for(j=0;j<MyWidth;j++) //第MyHeight-1行
		{
			k=total_preDrection[levels-1][(MyHeight/M-1)*(MyWidth/N)+int(j/N)];
			//k=Predict_direc[MyHeight/M-1][int(j/N)];
			temp=ori_QuartPixel_interp[MyHeight-1][4*j];
			x=4*j+k;
			y=4*j-k;
			if(x<0)
			{x=-x;}
			if(y<0)
			{y=-y;}
			if(x>4*MyWidth-1)
			{x=8*(MyWidth-1)-x;}
			if(y>4*MyWidth-1)
			{y=8*(MyWidth-1)-y;}
			temp1=ori_QuartPixel_interp[MyHeight-2][x];
			temp2=ori_QuartPixel_interp[MyHeight-2][y];
			ori_QuartPixel_interp[MyHeight-1][4*j]=temp+(temp1+temp2)/2;
		}
	
	for(i=0;i<MyHeight;i++)
		for(j=0;j<MyWidth;j++)
		{origin[i][j]=ori_QuartPixel_interp[i][4*j];}
}

void CTestView::Sinc_coefficitns()
{
	int i,j;
	int l;
	Sinc_interpolation=new double* [3];
	for(i=0;i<3;i++)
	   Sinc_interpolation[i]=new double [M_sample*2];
		for(j=0;j<M_sample*2;j++)
		{
			l=j-(M_sample-1);
			Sinc_interpolation[0][j]=sin(PI*(0.25-l))/(PI*(0.25-l));
			Sinc_interpolation[1][j]=sin(PI*(0.5-l))/(PI*(0.5-l));
			Sinc_interpolation[2][j]=sin(PI*(0.75-l))/(PI*(0.75-l));
		}

}

void CTestView::OnINTQuincunxWAVELETFORWARD() 
{
	// 获取文档
	CTestDoc* pDoc = GetDocument();
	
	// 指向DIB的指针
	LPSTR lpDIB;
	
	// 指向DIB象素指针
	LPSTR    lpDIBBits;
	
	// 锁定DIB
	lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB());
	
	// 找到DIB图像象素起始位置
	lpDIBBits = ::FindDIBBits(lpDIB);
	
	// 判断是否是8-bpp位图(这里为了方便,只处理8-bpp位图,其它的可以类推)
	if (::DIBNumColors(lpDIB) != 256)
	{
		// 提示用户
		MessageBox("目前只支持查看256色位图灰度直方图!", "系统提示" , MB_ICONINFORMATION | MB_OK);
		
		// 解除锁定
		::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
		
		// 返回
		return;
	}
	
	int lHeight,lWidth;
	int  i,j,m;
	lHeight=::DIBHeight(lpDIB);
	lWidth=::DIBWidth(lpDIB);
	DWORD dwLineBytes=WIDTHBYTES(lWidth*8);	
	
	MyHeight=lHeight;
	MyWidth=lWidth;
	M=8;
	N=8;
	
	Dialog1 tt;
	if(tt.DoModal()==IDOK)
	{ levels=tt.m_Levels;
	}
	free(tt);
	//申请空间,初始化原始图像数组
	origin=new int* [MyHeight];
	for(i=0;i<MyHeight;i++)
	   origin[i]=new int [MyWidth];
	for(i=0;i<MyHeight;i++)
      for(j=0;j<MyWidth;j++)
	  {
			//LPSTR lptemp=lpDIBBits+(lHeight-i-1)*dwLineBytes+j; //-128---+127
			//origin[i][j]=(int)(*lptemp);
			BYTE *lptemp=(BYTE *)lpDIBBits+(lHeight-1-i)*dwLineBytes+j; //0--255
			origin[i][j]=(int)(*lptemp);
	  }
	
	total_preDrection=new int* [levels];
	total_preDrection1=new int* [levels];

	for(m=0;m<levels;m++)
	{
		//预测
		quincunx_Prediction();
		//更新
		quincunx_Update();
		//在一次对L和H预测
		quincunx_Prediction1();
		//在一次对L和H更新
		quincunx_Update1();
		
		//保存预测的方向信息
		total_preDrection[m]=new int [(MyHeight/M)*(MyWidth/N)];
		for(i=0;i<MyHeight/M;i++)
			for(j=0;j<MyWidth/N;j++)
			{
				total_preDrection[m][i*(MyWidth/N)+j]=Predict_direc[i][j];
			}
		total_preDrection1[m]=new int [(MyHeight/M)*(MyWidth/N)];
		for(i=0;i<MyHeight/M;i++)
			for(j=0;j<MyWidth/N;j++)
			{
				total_preDrection1[m][i*(MyWidth/N)+j]=Predict_direc1[i][j];
			}
		//下一级分解初始化
		MyHeight=MyHeight/2;
		MyWidth=MyWidth/2;

		  if(Predict_direc!=NULL)
		  {delete Predict_direc;
		  Predict_direc=NULL;}
		  if(Block_energy!=NULL)
		  {delete Block_energy;
		   Block_energy=NULL;}

		  if(Predict_direc1!=NULL)
		  {delete Predict_direc1;
		   Predict_direc1=NULL;}
		  if(Block_energy1!=NULL)
		  {delete Block_energy1;
		  Block_energy1=NULL;} 
	}
	
	MyHeight=MyHeight<<levels;
	MyWidth=MyWidth<<levels;
	
	// 更新视图
	pDoc->UpdateAllViews(NULL);
	
    // 解除锁定
	::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
	//下面是将得到的系数写入文件,保存起来,文件保存在工程所在的目录下
	CFile CoeMatrix;
	CFileException fe;
	CoeMatrix.Open("coefficients.txt",CFile::modeCreate | CFile::modeReadWrite | CFile::shareExclusive,&fe);

	CString str;

	int E=0;
	int max=-10000;
	int min=100000;
	for(i=MyHeight/2;i<MyHeight;i++)
      for(j=0;j<MyWidth;j++)
	  {
		  if(max<origin[i][j])
		  {max=origin[i][j];}
		  if(min>origin[i][j])
		  {min=origin[i][j];}
		  E+=abs(origin[i][j]);
	  }

	str.Format("Max:%d,Min:%d,Energy:%d",max,min,E);
//	AfxMessageBox(str);


	for(i=0;i<lHeight;i++)
      for(j=0;j<lWidth;j++)
	  {
		  BYTE* lptemp=(BYTE*)lpDIBBits+(lHeight-i-1)*dwLineBytes+j;
		  (*lptemp)=(BYTE)origin[i][j];
	  }

}

void CTestView::OnINTQuincunxWAVELETBACKWARD() 
{

	// 获取文档
	CTestDoc* pDoc = GetDocument();
	
	// 指向DIB的指针
	LPSTR lpDIB;
	
	// 指向DIB象素指针
	LPSTR    lpDIBBits;
	
	// 锁定DIB
	lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB());
	
	// 找到DIB图像象素起始位置
	lpDIBBits = ::FindDIBBits(lpDIB);
	
	// 判断是否是8-bpp位图(这里为了方便,只处理8-bpp位图,其它的可以类推)
	if (::DIBNumColors(lpDIB) != 256)
	{
		// 提示用户
		MessageBox("目前只支持查看256色位图灰度直方图!", "系统提示" , MB_ICONINFORMATION | MB_OK);
		
		// 解除锁定
		::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
		
		// 返回
		return;
	}
		
	int lHeight,lWidth;
	int  i,j,m;

	lHeight=::DIBHeight(lpDIB);
	lWidth=::DIBWidth(lpDIB);
	DWORD dwLineBytes=WIDTHBYTES(lWidth*8);	
	MyHeight=lHeight;
	MyWidth=lWidth;

	for(m=levels;m>0;m--)
	{
		MyHeight=MyHeight>>(m-1);
		MyWidth=MyWidth>>(m-1);
		
/*		//预测
		quincunx_Prediction();
		//更新
		quincunx_Update();
		//在一次对L和H预测
		quincunx_Prediction1();
		//在一次对L和H更新
		quincunx_Update1();  */

		//对L和H逆更新
		inverse_quincunx_Update1(m);
		//对L和H逆预测
		inverse_quincunx_Prediction1(m);
		//逆更新
		inverse_quincunx_Update(m);
		//逆预测
		inverse_quincunx_Prediction(m);



		//下一级逆变换初始化

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -