?? dlgrecmatch.cpp
字號:
pWnd=GetDlgItem(IDC_RECOG_MATCH);
pWnd->GetWindowPlacement(winPlacement);
winPlacement->rcNormalPosition.top = nIniImgBottom +15;
winPlacement->rcNormalPosition.bottom = nIniImgBottom + 60;
pWnd->SetWindowPlacement(winPlacement);
// 調整此對話框的大小
this->GetWindowPlacement(winPlacement);
//winPlacement->rcNormalPosition.top = nIniImgtop -50;
winPlacement->rcNormalPosition.bottom = nIniImgBottom + 272;
winPlacement->rcNormalPosition.left = nIniImgLeft - 20;
winPlacement->rcNormalPosition.right = nIniImgRight + 20;
this->SetWindowPlacement(winPlacement);
// 釋放已分配內存
delete winPlacement;
// 設置計算圖象控件位置標志位為TRUE
m_bCalImgLoc = TRUE;
}
void CDlgRecMatch::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_pDibModel->IsEmpty()){
sizeDisplay.cx=m_pDibModel->m_lpBMIH->biWidth;
sizeDisplay.cy=m_pDibModel->m_lpBMIH->biHeight;
pointDisplay.x = m_rectModelImage.left;
pointDisplay.y = m_rectModelImage.top;
m_pDibModel->Draw(&dc,pointDisplay,sizeDisplay);
}
// 顯示結果圖象
if(!m_pDibResult->IsEmpty()){
sizeDisplay.cx=m_pDibResult->m_lpBMIH->biWidth;
sizeDisplay.cy=m_pDibResult->m_lpBMIH->biHeight;
pointDisplay.x = m_rectResltImage.left;
pointDisplay.y = m_rectResltImage.top;
m_pDibResult->Draw(&dc,pointDisplay,sizeDisplay);
}
}
/*************************************************************************
*
* \函數名稱:
* TemplateMatch()
*
* \輸入參數:
* CDib* pDibSrc - 指向CDib類的指針,含有待匹配圖象信息
* CDib* pDibTemplate - 指向CDib類的指針,含有模板圖象信息
*
* \返回值:
* BOOL - 成功則返回TRUE,否則返回FALSE
*
* \說明:
* 該函數將對圖象進行模板匹配操作。需要注意的是,此程序只處理256灰度級的
*圖象。
*
*************************************************************************
*/
BOOL CDlgRecMatch::TemplateMatch(CDib* pDibSrc, CDib* pDibTemplate)
{
// 指向源圖像的指針
LPBYTE lpSrc,lpTemplateSrc;
// 指向緩存圖像的指針
LPBYTE lpDst;
//循環變量
long i;
long j;
long m;
long n;
//中間結果
double dSigmaST;
double dSigmaS;
double dSigmaT;
//相似性測度
double R;
//最大相似性測度
double dbMaxR;
//最大相似性出現位置
int nMaxWidth;
int nMaxHeight;
//像素值
unsigned char unchPixel;
unsigned char unchTemplatePixel;
// 獲得圖象數據存儲的高度和寬度
CSize sizeSaveImage;
sizeSaveImage = pDibSrc->GetDibSaveDim();
// 獲得模板圖象數據存儲的高度和寬度
CSize sizeSaveTemplate;
sizeSaveTemplate = pDibTemplate->GetDibSaveDim();
// 暫時分配內存,以保存新圖像
CDib* pDibNew;
pDibNew = new CDib;
// 如果分配內存失敗,則推出
if(!CopyDIB(pDibSrc,pDibNew)){
// 釋放已分配內存
pDibNew->Empty();
// 返回
return FALSE;
}
// 初始化新分配的內存
lpDst = (LPBYTE)pDibNew->m_lpImage;
// 圖象的高度
int nImageHeight ;
nImageHeight = pDibSrc->m_lpBMIH->biHeight;
// 圖象的寬度
int nImageWidth;
nImageWidth = pDibSrc->m_lpBMIH->biWidth;
// 模板圖象的高度
int nTemplateHeight;
nTemplateHeight = pDibTemplate->m_lpBMIH->biHeight;
// 模板圖象的寬度
int nTemplateWidth;
nTemplateWidth = pDibTemplate->m_lpBMIH->biWidth;
//計算dSigmaT
dSigmaT = 0;
for (n = 0;n < nTemplateHeight ;n++)
{
for(m = 0;m < nTemplateWidth ;m++)
{
// 指向模板圖像倒數第j行,第i個象素的指針
lpTemplateSrc = (LPBYTE)pDibTemplate->m_lpImage + sizeSaveTemplate.cx * n + m;
unchTemplatePixel = (unsigned char)*lpTemplateSrc;
dSigmaT += (double)unchTemplatePixel*unchTemplatePixel;
}
}
//找到圖像中最大相似性的出現位置
dbMaxR = 0.0;
for (j = 0;j < nImageHeight - nTemplateHeight +1 ;j++)
{
for(i = 0;i < nImageWidth - nTemplateWidth + 1;i++)
{
dSigmaST = 0;
dSigmaS = 0;
for (n = 0;n < nTemplateHeight ;n++)
{
for(m = 0;m < nTemplateWidth ;m++)
{
// 指向源圖像倒數第j+n行,第i+m個象素的指針
lpSrc = (LPBYTE)pDibSrc->m_lpImage + sizeSaveImage.cx * (j+n) + (i+m);
// 指向模板圖像倒數第n行,第m個象素的指針
lpTemplateSrc = (LPBYTE)pDibTemplate->m_lpImage + sizeSaveTemplate.cx * n + m;
unchPixel = (unsigned char)*lpSrc;
unchTemplatePixel = (unsigned char)*lpTemplateSrc;
dSigmaS += (double)unchPixel*unchPixel;
dSigmaST += (double)unchPixel*unchTemplatePixel;
}
}
//計算相似性
R = dSigmaST / ( sqrt(dSigmaS)*sqrt(dSigmaT));
//與最大相似性比較
if (R > dbMaxR)
{
dbMaxR = R;
nMaxWidth = i;
nMaxHeight = j;
}
}
}
// 對目標圖象的象素進行賦值
for(i=0; i<nImageHeight; i++)
for( j=0; j<nImageWidth; j++){
lpDst[i*sizeSaveImage.cx +j] /=2;
}
//將最大相似性出現區域部分復制到目標圖像
for (n = 0;n < nTemplateHeight ;n++)
{
for(m = 0;m < nTemplateWidth ;m++)
{
lpTemplateSrc = (LPBYTE)pDibTemplate->m_lpImage + sizeSaveTemplate.cx * n + m;
lpDst = (LPBYTE)pDibNew->m_lpImage + sizeSaveImage.cx * (n+nMaxHeight) + (m+nMaxWidth);
*lpDst = *lpTemplateSrc;
}
}
// 復制圖像
memcpy(pDibSrc->m_lpImage, pDibNew->m_lpImage, nImageWidth * nImageHeight);
// 釋放內存
pDibNew->Empty();
// 返回
return TRUE;
}
/*************************************************************************
*
* \函數名稱:
* OnRecogMatch()
*
* \輸入參數:
* 無
*
* \返回值:
* 無
*
* \說明:
* 根據圖象模板,在待匹配的圖象中找到匹配的位置
*
*************************************************************************
*/
void CDlgRecMatch::OnRecogMatch()
{
// 更改光標形狀
BeginWaitCursor();
// 分配結果圖象內存
if(!m_pDibInit->IsEmpty()){
// 如果分配內存失敗,則推出
if(!CopyDIB(m_pDibInit,m_pDibResult)){
// 釋放已分配內存
m_pDibResult->Empty();
// 返回
return ;
}
}
// 設置計算圖象控件位置標志位為FALSE以重新設置圖象控件位置
m_bCalImgLoc = FALSE;
// 調用TemplateMatch()函數進行模板匹配
if (TemplateMatch(m_pDibResult, m_pDibModel))
{
// 更新顯示
Invalidate();
}
else
{
// 提示用戶
MessageBox("分配內存失敗!", "系統提示" , MB_ICONINFORMATION | MB_OK);
}
// 恢復光標
EndWaitCursor();
// 更新視圖
Invalidate();
}
void CDlgRecMatch::OnOK()
{
// TODO: Add extra validation here
// 釋放已分配內存
if(!m_pDibModel->IsEmpty()){
m_pDibModel->Empty();
m_pDibModel = NULL;
}
if(!m_pDibResult->IsEmpty()){
m_pDibResult->Empty();
m_pDibResult = NULL;
}
CDialog::OnOK();
}
void CDlgRecMatch::OnCancel()
{
// TODO: Add extra cleanup here
// 釋放已分配內存
if(!m_pDibModel->IsEmpty()){
m_pDibModel->Empty();
m_pDibModel = NULL;
}
if(!m_pDibResult->IsEmpty()){
m_pDibResult->Empty();
m_pDibResult = NULL;
}
CDialog::OnCancel();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -