⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 winc3ddlg.cpp

📁 本程序是计算机视觉稠密匹配的程序
💻 CPP
字号:
// winc3dDlg.cpp : implementation file
//

#include "stdafx.h"
#include "winc3d.h"
#include "Sequence.h"
#include "Image.h"
#include "BMPImage.h"
#include "Aligner.h"
#include "winc3dDlg.h"
#include "DisparityMap.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CWinc3dDlg dialog

CWinc3dDlg::CWinc3dDlg(CWnd* pParent /*=NULL*/)
: CDialog(CWinc3dDlg::IDD, pParent), m_pDisparityMap(NULL), m_pAlignedLeft(NULL), 
m_pAlignedRight(NULL)
{
	//{{AFX_DATA_INIT(CWinc3dDlg)
	m_fileLeft = _T("");
	m_fileRight = _T("");
	m_iMinParallax = 0;
	m_iMaxParallax = 20;
	m_iMatch3 = 30;
	m_iOffset = 0;
	m_iGapContinue = 4;
	m_iGapStart = 1;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CWinc3dDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWinc3dDlg)
	DDX_Control(pDX, IDC_PLACEHOLDER, m_PlaceHolder);
	DDX_Control(pDX, IDC_MARGINCALC, m_MarginCalc);
	DDX_Text(pDX, IDC_LEFTNAME, m_fileLeft);
	DDX_Text(pDX, IDC_RIGHTNAME, m_fileRight);
	DDX_Text(pDX, IDC_MINPARALLAX, m_iMinParallax);
	DDX_Text(pDX, IDC_MAXPARALLAX, m_iMaxParallax);
	DDX_Text(pDX, IDC_MATCH_3, m_iMatch3);
	DDX_Text(pDX, IDC_OFFSET, m_iOffset);
	DDX_Text(pDX, IDC_GAPCONTINUE, m_iGapContinue);
	DDX_Text(pDX, IDC_GAPSTART, m_iGapStart);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CWinc3dDlg, CDialog)
	//{{AFX_MSG_MAP(CWinc3dDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BROWSELEFT, OnBrowseLeft)
	ON_BN_CLICKED(IDC_BROWSERIGHT, OnBrowseRight)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWinc3dDlg message handlers

BOOL CWinc3dDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CWinc3dDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		COLORREF clr;
        CPaintDC dc(this);
        CRect rectPlaceholder;
        m_MarginCalc.GetWindowRect(&rectPlaceholder);
        ScreenToClient(&rectPlaceholder);
        double dDisp;
        int iMinDisp=0, iMaxDisp=0, iDisp;
        unsigned int i, j;
        
        if (NULL != m_pDisparityMap)
        {
            for (i=0; i < m_pDisparityMap->m_cRows; i++)
            {
                for (j=0; j < m_pDisparityMap->m_cCols; j++)
                {
                    iDisp = m_pDisparityMap->DisparityAt(i, j);
                    iMinDisp = min(iMinDisp, iDisp);
                    iMaxDisp = max(iMaxDisp, iDisp);
                }
            }
            iMinDisp = max(iMinDisp, m_iMinParallax);
            iMaxDisp = min(iMaxDisp, m_iMaxParallax);
            for (i=0; i < m_pDisparityMap->m_cRows; i++)
            {
                for (j=0; j < m_pDisparityMap->m_cCols; j++)
                {
                    dDisp = max(m_pDisparityMap->DisparityAt(i, j), iMinDisp);
                    dDisp = min(dDisp, iMaxDisp);
					
                    dDisp -= iMinDisp;
                    
                    dDisp *= (255.0 / (iMaxDisp - iMinDisp));
                    clr = (COLORREF) RGB((int) dDisp, (int) dDisp, (int) dDisp);
                    dc.SetPixel(rectPlaceholder.left + j, rectPlaceholder.top + i, clr);
                }
            }
        }
//		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CWinc3dDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CWinc3dDlg::OnBrowseLeft() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
    CFileDialog dlg( TRUE, NULL, NULL, OFN_HIDEREADONLY, 
		_T("Windows Bitmaps (*.bmp)|*.bmp||"), this );
	if ( dlg.DoModal() == IDOK )
	{
        m_fileLeft = dlg.GetPathName();
        UpdateData(FALSE);
    }
}

