www.pudn.com > Face3DModel.zip > CornerMatchTestDlg.cpp
// CornerMatchTestDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Face3DModel.h"
#include "CornerMatchTestDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define CORNER_RADIUS 1
/////////////////////////////////////////////////////////////////////////////
// CCornerMatchTestDlg dialog
CCornerMatchTestDlg::CCornerMatchTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCornerMatchTestDlg::IDD, pParent)
, m_pbDibBits1(NULL)
, m_pbDibBits2(NULL)
, m_pbMask1(NULL)
, m_pbMask2(NULL)
, m_fMacthCancel(FALSE)
{
//{{AFX_DATA_INIT(CCornerMatchTestDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
CCornerMatchTestDlg::~CCornerMatchTestDlg()
{
if (m_pbDibBits1 != NULL)
{
delete [] m_pbDibBits1;
m_pbDibBits1 = NULL;
}
if (m_pbDibBits2 != NULL)
{
delete [] m_pbDibBits2;
m_pbDibBits2 = NULL;
}
if (m_pbMask1 != NULL)
{
delete [] m_pbMask1;
m_pbMask1 = NULL;
}
if (m_pbMask2 != NULL)
{
delete [] m_pbMask2;
m_pbMask2 = NULL;
}
}
void CCornerMatchTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCornerMatchTestDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCornerMatchTestDlg, CDialog)
//{{AFX_MSG_MAP(CCornerMatchTestDlg)
ON_WM_PAINT()
ON_BN_CLICKED(IDC_LOAD_FIRST, OnLoadFirst)
ON_BN_CLICKED(IDC_LOAD_SECOND, OnLoadSecond)
ON_BN_CLICKED(IDC_DETECT_FIRST, OnDetectFirst)
ON_BN_CLICKED(IDC_DETECT_SECOND, OnDetectSecond)
ON_BN_CLICKED(IDC_MATCH, OnMatch)
ON_BN_CLICKED(IDC_FALSE_CANCELLATION, OnFalseCancellation)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCornerMatchTestDlg message handlers
void CCornerMatchTestDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
RECT rect;
GetClientRect(&rect);
CPen penRed(PS_SOLID, 1, RGB(255, 0, 0));
CPen penBlue(PS_SOLID, 1, RGB(0, 0, 255));
CBrush brush(RGB(255, 0, 0));
CPen *oldPen = dc.SelectObject(&penRed);
CBrush *oldBrush = dc.SelectObject(&brush);
int oldMode = dc.SetStretchBltMode(COLORONCOLOR);
if (m_pbDibBits1 != NULL)
{
RECT rcIn = rect;
rcIn.right = rect.right / 2;
rcIn.bottom = rect.bottom - 40;
RECT rcOut = rcIn;
ComputeRenderRect(rcIn, rcOut, m_bmpInfoHeader1.biWidth, m_bmpInfoHeader1.biHeight);
StretchDIBits(dc.m_hDC,
rcOut.left, rcOut.top,
rcOut.right - rcOut.left, rcOut.bottom - rcOut.top,
0, 0, m_bmpInfoHeader1.biWidth, m_bmpInfoHeader1.biHeight,
m_pbDibBits1,
reinterpret_cast(&m_bmpInfoHeader1),
DIB_RGB_COLORS,
SRCCOPY);
double dbRatio = (rcOut.right - rcOut.left) * 1.0 / m_bmpInfoHeader1.biWidth;
if (m_fMacthCancel == FALSE)
{
for (int i = 0; i < m_vecCorners1.size(); i++)
{
int x = ((int)(m_vecCorners1[i].x * dbRatio)) + rcOut.left;
int y = ((int)((m_bmpInfoHeader1.biHeight - 1 - m_vecCorners1[i].y) * dbRatio)) + rcOut.top;
dc.Ellipse(x - CORNER_RADIUS, y - CORNER_RADIUS, x + CORNER_RADIUS, y + CORNER_RADIUS);
}
}
else
{
for (int i = 0; i < m_vecMatchCorners.size(); i++)
{
int x = ((int)(m_vecMatchCorners[i].ptCorner1.x * dbRatio)) + rcOut.left;
int y = ((int)((m_bmpInfoHeader1.biHeight - 1 - m_vecMatchCorners[i].ptCorner1.y) * dbRatio)) + rcOut.top;
dc.Ellipse(x - CORNER_RADIUS, y - CORNER_RADIUS, x + CORNER_RADIUS, y + CORNER_RADIUS);
}
}
dc.SelectObject(&penBlue);
for (int i = 0; i < m_vecMatchCorners.size(); i++)
{
int x1 = ((int)(m_vecMatchCorners[i].ptCorner1.x * dbRatio)) + rcOut.left;
int y1 = ((int)((m_bmpInfoHeader1.biHeight - 1 - m_vecMatchCorners[i].ptCorner1.y) * dbRatio)) + rcOut.top;
int x2 = ((int)(m_vecMatchCorners[i].ptCorner2.x * dbRatio)) + rcOut.left;
int y2 = ((int)((m_bmpInfoHeader1.biHeight - 1 - m_vecMatchCorners[i].ptCorner2.y) * dbRatio)) + rcOut.top;
dc.MoveTo(x1, y1);
dc.LineTo(x2, y2);
}
dc.SelectObject(&penRed);
}
if (m_pbDibBits2 != NULL)
{
RECT rcIn = rect;
rcIn.left = rect.right / 2;
rcIn.bottom = rect.bottom - 40;
RECT rcOut = rcIn;
ComputeRenderRect(rcIn, rcOut, m_bmpInfoHeader2.biWidth, m_bmpInfoHeader2.biHeight);
StretchDIBits(dc.m_hDC,
rcOut.left, rcOut.top,
rcOut.right - rcOut.left, rcOut.bottom - rcOut.top,
0, 0, m_bmpInfoHeader2.biWidth, m_bmpInfoHeader2.biHeight,
m_pbDibBits2,
reinterpret_cast(&m_bmpInfoHeader2),
DIB_RGB_COLORS,
SRCCOPY);
double dbRatio = (rcOut.right - rcOut.left) * 1.0 / m_bmpInfoHeader2.biWidth;
if (m_fMacthCancel == FALSE)
{
for (int i = 0; i < m_vecCorners2.size(); i++)
{
int x = ((int)(m_vecCorners2[i].x * dbRatio)) + rcOut.left;
int y = ((int)((m_bmpInfoHeader2.biHeight - 1 - m_vecCorners2[i].y) * dbRatio)) + rcOut.top;
dc.Ellipse(x - CORNER_RADIUS, y - CORNER_RADIUS, x + CORNER_RADIUS, y + CORNER_RADIUS);
}
}
else
{
for (int i = 0; i < m_vecMatchCorners.size(); i++)
{
int x = ((int)(m_vecMatchCorners[i].ptCorner2.x * dbRatio)) + rcOut.left;
int y = ((int)((m_bmpInfoHeader2.biHeight - 1 - m_vecMatchCorners[i].ptCorner2.y) * dbRatio)) + rcOut.top;
dc.Ellipse(x - CORNER_RADIUS, y - CORNER_RADIUS, x + CORNER_RADIUS, y + CORNER_RADIUS);
}
}
dc.SelectObject(&penBlue);
for (int i = 0; i < m_vecMatchCorners.size(); i++)
{
int x1 = ((int)(m_vecMatchCorners[i].ptCorner1.x * dbRatio)) + rcOut.left;
int y1 = ((int)((m_bmpInfoHeader2.biHeight - 1 - m_vecMatchCorners[i].ptCorner1.y) * dbRatio)) + rcOut.top;
int x2 = ((int)(m_vecMatchCorners[i].ptCorner2.x * dbRatio)) + rcOut.left;
int y2 = ((int)((m_bmpInfoHeader2.biHeight - 1 - m_vecMatchCorners[i].ptCorner2.y) * dbRatio)) + rcOut.top;
dc.MoveTo(x2, y2);
dc.LineTo(x1, y1);
}
dc.SelectObject(&penRed);
}
dc.SelectObject(oldPen);
dc.SelectObject(oldBrush);
dc.SetStretchBltMode(oldMode);
}
void CCornerMatchTestDlg::ComputeRenderRect(RECT &rcIn, RECT &rcOut, int width, int height)
{
int nInWidth = rcIn.right - rcIn.left;
int nInHeight = rcIn.bottom - rcIn.top;
if (nInWidth >= width && nInWidth >= height)
{
rcOut.left = rcIn.left + (nInWidth - width) / 2;
rcOut.right = rcOut.left + width;
rcOut.top = rcIn.top + (nInHeight - height) / 2;
rcOut.bottom = rcOut.top + height;
}
else if (width * nInHeight >= height * nInWidth)
{
rcOut.left = rcIn.left;
rcOut.right = rcIn.right;
int nOutHeight = height * nInWidth / width;
rcOut.top = rcIn.top + (nInHeight - nOutHeight) / 2;
rcOut.bottom = rcOut.top + nOutHeight;
}
else
{
rcOut.top = rcIn.top;
rcOut.bottom = rcIn.bottom;
int nOutWidth = width * nInHeight / height;
rcOut.left = rcIn.left + (nInWidth - nOutWidth) / 2;
rcOut.right = rcOut.left + nOutWidth;
}
}
void CCornerMatchTestDlg::OnLoadFirst()
{
if (LoadBitmap(m_bmpInfoHeader1, &m_pbDibBits1))
{
Invalidate();
}
}
void CCornerMatchTestDlg::OnLoadSecond()
{
if (LoadBitmap(m_bmpInfoHeader2, &m_pbDibBits2))
{
Invalidate();
}
}
BOOL CCornerMatchTestDlg::LoadBitmap(BITMAPINFOHEADER &bmpInfoHeader, BYTE **ppbDibBits)
{
static OPENFILENAME ofn={0};
TCHAR szFilename[MAX_PATH] = _T("");
// Fill in standard structure fields
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = GetSafeHwnd();
ofn.lpstrFilter = _T("Bitmap Files (*.bmp)\0*.bmp\0All Files (*.*)\0*.*\0\0");
ofn.lpstrCustomFilter = NULL;
ofn.nFilterIndex = 1;
ofn.lpstrFile = szFilename;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrTitle = TEXT("Load Bitmap File...\0");
ofn.lpstrFileTitle = NULL;
ofn.lpstrDefExt = TEXT("*\0");
ofn.Flags = OFN_FILEMUSTEXIST | OFN_READONLY | OFN_PATHMUSTEXIST;
ofn.lpstrInitialDir = NULL;
// Create the standard file open dialog and return its result
if (GetOpenFileName((LPOPENFILENAME)&ofn))
{
CFile file;
if (file.Open(szFilename, CFile::modeRead, NULL))
{
BITMAPFILEHEADER bmpFileHeader;
UINT size = sizeof(BITMAPFILEHEADER);
if (size == file.Read(&bmpFileHeader, size) && bmpFileHeader.bfType == 0x4d42)
{
BITMAPINFOHEADER infoHeader;
size = sizeof(BITMAPINFOHEADER);
if (size == file.Read(&infoHeader, size) && infoHeader.biBitCount == 24
&& (infoHeader.biWidth * 3 % 4) == 0)
{
if (*ppbDibBits != NULL)
{
delete [] (*ppbDibBits);
*ppbDibBits = NULL;
}
size = infoHeader.biWidth * infoHeader.biHeight * 3;
*ppbDibBits = new BYTE[size];
if (size == file.Read(*ppbDibBits, size))
{
bmpInfoHeader = infoHeader;
file.Close();
return TRUE;
}
}
}
file.Close();
}
MessageBox(_T("Load Bitmap Failed! Bad bitmap format or not 24bit true color bitmap!"));
}
return FALSE;
}
void CCornerMatchTestDlg::OnDetectFirst()
{
if (m_pbDibBits1 != NULL)
{
m_vecCorners1.clear();
CornerDetection(m_bmpInfoHeader1.biWidth, m_bmpInfoHeader1.biHeight
, m_pbDibBits1, m_pbMask1, m_vecCorners1);
m_vecMatchCorners.clear();
m_fMacthCancel = FALSE;
Invalidate();
}
}
void CCornerMatchTestDlg::OnDetectSecond()
{
if (m_pbDibBits2 != NULL)
{
m_vecCorners2.clear();
CornerDetection(m_bmpInfoHeader2.biWidth, m_bmpInfoHeader2.biHeight
, m_pbDibBits2, m_pbMask2, m_vecCorners2);
m_vecMatchCorners.clear();
m_fMacthCancel = FALSE;
Invalidate();
}
}
void CCornerMatchTestDlg::OnMatch()
{
if (m_pbDibBits1 != NULL && m_pbDibBits2 != NULL)
{
ASSERT(m_bmpInfoHeader1.biWidth == m_bmpInfoHeader2.biWidth
&& m_bmpInfoHeader1.biHeight == m_bmpInfoHeader2.biHeight);
CornerMatch(m_bmpInfoHeader1.biWidth
, m_bmpInfoHeader1.biHeight
, m_pbDibBits1
, m_pbDibBits2
, m_vecCorners1
, m_vecCorners2
, m_vecMatchCorners);
m_fMacthCancel = FALSE;
Invalidate();
}
}
void CCornerMatchTestDlg::OnFalseCancellation()
{
if (m_pbDibBits1 != NULL && m_pbDibBits2 != NULL)
{
ASSERT(m_bmpInfoHeader1.biWidth == m_bmpInfoHeader2.biWidth
&& m_bmpInfoHeader1.biHeight == m_bmpInfoHeader2.biHeight);
double thresholdLevel = 1;
// for (int i = 0; i < 1; i++, thresholdLevel *= 2)
// {
MatchCancel(m_bmpInfoHeader1.biWidth
, m_bmpInfoHeader1.biHeight
, m_pbDibBits1
, m_pbDibBits2
, m_vecMatchCorners
, thresholdLevel);
// }
m_fMacthCancel = TRUE;
Invalidate();
}
}
void CCornerMatchTestDlg::SetBitmap1(BITMAPINFOHEADER &bmpInfoHeader1, BYTE *pbDibBits1, BYTE *pbMask1)
{
ASSERT(bmpInfoHeader1.biBitCount == 24 && pbDibBits1 != NULL);
m_bmpInfoHeader1 = bmpInfoHeader1;
if (m_pbDibBits1 != NULL)
{
delete [] m_pbDibBits1;
m_pbDibBits1 = NULL;
}
m_pbDibBits1 = new BYTE[bmpInfoHeader1.biWidth * bmpInfoHeader1.biHeight * 3];
memcpy(m_pbDibBits1, pbDibBits1, bmpInfoHeader1.biWidth * bmpInfoHeader1.biHeight * 3);
if (m_pbMask1 != NULL)
{
delete [] m_pbMask1;
m_pbMask1 = NULL;
}
if (pbMask1 != NULL)
{
m_pbMask1 = new BYTE[bmpInfoHeader1.biWidth * bmpInfoHeader1.biHeight];
memcpy(m_pbMask1, pbMask1, bmpInfoHeader1.biWidth * bmpInfoHeader1.biHeight);
}
}
void CCornerMatchTestDlg::SetBitmap2(BITMAPINFOHEADER &bmpInfoHeader2, BYTE *pbDibBits2, BYTE *pbMask2)
{
ASSERT(bmpInfoHeader2.biBitCount == 24 && pbDibBits2 != NULL);
m_bmpInfoHeader2 = bmpInfoHeader2;
if (m_pbDibBits2 != NULL)
{
delete [] m_pbDibBits2;
m_pbDibBits2 = NULL;
}
m_pbDibBits2 = new BYTE[bmpInfoHeader2.biWidth * bmpInfoHeader2.biHeight * 3];
memcpy(m_pbDibBits2, pbDibBits2, bmpInfoHeader2.biWidth * bmpInfoHeader2.biHeight * 3);
if (m_pbMask2 != NULL)
{
delete [] m_pbMask2;
m_pbMask2 = NULL;
}
if (pbMask2 != NULL)
{
m_pbMask2 = new BYTE[bmpInfoHeader2.biWidth * bmpInfoHeader2.biHeight];
memcpy(m_pbMask2, pbMask2, bmpInfoHeader2.biWidth * bmpInfoHeader2.biHeight);
}
}
BOOL CCornerMatchTestDlg::GetBmpCorners1(vector &vecCorners)
{
vecCorners.clear();
vecCorners.resize(m_vecCorners1.size());
memcpy(&vecCorners[0], &m_vecCorners1[0], m_vecCorners1.size() * sizeof(POINT));
return TRUE;
}
BOOL CCornerMatchTestDlg::GetBmpCorners2(vector &vecCorners)
{
vecCorners.clear();
vecCorners.resize(m_vecCorners2.size());
memcpy(&vecCorners[0], &m_vecCorners2[0], m_vecCorners2.size() * sizeof(POINT));
return TRUE;
}