📄 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 + -