void CWinc3dDlg::OnBrowseRight() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
    CFileDialog dlg( TRUE, NULL, NULL, OFN_HIDEREADONLY, 
		_T("Windows Bitmaps (*.bmp)|*.bmp||"), this );
	if ( dlg.DoModal() == IDOK )
	{
        m_fileRight = dlg.GetPathName();
        UpdateData(FALSE);
    }
}

void CWinc3dDlg::OnOK() 
{
	// TODO: Add extra validation here
	UpdateData(TRUE);
    CWaitCursor w;
    
    unsigned int cRows, iRow;
    unsigned int cCols, iCol;
    int iCurDisparity, iCurLeftLoc;
	
    CBMPImage imgLeft, imgRight;
    CSequence *pSeqLeft, *pSeqRight;
    CImage::m_iGapPenalty = -m_iGapContinue;
	//    CImage::m_iMatchZero = m_iMatch0;
	//    CImage::m_iMatchOne = m_iMatch1;
	//    CImage::m_iMatchTwo = m_iMatch2;
    CImage::m_iMatchThree = m_iMatch3;
	
    // do some sanity checking...
	
    if (!imgLeft.LoadFile(m_fileLeft))
    {
        ::MessageBox(NULL, "Couldn't open the left file as a Windows Bitmap.", "Couldn't Find File", 0);
        return;
    }
    if (!imgRight.LoadFile(m_fileRight))
    {
        ::MessageBox(NULL, "Couldn't open the left file as a Windows Bitmap.", "Couldn't Find File", 0);
        return;
    }
    cRows = min(imgLeft.cRows(), imgRight.cRows());
    if (m_iOffset < 0)
    {
        iRow = 0-m_iOffset;
    }
    else
    {
        iRow = 0;
        cRows -= m_iOffset;
    }
    cCols = imgLeft.cCols();
    if (NULL != m_pDisparityMap)
    {
        delete m_pDisparityMap;
    }
    m_pDisparityMap = new CDisparityMap(cRows, cCols);
    CAligner align(m_iGapStart, m_iGapContinue, CImage::MatchScore, m_iMinParallax, m_iMaxParallax);
    for (;iRow < cRows; iRow++)
    {
        pSeqLeft = imgLeft.getRow(iRow);
        pSeqRight = imgRight.getRow(iRow + m_iOffset);
		
        align.SetSequences(pSeqLeft, pSeqRight);
        align.Align(); // do da magic!
        align.GetAlignment(0, &m_pAlignedLeft, &m_pAlignedRight);
        iCurDisparity = 0;
        iCurLeftLoc = 0;
        for (iCol = 0; iCol < m_pAlignedLeft->Length() ; iCol++)
        {
            if (0xFFFFFFFF == m_pAlignedRight->LetterAt(iCol))
                iCurDisparity++;
            if (0xFFFFFFFF == m_pAlignedLeft->LetterAt(iCol))
                iCurDisparity--;
            else
            {
                m_pDisparityMap->DisparityAt(iRow, iCurLeftLoc++) = iCurDisparity;
            }
        }
		
        delete m_pAlignedRight;
        delete m_pAlignedLeft;
        delete pSeqLeft;
        delete pSeqRight;
    }
	
    CRect rectPlaceholder, rectMinWidth;
    m_PlaceHolder.GetWindowRect(&rectPlaceholder);
    m_MarginCalc.GetWindowRect(&rectMinWidth);
    int iMargin = rectPlaceholder.Width() - rectMinWidth.Width();
    ScreenToClient(&rectPlaceholder);
    unsigned int xSize = max(rectMinWidth.Width(), (int) cCols) + iMargin;
    unsigned int ySize = rectPlaceholder.bottom + cRows + iMargin*2;
    SetWindowPos(NULL, 0, 0, xSize, ySize, SWP_NOMOVE | SWP_NOZORDER);    
    InvalidateRect(NULL);
//	CDialog::OnOK();
}

BOOL CWinc3dDlg::DestroyWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
	if (NULL != m_pDisparityMap)
    {
        delete m_pDisparityMap;
    }
	return CDialog::DestroyWindow();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -