?? dlgreg.cpp
字號:
// DlgReg.cpp : implementation file
//
#include "stdafx.h"
#include "ImageProcessing.h"
#include "ImageProcessingDoc.h"
#include "GlobalApi.h"
#include "DlgReg.h"
#include "DlgAftReg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDlgReg dialog
CDlgReg::CDlgReg(CWnd* pParent /*=NULL*/, CImageProcessingDoc* pDoc)
: CDialog(CDlgReg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDlgReg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// 獲取文檔類指針
m_pDoc = pDoc;
// 設置計算圖象位置標志位位FALSE
m_bCalImgLoc = FALSE;
// 設置基準圖象為原始打開的圖象
m_pDibInit = pDoc->m_pDibInit;
// 設置待配準圖象
m_pDibSamp = new CDib;
// 設置選取特征點的數目初始值
m_nChsFeatureNum = 0;
// 設置選取特征點的標志位為FALSE
m_bChoseFeature = FALSE;
}
void CDlgReg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDlgReg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDlgReg, CDialog)
//{{AFX_MSG_MAP(CDlgReg)
ON_WM_PAINT()
ON_BN_CLICKED(IDC_REG_OPEN, OnRegOpen)
ON_BN_CLICKED(IDC_REG_REG, OnRegReg)
ON_BN_CLICKED(IDC_REG_CHOSE_FEATURE, OnRegChoseFeature)
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDlgReg message handlers
void CDlgReg::OnPaint()
{
CPaintDC dc(this); // device context for painting
// 如果還沒有計算圖象的位置,則進行計算
if(!m_bCalImgLoc){
CalImageLocation();
}
CSize sizeDisplay;
CPoint pointDisplay;
// 顯示基準圖象
if(!m_pDibInit->IsEmpty()){
sizeDisplay.cx=m_pDibInit->m_lpBMIH->biWidth;
sizeDisplay.cy=m_pDibInit->m_lpBMIH->biHeight;
pointDisplay.x = m_rectInitImage.left;
pointDisplay.y = m_rectInitImage.top;
m_pDibInit->Draw(&dc,pointDisplay,sizeDisplay);
}
// 顯示待配準圖象
if(!m_pDibSamp->IsEmpty()){
sizeDisplay.cx=m_pDibSamp->m_lpBMIH->biWidth;
sizeDisplay.cy=m_pDibSamp->m_lpBMIH->biHeight;
pointDisplay.x = m_rectResltImage.left;
pointDisplay.y = m_rectResltImage.top;
m_pDibSamp->Draw(&dc,pointDisplay,sizeDisplay);
}
// 顯示特征點與配準的特征點
DrawFeature(&dc);
}
/*************************************************************************
*
* \函數名稱:
* CalImageLocation()
*
* \輸入參數:
* 無
*
* \返回值:
* 無
*
* \說明:
* 該函數設置對話框中的控件位置和大小,并設置顯示圖象的位置。默認的圖象大小為352×288,如果圖象小于
*此大小,則控件大小設置為352×288,并將圖象放置在控件中間。
*
*************************************************************************
*/
void CDlgReg::CalImageLocation()
{
// 獲得控件IDC_REG_INIT_IMAGE的句柄,并獲得控件的初始位置信息
CWnd* pWnd=GetDlgItem(IDC_REG_INIT_IMAGE);
WINDOWPLACEMENT *winPlacement;
winPlacement=new WINDOWPLACEMENT;
pWnd->GetWindowPlacement(winPlacement);
// 圖象寬度
int nImageWidth;
nImageWidth = m_pDibInit->m_lpBMIH->biWidth;
// 圖象高度
int nImageHeight;
nImageHeight = m_pDibInit->m_lpBMIH->biHeight;
// 調整控件IDC_REG_INIT_IMAGE的大小位置,并同時設置顯示基準圖象的位置
if(nImageHeight > 352){
winPlacement->rcNormalPosition.bottom = winPlacement->rcNormalPosition.top + nImageHeight;
m_rectInitImage.bottom = winPlacement->rcNormalPosition.bottom;
m_rectInitImage.top = winPlacement->rcNormalPosition.top;
}
else{
winPlacement->rcNormalPosition.bottom = winPlacement->rcNormalPosition.top + 352;
m_rectInitImage.bottom = winPlacement->rcNormalPosition.top + 176 + nImageHeight/2;
m_rectInitImage.top = winPlacement->rcNormalPosition.top + 176 - nImageHeight/2;
}
if(nImageWidth > 288){
winPlacement->rcNormalPosition.right = winPlacement->rcNormalPosition.left + nImageWidth;
m_rectInitImage.right = winPlacement->rcNormalPosition.right;
m_rectInitImage.left = winPlacement->rcNormalPosition.left;
}
else{
winPlacement->rcNormalPosition.right = winPlacement->rcNormalPosition.left + 288;
m_rectInitImage.right = winPlacement->rcNormalPosition.left + 144 + nImageWidth/2;
m_rectInitImage.left = winPlacement->rcNormalPosition.left + 144 - nImageWidth/2;
}
// 設置IDC_REG_INIT_IMAGE控件的大小位置
pWnd->SetWindowPlacement(winPlacement);
// 獲得顯示基準圖象控件的右邊位置,以便確認顯示待配準圖象控件的位置
int nIniImgRight;
nIniImgRight = winPlacement->rcNormalPosition.right;
int nIniImgLeft;
nIniImgLeft = winPlacement->rcNormalPosition.left;
// 獲得IDC_REG_INIT_IMAGE控件的下邊位置,以便調整其他控件的位置
int nIniImgBottom;
nIniImgBottom = winPlacement->rcNormalPosition.bottom;
// 獲得控件IDC_REG_RESLT_IMAGE的句柄,并獲得初始位置信息
pWnd=GetDlgItem(IDC_REG_RESLT_IMAGE);
pWnd->GetWindowPlacement(winPlacement);
// 如果還未打開待配準圖象,則設置待配準圖象大小和基準圖象大小相等
if(!m_pDibSamp->IsEmpty()){
nImageWidth = m_pDibSamp->m_lpBMIH->biWidth;
nImageHeight = m_pDibSamp->m_lpBMIH->biHeight;
}
// 調整控件IDC_REG_RESLT_IMAGE的大小位置,并同時設置顯示待配準圖象的位置
// 先調整控件的左邊位置,和IDC_REG_INIT_IMAGE控件相隔15個象素
winPlacement->rcNormalPosition.left = nIniImgRight + 15;
if(nImageHeight > 352){
winPlacement->rcNormalPosition.bottom = winPlacement->rcNormalPosition.top + nImageHeight;
m_rectResltImage.bottom = winPlacement->rcNormalPosition.bottom;
m_rectResltImage.top = winPlacement->rcNormalPosition.top;
}
else{
winPlacement->rcNormalPosition.bottom = winPlacement->rcNormalPosition.top + 352;
m_rectResltImage.bottom = winPlacement->rcNormalPosition.top + 176 + nImageHeight/2;
m_rectResltImage.top = winPlacement->rcNormalPosition.top + 176 - nImageHeight/2;
}
if(nImageWidth > 288){
winPlacement->rcNormalPosition.right = winPlacement->rcNormalPosition.left + nImageWidth;
m_rectResltImage.right = winPlacement->rcNormalPosition.right;
m_rectResltImage.left = winPlacement->rcNormalPosition.left;
}
else{
winPlacement->rcNormalPosition.right = winPlacement->rcNormalPosition.left + 288;
m_rectResltImage.right = winPlacement->rcNormalPosition.left + 144 + nImageWidth/2;
m_rectResltImage.left = winPlacement->rcNormalPosition.left + 144 - nImageWidth/2;
}
// 設置IDC_REG_RESLT_IMAGE控件的大小位置
pWnd->SetWindowPlacement(winPlacement);
if(nIniImgBottom < winPlacement->rcNormalPosition.bottom)
nIniImgBottom = winPlacement->rcNormalPosition.bottom;
nIniImgBottom = winPlacement->rcNormalPosition.bottom;
nIniImgRight = winPlacement->rcNormalPosition.right;
// 設置控件IDOK的位置大小
pWnd=GetDlgItem(IDOK);
pWnd->GetWindowPlacement(winPlacement);
winPlacement->rcNormalPosition.top = nIniImgBottom +15;
winPlacement->rcNormalPosition.bottom = nIniImgBottom + 60;
pWnd->SetWindowPlacement(winPlacement);
// 設置控件IDCANCEL的位置大小
pWnd=GetDlgItem(IDCANCEL);
pWnd->GetWindowPlacement(winPlacement);
winPlacement->rcNormalPosition.top = nIniImgBottom +15;
winPlacement->rcNormalPosition.bottom = nIniImgBottom + 60;
pWnd->SetWindowPlacement(winPlacement);
// 設置控件IDC_REG_OPEN的位置大小
pWnd=GetDlgItem(IDC_REG_OPEN);
pWnd->GetWindowPlacement(winPlacement);
winPlacement->rcNormalPosition.top = nIniImgBottom +15;
winPlacement->rcNormalPosition.bottom = nIniImgBottom + 60;
pWnd->SetWindowPlacement(winPlacement);
// 設置控件IDC_REG_REG的位置大小
pWnd=GetDlgItem(IDC_REG_REG);
pWnd->GetWindowPlacement(winPlacement);
winPlacement->rcNormalPosition.top = nIniImgBottom +15;
winPlacement->rcNormalPosition.bottom = nIniImgBottom + 60;
pWnd->SetWindowPlacement(winPlacement);
// 設置控件IDC_REG_CHOSE_FEATUR的位置大小
pWnd=GetDlgItem(IDC_REG_CHOSE_FEATURE);
pWnd->GetWindowPlacement(winPlacement);
winPlacement->rcNormalPosition.top = nIniImgBottom +15;
winPlacement->rcNormalPosition.bottom = nIniImgBottom + 60;
pWnd->SetWindowPlacement(winPlacement);
// 調整此對話框的大小
//pWnd = GetDlgItem(IDD_DLG_REG);
this->GetWindowPlacement(winPlacement);
//winPlacement->rcNormalPosition.top = nIniImgBottom +15;
winPlacement->rcNormalPosition.bottom = nIniImgBottom + 300;
winPlacement->rcNormalPosition.left = nIniImgLeft - 20;
winPlacement->rcNormalPosition.right = nIniImgRight + 20;
this->SetWindowPlacement(winPlacement);
// 釋放已分配內存
delete winPlacement;
// 設置計算圖象控件位置標志位為TRUE
m_bCalImgLoc = TRUE;
}
/*************************************************************************
*
* \函數名稱:
* DrawFeature()
*
* \輸入參數:
* 無
*
* \返回值:
* 無
*
* \說明:
* 該函數根據類的成員變量確定特征點的數目和位置,并在圖象中進行顯示。
*
*************************************************************************
*/
void CDlgReg::DrawFeature(CDC* pDC)
{
// 循環變量
int i;
// 臨時變量
CPoint pointTemp;
// 半徑
int nRadius;
nRadius = 5;
// 設置畫圖類型
pDC->SelectStockObject(HOLLOW_BRUSH);
// 聲明畫筆
CPen penWhite(PS_SOLID,1,RGB(255,255,255));
CPen *pOldPen;
// 將畫筆選入,并保存以前的畫筆
pOldPen = pDC->SelectObject(&penWhite);
for(i=0; i<m_nChsFeatureNum; i++){
// 首先顯示特征點
// 確定此點的顯示位置
pointTemp.x = m_pPointSampl[i].x + m_rectResltImage.left;
pointTemp.y = m_pPointSampl[i].y + m_rectResltImage.top ;
// 畫出此特征點,其中園的半徑為nRadius
CRect rectSamp(pointTemp.x-nRadius , pointTemp.y-nRadius ,
pointTemp.x+nRadius , pointTemp.y+nRadius);
pDC->Ellipse(rectSamp);
// 再顯示配準特征點
// 確定此點的顯示位置
pointTemp.x = m_pPointBase[i].x + m_rectInitImage.left;
pointTemp.y = m_pPointBase[i].y + m_rectInitImage.top ;
// 畫出此特征點,其中園的半徑為nRadius
CRect rectBase(pointTemp.x-nRadius , pointTemp.y-nRadius ,
pointTemp.x+nRadius , pointTemp.y+nRadius);
pDC->Ellipse(rectBase);
}
// 回復以前的畫筆
pDC->SelectObject(pOldPen);
penWhite.DeleteObject();
}
/*************************************************************************
*
* \函數名稱:
* OnRegOpen()
*
* \輸入參數:
* 無
*
* \返回值:
* 無
*
* \說明:
* 該函數打開待配準圖象,并將圖象存放在m_pDibSamp中。
*
*************************************************************************
*/
void CDlgReg::OnRegOpen()
{
// TODO: Add your control notification handler code here
CFileDialog dlg(TRUE,"bmp","*.bmp");
if(dlg.DoModal() == IDOK)
{
CFile file;
CString strPathName;
strPathName = dlg.GetPathName();
// 打開文件
if( !file.Open(strPathName, CFile::modeRead | CFile::shareDenyWrite))
{
// 返回
return ;
}
// 讀入模板圖象
if(!m_pDibSamp->Read(&file)){
// 恢復光標形狀
EndWaitCursor();
// 清空已分配內存
m_pDibSamp->Empty();
// 返回
return;
}
}
// 判斷是否是8-bpp位圖(這里為了方便,只處理8-bpp位圖的模板配準,其它的可以類推)
if(m_pDibSamp->m_nColorTableEntries != 256)
{
// 提示用戶
MessageBox("目前只支持256色位圖!", "系統提示" , MB_ICONINFORMATION | MB_OK);
// 清空已分配內存
m_pDibSamp->Empty();
// 返回
return;
}
// 如果打開新的待配準文件,將圖象位置設置標志位設為FALSE,以便再次調整位置
m_bCalImgLoc = FALSE;
// 更新顯示
this->UpdateData();
this->Invalidate();
}
/*************************************************************************
*
* \函數名稱:
* OnRegChoseFeatureOnRegReg()
*
* \輸入參數:
* 無
*
* \返回值:
* 無
*
* \說明:
* 該函數設置選取特征點標志位,然后調用函數在待配準圖象中選取特征點,并
*配準這些特征點。特征點的數目至少應該選取三個。
*
*************************************************************************
*/
void CDlgReg::OnRegChoseFeature()
{
// 如果待配準圖象尚未打開,則不能進行特征點選取工作
if((m_pDibSamp->IsEmpty())){
AfxMessageBox("尚未打開待配準圖象文件,請打開待配準圖象");
return;
}
// 設置選取特征點標志位
m_bChoseFeature = TRUE;
AfxMessageBox("請在待配準圖象中選取特征點");
}
/*************************************************************************
*
* \函數名稱:
* OnLButtonUp()
*
* \輸入參數:
* 無
*
* \返回值:
* 無
*
* \說明:
* 該函數根據鼠標所標定的位置設置特征點。然后調用特征配準函數配準此特征點。
*特征點的數目至少應該選取三個。
*
*************************************************************************
*/
void CDlgReg::OnLButtonUp(UINT nFlags, CPoint point)
{
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -