📄 pixelfusiondoc.cpp
字号:
// PixelFusionDoc.cpp : implementation of the CPixelFusionDoc class
//
#include "stdafx.h"
#include "PixelFusion.h"
#include "PixelFusionDoc.h"
#include "DlgOpenFile.h"
#include "dibapi.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/*
* Dib文件头标志(字符串"BM",写DIB时用到该常数)
*/
#define DIB_HEADER_MARKER ((WORD) ('M' << 8) | 'B')
#define WIDTHBYTES(bits) (((bits) + 31) / 32 * 4)
#define pi (float)3.14159
#include "math.h"
/////////////////////////////////////////////////////////////////////////////
// CPixelFusionDoc
IMPLEMENT_DYNCREATE(CPixelFusionDoc, CDocument)
BEGIN_MESSAGE_MAP(CPixelFusionDoc, CDocument)
//{{AFX_MSG_MAP(CPixelFusionDoc)
ON_COMMAND(ID_CorrelationFusion, OnCorrelationFusion)
ON_COMMAND(ID_TriangleHISFusion, OnTriangleHISFusion)
ON_COMMAND(ID_Cylinder1HISFusion, OnCylinder1HISFusion)
ON_COMMAND(ID_Cylinder2HISFusion, OnCylinder2HISFusion)
ON_COMMAND(ID_BroveyFusion, OnBroveyFusion)
ON_COMMAND(ID_MultiplyFusion, OnMultiplyFusion)
ON_COMMAND(ID_LineMultiplyFusion, OnLineMultiplyFusion)
ON_COMMAND(ID_PrincipalFusion, OnPrincipalFusion)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPixelFusionDoc construction/destruction
CPixelFusionDoc::CPixelFusionDoc()
{
// TODO: add one-time construction code here
m_hDIB = NULL;
m_palDIB = NULL;
m_hMulDIB = NULL;
m_hFusionDIB = NULL;
m_sizeDoc = CSize(1,1);
lpszResultPath = "";
lpszMulspecPath = "";
lpszSpotPath = "";
}
CPixelFusionDoc::~CPixelFusionDoc()
{
}
BOOL CPixelFusionDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CPixelFusionDoc serialization
void CPixelFusionDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CPixelFusionDoc diagnostics
#ifdef _DEBUG
void CPixelFusionDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CPixelFusionDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
void CPixelFusionDoc::OpenFile()
{
DlgOpenFile dlgOpenFile;
if (dlgOpenFile.DoModal() != IDOK)
{
AfxGetMainWnd()->SendMessage(WM_CLOSE);
return;
}
lpszMulspecPath = dlgOpenFile.m_lpszMulspecPath;
lpszSpotPath = dlgOpenFile.m_lpszSpotPath;
m_hDIB = NULL;
m_hMulDIB = NULL;
m_hFusionDIB = NULL;
if (lpszSpotPath=="")
return;
if (lpszMulspecPath=="")
return;
/////////////////////////////////////////////////
// 打开全色图像文件
//
CFile file;
CFileException fe;
if (!file.Open(lpszSpotPath, CFile::modeRead | CFile::shareDenyWrite, &fe))
{
// 失败
ReportSaveLoadException(lpszSpotPath, &fe,
FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
// 返回FALSE
return ;
}
DeleteContents();
// 尝试调用ReadDIBFile()读取图像
TRY
{
m_hDIB = ::ReadDIBFile(file);
}
CATCH (CFileException, eLoad)
{
// 读取失败
file.Abort();
// 恢复光标形状
EndWaitCursor();
// 报告失败
ReportSaveLoadException(lpszSpotPath, eLoad,
FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
// 设置DIB为空
m_hDIB = NULL;
file.Close();
// 返回FALSE
return ;
}
END_CATCH
// 初始化DIB
InitDIBData();
// 判断读取文件是否成功
if (m_hDIB == NULL)
{
// 失败,可能非BMP格式
CString strMsg;
strMsg = "读取图像时出错!可能是不支持该类型的图像文件!";
// 提示出错
//MessageBox(NULL, strMsg, "系统提示", MB_ICONINFORMATION | MB_OK);
file.Close();
// 返回FALSE
return ;
}
file.Close();
/////////////////////////////////////////////////
// 打开多光谱文件
//
CFile fileM;
CFileException feM;
if (!fileM.Open(lpszMulspecPath, CFile::modeRead | CFile::shareDenyWrite, &feM))
{
// 失败
ReportSaveLoadException(lpszMulspecPath, &feM,
FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
// 返回FALSE
return ;
}
// 尝试调用ReadDIBFile()读取图像
TRY
{
m_hMulDIB = ::ReadDIBFile(fileM);
}
CATCH (CFileException, eLoad)
{
// 读取失败
fileM.Abort();
// 恢复光标形状
EndWaitCursor();
// 报告失败
ReportSaveLoadException(lpszMulspecPath, eLoad,
FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
// 设置DIB为空
m_hMulDIB = NULL;
fileM.Close();
// 返回FALSE
return ;
}
END_CATCH
// 判断读取文件是否成功
if (m_hMulDIB == NULL)
{
// 失败,可能非BMP格式
CString strMsg;
strMsg = "读取图像时出错!可能是不支持该类型的图像文件!";
// 提示出错
//MessageBox(NULL, strMsg, "系统提示", MB_ICONINFORMATION | MB_OK);
fileM.Close();
// 返回FALSE
return ;
}
fileM.Close();
UpdateAllViews(NULL);
}
void CPixelFusionDoc::InitDIBData()
{
// 初始化DIB对象
// 判断调色板是否为空
if (m_palDIB != NULL)
{
// 删除调色板对象
delete m_palDIB;
// 重置调色板为空
m_palDIB = NULL;
}
// 如果DIB对象为空,直接返回
if (m_hDIB == NULL)
{
// 返回
return;
}
LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) m_hDIB);
// 判断图像是否过大
if (::DIBWidth(lpDIB) > INT_MAX ||::DIBHeight(lpDIB) > INT_MAX)
{
::GlobalUnlock((HGLOBAL) m_hDIB);
// 释放DIB对象
::GlobalFree((HGLOBAL) m_hDIB);
// 设置DIB为空
m_hDIB = NULL;
CString strMsg;
strMsg = "BMP图像太大!";
// 提示用户
MessageBox(NULL, strMsg, "系统提示", MB_ICONINFORMATION | MB_OK);
// 返回
return;
}
// 设置文档大小
m_sizeDoc = CSize((int) ::DIBWidth(lpDIB), (int) ::DIBHeight(lpDIB));
::GlobalUnlock((HGLOBAL) m_hDIB);
// 创建新调色板
m_palDIB = new CPalette;
// 判断是否创建成功
if (m_palDIB == NULL)
{
// 失败,可能是内存不足
::GlobalFree((HGLOBAL) m_hDIB);
// 设置DIB对象为空
m_hDIB = NULL;
// 返回
return;
}
// 调用CreateDIBPalette来创建调色板
if (::CreateDIBPalette(m_hDIB, m_palDIB) == NULL)
{
// 返回空,可能该DIB对象没有调色板
// 删除
delete m_palDIB;
// 设置为空
m_palDIB = NULL;
// 返回
return;
}
}
//+++++++++++++++++++++++++++++++++++++++++++++
//
// 相关系数法
//
// 求得全色图像和多光谱图像各波段的相关系数,用于融合
//
//+++++++++++++++++++++++++++++++++++++++++++++
void CPixelFusionDoc::OnCorrelationFusion()
{
// 存储文件对话框
CFileDialog dlg(FALSE, "Bmp", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
"BMP文件(*.bmp)|*.bmp|所有文件(*.*)|*.*||",NULL);
if (dlg.DoModal() == IDCANCEL)
return ;
// 获取存储文件路径
lpszResultPath = dlg.GetPathName();
// TODO: Add your command handler code here
BITMAPFILEHEADER bmfHeaderSpot,bmfHeaderMulspec;
BITMAPINFOHEADER bmiHeaderSpot,bmiHeaderMulspec;
////////////////////////////////////////////////
//获取高空间分辨率影像的信息
CFile fileSpot;
if(!fileSpot.Open(lpszSpotPath,CFile::modeRead))
{
AfxMessageBox("当前文件不能打开!",MB_OK);
return ;
}
//读文件头
if(fileSpot.Read(&bmfHeaderSpot,sizeof(BITMAPFILEHEADER)) != sizeof(BITMAPFILEHEADER))
return ;
//判断读入的文件是否是BMP文件
if(bmfHeaderSpot.bfType != DIB_HEADER_MARKER)
return ;
//读信息头
if(fileSpot.Read(&bmiHeaderSpot,sizeof(BITMAPINFOHEADER)) != sizeof(BITMAPINFOHEADER))
{
return ;
}
WORD wBitCountSpot = bmiHeaderSpot.biBitCount;
LONG lSpotWidth = bmiHeaderSpot.biWidth;
LONG lSpotHeight = bmiHeaderSpot.biHeight;
DWORD dwSpotSizeOff = bmfHeaderSpot.bfOffBits; //文件头到象素的偏移量
//计算高分辨率图象的行宽度
LONG lLineBytesSpot = WIDTHBYTES(bmiHeaderSpot.biWidth*bmiHeaderSpot.biBitCount*bmiHeaderSpot.biPlanes);
//判断图像是否满足SPOT全色影像的条件
if(wBitCountSpot != 8)
{
AfxMessageBox("SPOT全色图像必须为8-bit,请重新输入图像!",MB_OK);
return ;
}
fileSpot.Close();
////////////////////////////////////////////////
//获取高光谱分辨率影像的信息
CFile fileMulspec;
if(!fileMulspec.Open(lpszMulspecPath,CFile::modeRead))
{
AfxMessageBox("当前文件不能打开!",MB_OK);
return ;
}
//读文件头
if(fileMulspec.Read(&bmfHeaderMulspec,sizeof(BITMAPFILEHEADER)) != sizeof(BITMAPFILEHEADER))
return ;
//判断读入的文件是否是BMP文件
if(bmfHeaderMulspec.bfType != DIB_HEADER_MARKER)
return ;
//读信息头
if(fileMulspec.Read(&bmiHeaderMulspec,sizeof(BITMAPINFOHEADER)) != sizeof(BITMAPINFOHEADER))
{
return ;
}
WORD wBitCountMulspec = bmiHeaderMulspec.biBitCount;
LONG lMulspecWidth = bmiHeaderMulspec.biWidth;
LONG lMulspecHeight = bmiHeaderMulspec.biHeight;
DWORD dwMulspecSizeOff = bmfHeaderMulspec.bfOffBits; //文件头到象素的偏移量
//计算高分辨率图象的行宽度
LONG lLineBytesMulspec = WIDTHBYTES(bmiHeaderMulspec.biWidth*bmiHeaderMulspec.biBitCount*bmiHeaderMulspec.biPlanes);
int m = bmiHeaderMulspec.biWidth*bmiHeaderMulspec.biBitCount*bmiHeaderMulspec.biPlanes;
//判断图像是否满足SPOT全色影像的条件
if(wBitCountMulspec != 24)
{
AfxMessageBox("多光谱图像必须为24-bit,请重新输入图像!",MB_OK);
return ;
}
fileMulspec.Close();
LONG i,j;
////////////////////////////////////////////////
//判断两幅图像尺寸是否一致。
if((lSpotWidth != lMulspecWidth) || (lSpotHeight != lMulspecHeight))
{
AfxMessageBox("图像大小不匹配,请重新输入!");
return ;
}
/////////////////////////////////////////////////
//建立文件映射
//高空间分辨率图象
HANDLE hFileSpot = ::CreateFile(lpszSpotPath,GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,
OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
HANDLE hMapSpot = ::CreateFileMapping(hFileSpot,NULL,PAGE_READONLY,0,0,NULL);
if(hMapSpot != NULL && GetLastError() == ERROR_ALREADY_EXISTS)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -