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

📄 hyperspectraldatacompressdlg.cpp

📁 改进的JPEG-LS算法
💻 CPP
📖 第 1 页 / 共 3 页
字号:
		R[i]=new double[HEncode.BandNumber];
		for(j=0;j<HEncode.BandNumber;j++)R[i][j]=0;
	}
	if(HEncode.HyperspectralDataFormat==BFILE||HEncode.HyperspectralDataFormat==BSQ_H)
	{//逐波段统计
		allLine=HEncode.BandNumber*HEncode.BandNumber/2;
		bg=0;a=0;
		for(i=0;i<HEncode.BandNumber;i++)
		{
			for(j=0;j<HEncode.Height;j++)
			{//统计均方及均值
				HEncode.GetOneLineImageData(lpImageData1[j],i,j);
				StatisticOneLineImage(lpImageData1[j],HEncode.OnePixelBytes,
										HEncode.Width,Mse,M);
				R[i][i]+=Mse;
				AllM[i]+=M;
			}
			a++;
			if(((a*100)/allLine)>bg)
			{//向主窗口报告当前执行的进度
				bg=(a*100)/allLine;
				PostMessage(WM_USER_HYPERSPECTRAL_COMPRESS_DLG,2,bg);
				if(TestRelativeThreadRun==FALSE)break;//检查是否有中止标志
			}
			if(i<HEncode.BandNumber-1)
			{
				for(k=i+1;k<HEncode.BandNumber;k++)
				{
					a++;
					if(((a*100)/allLine)>bg)
					{//向主窗口报告当前执行的进度
						bg=(a*100)/allLine;
						PostMessage(WM_USER_HYPERSPECTRAL_COMPRESS_DLG,2,bg);
						if(TestRelativeThreadRun==FALSE)break;//检查是否有中止标志
					}
					for(j=0;j<HEncode.Height;j++)
					{//统计相关函数
						HEncode.GetOneLineImageData(lpImageData2,k,j);
						StatisticOneLineTwoImage(lpImageData1[j],
												lpImageData2,
												HEncode.OnePixelBytes,
												HEncode.Width,
												Relative);
						R[i][k]+=Relative;
					}
				}
				if(k<HEncode.BandNumber)break;
			}
		}
	}
	else
	{//逐行统计
		allLine=HEncode.Height;
		bg=0;
		for(j=0;j<HEncode.Height;j++)
		{
			if((j*100/allLine)>bg)
			{//向主窗口报告当前执行的进度
				bg=j*100/allLine;
				PostMessage(WM_USER_HYPERSPECTRAL_COMPRESS_DLG,2,bg);
				if(TestRelativeThreadRun==FALSE)break;//检查是否有中止标志
			}
			for(i=0;i<HEncode.BandNumber;i++)
			{
				HEncode.GetOneLineImageData(lpImageData1[i],i,j);
				StatisticOneLineImage(lpImageData1[i],HEncode.OnePixelBytes,
										HEncode.Width,Mse,M);
				R[i][i]+=Mse;
				AllM[i]+=M;
				if(i<HEncode.BandNumber-1)
				{
					for(k=i+1;k<HEncode.BandNumber;k++)
					{
						//统计相关函数
						HEncode.GetOneLineImageData(lpImageData2,k,j);
						StatisticOneLineTwoImage(lpImageData1[i],
												lpImageData2,
												HEncode.OnePixelBytes,
												HEncode.Width,
												Relative);
						R[i][k]+=Relative;
					}
				}
			}
		}
	}
	for(i=0;i<HEncode.Height;i++)
	{
		delete lpImageData1[i];
	}
	delete lpImageData1;
	delete lpImageData2;
	if(TestRelativeThreadRun==FALSE)
	{
		delete AllM;
		for(i=0;i<HEncode.BandNumber;i++)
		{
			delete R[i];
		}
		delete R;
		HEncode.EndHyperspectralCode();
		PostMessage(WM_USER_HYPERSPECTRAL_COMPRESS_DLG,3,0);
		return;
	}
	for(i=0;i<HEncode.BandNumber;i++)
	{
		AllM[i]/=HEncode.Height;//均值
		R[i][i]/=HEncode.Height;
		R[i][i]=sqrt(R[i][i]-AllM[i]*AllM[i]);//方差
	}
	for(i=0;i<HEncode.BandNumber;i++)
	{
		if(i<HEncode.BandNumber-1)
		{
			for(k=i+1;k<HEncode.BandNumber;k++)
			{
				R[i][k]/=HEncode.Height;
				R[i][k]-=AllM[i]*AllM[k];//协方差
				R[i][k]/=(R[i][i]*R[k][k]);//相关系数
				R[k][i]=R[i][k];//矩阵对称
			}
		}
		R[i][i]=1;
	}
	int *lpPredictBand=new int[HEncode.BandNumber];
	lpPredictBand[0]=-1;//第一个波段是不进行波段间预测的。
	for(i=1;i<HEncode.BandNumber;i++)
	{
		for(j=0,M=0;j<i;j++)
		{//从前面的波段中寻找预测波段。
			if(M<R[i][j])
			{
				M=R[i][j];
				lpPredictBand[i]=j;
			}
		}
		if(M<0.85)lpPredictBand[i]=-1;
	}
	delete AllM;
	for(i=0;i<HEncode.BandNumber;i++)
	{
		delete R[i];
	}
	delete R;
	PredictBandList.Empty();
	CString s;
	for(i=0;i<HEncode.BandNumber;i++)
	{
		s.Format(_T("%d "),lpPredictBand[i]);
		PredictBandList+=s;
	}
	delete lpPredictBand;
	TestRelativeComplate=TRUE;
	HEncode.EndHyperspectralCode();
	PostMessage(WM_USER_HYPERSPECTRAL_COMPRESS_DLG,3,0);
}
void CHyperspectralDataCompressDlg::StatisticOneLineImage(LPBYTE lpImageLine1,
														  int BytesPerPixel,
														  int OneLineWidth,
														  double &Mse1,
														  double &M1)
{//统计图像行均值与均方值
	int i,p1;
	Mse1=M1=0;
	if(OneLineWidth<=0||BytesPerPixel<=0)return;
	if(BytesPerPixel==1)
	{
		for(i=0;i<OneLineWidth;i++)
		{
			p1=((unsigned char)lpImageLine1[i]);
			M1+=p1;
			Mse1+=p1*p1;
		}
	}
	else if(BytesPerPixel==2)
	{
		OneLineWidth*=2;
		for(i=0;i<OneLineWidth;i+=2)
		{
			p1=*((unsigned short int *)(lpImageLine1+i));
			M1+=p1;
			Mse1+=p1*p1;
		}
		OneLineWidth/=2;
	}
	M1/=OneLineWidth;
	Mse1/=OneLineWidth;
}
void CHyperspectralDataCompressDlg::StatisticOneLineTwoImage(LPBYTE lpImageLine1,
															LPBYTE lpImageLine2,
															int BytesPerPixel,
															int OneLineWidth,
															double &Relative)
{//统计两图像行的相关函数
	int i,p1,p2;
	Relative=0;
	if(OneLineWidth<=0||BytesPerPixel<=0)return;
	if(BytesPerPixel==1)
	{
		for(i=0;i<OneLineWidth;i++)
		{
			p1=((unsigned char)lpImageLine1[i]);
			p2=((unsigned char)lpImageLine2[i]);
			Relative+=p1*p2;
		}
	}
	else if(BytesPerPixel==2)
	{
		OneLineWidth*=2;
		for(i=0;i<OneLineWidth;i+=2)
		{
			p1=*((unsigned short int *)(lpImageLine1+i));
			p2=*((unsigned short int *)(lpImageLine2+i));
			Relative+=p1*p2;
		}
		OneLineWidth/=2;
	}
	Relative/=OneLineWidth;
}
void CHyperspectralDataCompressDlg::OnKillfocusImageWidth() 
{
	UpdateData(TRUE);
	if(PureImageFormat==TRUE)
	{
		AllDataWidth=m_ImageWidth;
	}
}

void CHyperspectralDataCompressDlg::OnKillfocusAuxDataWidth() 
{
	//辅助数据宽度的输入控制
	UpdateData(TRUE);
	if(m_AuxDataWidth>=0&&m_AuxDataWidth<AllDataWidth)
	{
		if(PureImageFormat==FALSE)m_ImageWidth=AllDataWidth-m_AuxDataWidth;
		else if(m_AuxDataWidth>0)m_AuxDataWidth=0;
		UpdateData(FALSE);
	}
}

void CHyperspectralDataCompressDlg::OnKillfocusLinesPerBlock() 
{
	//压缩块行数的输入控制
	UpdateData(TRUE);
	if(m_LinesPerBlock<MIN_LINES_PER_BLOCK)
	{
		m_LinesPerBlock=MIN_LINES_PER_BLOCK;
		UpdateData(FALSE);
	}
	else if(m_LinesPerBlock>m_ImageHeight)
	{
		m_LinesPerBlock=m_ImageHeight;
		UpdateData(FALSE);
	}
	else if(m_LinesPerBlock>MAX_LINES_PER_BLOCK)
	{
		m_LinesPerBlock=MAX_LINES_PER_BLOCK;
		UpdateData(FALSE);
	}
}
void CHyperspectralDataCompressDlg::UpdateControlList(BOOL UpdateMode)//FALSE: 数据->控件,TRUE:控件->数据
{
	int i,j,Seat;
	CString s;
	LVITEM lvitem;
	TCHAR Blist[32];
	if(UpdateMode==TRUE)
	{//控件->数据
		UpdateData(TRUE);
		PredictBandList.Empty();
		j=m_RelativeList.GetItemCount();
		s=_T(" ");
		for (i = 0; i < j; i++)
		{
			PredictBandList+=m_RelativeList.GetItemText(i,1);
			PredictBandList+=s;
		}
	}
	else
	{//数据->控件
		m_RelativeList.DeleteAllItems();
		lvitem.mask = LVIF_TEXT | LVIF_STATE| LVIF_DI_SETITEM;
		for (i = 0,Seat=0; i < m_BandNumber; i++)
		{
			s.Format(_T("%d"),i);
			lvitem.iItem = i;
			lvitem.iSubItem = 0;
			lvitem.pszText = s.GetBuffer(s.GetLength());
			lvitem.cchTextMax=4;
			lvitem.lParam=i;
			lvitem.stateMask=LVM_EDITLABEL;
			lvitem.state=LVM_EDITLABEL;
			j = m_RelativeList.InsertItem(&lvitem);

			GetNextDataSeat((LPCTSTR)PredictBandList,Blist,Seat);
			lvitem.iItem = j;
			lvitem.iSubItem = 1;
			lvitem.pszText = Blist;
			lvitem.cchTextMax=4;
			lvitem.lParam=i|(lvitem.iSubItem<<16);
			lvitem.stateMask=LVM_EDITLABEL;
			lvitem.state=LVM_EDITLABEL;
			m_RelativeList.SetItem(&lvitem);
		}
		UpdateData(FALSE);
	}
}
int CHyperspectralDataCompressDlg::GetNextDataSeat(LPCTSTR lpAsc,LPSTR lpOne,int &Seat)
{
	int i=0;
	for(;lpAsc[Seat]!='-'&&(lpAsc[Seat]>'9'||lpAsc[Seat]<'0')&&lpAsc[Seat]!='\0';Seat++);
	if(lpAsc[Seat]!='\0')
	{
		for(;(lpAsc[Seat]=='-'||(lpAsc[Seat]<='9'&&lpAsc[Seat]>='0'))&&lpAsc[Seat]!='\0';Seat++)
		{
			lpOne[i++]=lpAsc[Seat];
		}
		lpOne[i]='\0';
	}
	return i;
}
void CHyperspectralDataCompressDlg::InitPredictBandString()
{
	PredictBandList.Empty();
	for(int i=0;i<m_BandNumber;i++)
	{
		PredictBandList+=_T("-1 ");
	}
}

void CHyperspectralDataCompressDlg::OnDblclkCompressRelativeList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
	// 双击列表某项,如可编辑,则响应之。
	// 实现方法:创建仅含编辑控件无头对话框,将其大小和位置置于列表项处,当输入聚焦移开此编辑框但
	// 未移开本程序时,就认为输入确认。响应Esc键就认为是取消。
	CRect Rect,ListRect;
	int ColLeft;//存放列左边界位置
	int ItemTop,ItemNumber,ItemHeight,i,x,y;
	m_RelativeList.GetWindowRect(&ListRect);//取控键屏幕矩形
	m_RelativeList.GetSubItemRect(0,0,LVIR_BOUNDS,Rect);//取初始列表对象矩形,为空时值不定
//目的是确定列表对象纵向初始位置和单对象的高度
	ItemNumber=m_RelativeList.GetItemCount();//列表数目
	ItemTop=Rect.top;//列表对象纵向初始位置
	ItemHeight=Rect.Height();//列表对象的高度
	//计算列左边界位置
	ColLeft=m_RelativeList.GetColumnWidth(0);
	x=pNMListView->ptAction.x;//当前的点击坐标,相对于ListCtrl控键
	y=pNMListView->ptAction.y;
	i=(y-ItemTop)/ItemHeight;//计算点击点所在的列表项
	//计算点击点所在的列
	if(x<=ColLeft){*pResult = 0;return;}
	m_RelativeList.GetSubItemRect(i,1,LVIR_BOUNDS,Rect);//获取子项的矩形
	Rect.top+=(ListRect.top+2);//将子项矩形映射成屏幕矩形
	Rect.left+=(ListRect.left+2);
	Rect.bottom+=(ListRect.top+2);
	Rect.right+=(ListRect.left+2);
	TCHAR asc[1024];
	CString s,str;
	CSmallEdit *pEdit=new CSmallEdit;
	if(pEdit!=NULL)
	{
		pEdit->Rect=Rect;//将创建的编辑框矩形置成子项矩形
		m_RelativeList.GetItemText( i, 1, asc, 255);
		pEdit->m_Text=asc;
		pEdit->Item.x=i;//为返回具体子项提供信息
		pEdit->Item.y=1;
		pEdit->RemoveEditStyle=ES_MULTILINE|ES_AUTOVSCROLL;
		if(pEdit->Create(NULL,NULL,0,CRect(0,0,0,0),this)==TRUE)//用无模态对话框,是要在对话框外可响应
		{// ES_READONLY ES_WANTRETURN                   注:this 是接收返回WM_USER_SMALL_EDIT消息的窗口
			pEdit->ShowWindow(SW_SHOW);
		}
	}
	*pResult = 0;
}
LRESULT CHyperspectralDataCompressDlg::OnSmallEditRecvMessage(WPARAM wParam, LPARAM lParam)
{
	CSmallEdit *pEdit=(CSmallEdit *)lParam;

	m_RelativeList.SetItemText(pEdit->Item.x,pEdit->Item.y,(LPCTSTR)pEdit->m_Text);
	UpdateControlList(TRUE);
	return 0;
}
/////////////////////////////////////////////////////////////////////////////
// HyperspectralCompressThread

IMPLEMENT_DYNCREATE(HyperspectralCompressThread, CWinThread)

HyperspectralCompressThread::HyperspectralCompressThread()
{
	m_bDone = FALSE;
	m_pOwner = NULL;
	m_bAutoDelete = FALSE;
}

HyperspectralCompressThread::~HyperspectralCompressThread()
{
}

BOOL HyperspectralCompressThread::InitInstance()
{
	// TODO:  perform and per-thread initialization here
	return TRUE;
}

int HyperspectralCompressThread::ExitInstance()
{
	// TODO:  perform any per-thread cleanup here
	return CWinThread::ExitInstance();
}

BEGIN_MESSAGE_MAP(HyperspectralCompressThread, CWinThread)
	//{{AFX_MSG_MAP(HyperspectralCompressThread)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// HyperspectralCompressThread message handlers

int HyperspectralCompressThread::Run() 
{
	// TODO: Add your specialized code here and/or call the base class
	m_pOwner->OkCompressProce();
	
	return 0;
}
/////////////////////////////////////////////////////////////////////////////
// HyperspectralTestRelativeThread

IMPLEMENT_DYNCREATE(HyperspectralTestRelativeThread, CWinThread)

HyperspectralTestRelativeThread::HyperspectralTestRelativeThread()
{
	m_bDone = FALSE;
	m_pOwner = NULL;
	m_bAutoDelete = FALSE;
}

HyperspectralTestRelativeThread::~HyperspectralTestRelativeThread()
{
}

BOOL HyperspectralTestRelativeThread::InitInstance()
{
	// TODO:  perform and per-thread initialization here
	return TRUE;
}

int HyperspectralTestRelativeThread::ExitInstance()
{
	// TODO:  perform any per-thread cleanup here
	return CWinThread::ExitInstance();
}

BEGIN_MESSAGE_MAP(HyperspectralTestRelativeThread, CWinThread)
	//{{AFX_MSG_MAP(HyperspectralTestRelativeThread)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// HyperspectralTestRelativeThread message handlers

int HyperspectralTestRelativeThread::Run() 
{
	// TODO: Add your specialized code here and/or call the base class
	m_pOwner->OkTestRelativeProce();
	
	return 0;
}

⌨️ 快捷键说明

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