StereoPlusDlg.cpp
资源名称:estereo2.zip [点击查看]
上传用户:fengshi120
上传日期:2014-07-17
资源大小:6155k
文件大小:30k
源码类别:
3D图形编程
开发平台:
C/C++
- // StereoPlusDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include <gdiplus.h>
- #include "StereoPlus.h"
- #include "GeneralPurpose.h"
- #include "StereoPlusDlg.h"
- #include ".stereoplusdlg.h"
- #include "stereomatching.h"
- #include "DirectXDialog.h"
- #include "ThumbnailFileDialog.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- // CStereoPlusDlg dialog
- #define MIN_HOROPTER -500
- #define MAX_HOROPTER 500
- #define MIN_DISPARITY_RANGE 8
- #define MAX_DISPARITY_RANGE 128
- #define MIN_CORRELATION_WINDOW 5
- #define MAX_CORRELATION_WINDOW 49
- #define MIN_SAD_THRESHOLD 0
- #define MAX_SAD_THRESHOLD 99
- #define MIN_DISPARITY_IMAGE_SCALE 0
- #define MAX_DISPARITY_IMAGE_SCALE 4
- #define DELETENULL(object) if (object) {delete object;object = NULL;}
- CStereoPlusDlg* pMainDialog = NULL;
- CStereoPlusDlg::CStereoPlusDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CStereoPlusDlg::IDD, pParent)
- , m_iDisparityRange(0)
- , m_iHoropter(0)
- , m_iCorrelationWindow(0)
- , m_iSADThreshold(0)
- , m_iDisparityImageScale(0)
- , m_bPropagateStereo(FALSE)
- , m_SADControls(this,IDC_STATIC_CONTROLS)
- , m_fBaseLine(0)
- , m_fFocalLength(0)
- , m_bSwapLeftRight(FALSE)
- , m_iImageResampleWidth(0)
- {
- m_bClosing = false;
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- m_pLeftImageBitmap = NULL;
- m_pRightImageBitmap = NULL;
- m_pDisparityBitmap = NULL;
- m_bSizeChanged = true;
- m_bNewFilesLoaded = false;
- m_pLeftBitmapData = NULL;
- m_pRightBitmapData = NULL;
- m_bImagesAreSwapped = FALSE;
- m_iImageXTranslation = 0;
- m_iImageYTranslation = 0;
- m_fFocalLength = 260.0f / 0.01;
- m_fBaseLine = 100.0f;
- m_pStereoContext = NULL;
- m_pPts3d = NULL;
- m_bPropagateStereo = false;
- m_bGreyScaleDataLoaded = false;
- m_fExtentX = 1.0f;
- m_fExtentZ = 1.0f;
- m_iHoropter = theApp.GetProfileInt("SAD Params","Horopter",0);
- m_iDisparityRange = theApp.GetProfileInt("SAD Params","Disparity Range",32);
- m_iCorrelationWindow = theApp.GetProfileInt("SAD Params","Correlation Window Size",7);
- m_iSADThreshold = theApp.GetProfileInt("SAD Params","SAD Threshold",2);
- m_iDisparityImageScale = theApp.GetProfileInt("SAD Params","Disparity Image Scale",1);
- m_bPropagateStereo = theApp.GetProfileInt("SAD Params","Propagate Stereo",0);
- m_fFocalLength = (float)theApp.GetProfileDouble("Recon Params","Focal Length",150.0);
- m_fBaseLine = (float)theApp.GetProfileDouble("Recon Params","Base Line",0.05);
- m_iImageResampleWidth = theApp.GetProfileInt("Image Options","Resample Size",320);
- m_bResample = theApp.GetProfileInt("Image Options","Resample ON",1);
- m_iImageXTranslation = theApp.GetProfileInt("Image Options","xAxis Translate",0);
- m_iImageYTranslation = theApp.GetProfileInt("Image Options","yAxis Translate",0);
- InitStereoContext();
- pMainDialog = this;
- }
- CStereoPlusDlg::~CStereoPlusDlg()
- {
- theApp.WriteProfileInt("SAD Params","Disparity Range",m_iDisparityRange);
- theApp.WriteProfileInt("SAD Params","Horopter",m_iHoropter);
- theApp.WriteProfileInt("SAD Params","Correlation Window Size",m_iCorrelationWindow);
- theApp.WriteProfileInt("SAD Params","SAD Threshold",m_iSADThreshold);
- theApp.WriteProfileInt("SAD Params","Disparity Image Scale",m_iDisparityImageScale);
- theApp.WriteProfileInt("SAD Params","Propagate Stereo",m_bPropagateStereo);
- theApp.WriteProfileDouble("Recon Params","Focal Length",(double)m_fFocalLength);
- theApp.WriteProfileDouble("Recon Params","Base Line",(double)m_fBaseLine);
- theApp.WriteProfileInt("Image Options","Resample Size",m_iImageResampleWidth);
- theApp.WriteProfileInt("Image Options","Resample ON",m_bResample);
- theApp.WriteProfileInt("Image Options","xAxis Translate",m_iImageXTranslation);
- theApp.WriteProfileInt("Image Options","yAxis Translate",m_iImageYTranslation);
- if(m_pLeftImageBitmap)
- {
- delete m_pLeftImageBitmap;
- m_pLeftImageBitmap = NULL;
- }
- if(m_pRightImageBitmap)
- {
- delete m_pRightImageBitmap;
- m_pRightImageBitmap = NULL;
- }
- if(m_pDisparityBitmap)
- {
- delete m_pDisparityBitmap;
- m_pDisparityBitmap = NULL;
- }
- if(m_pLeftBitmapData)
- delete [] m_pLeftBitmapData;
- if(m_pRightBitmapData)
- delete [] m_pRightBitmapData;
- if(m_pStereoContext)
- delete m_pStereoContext;
- if(m_pPts3d)
- delete m_pPts3d;
- }
- void CStereoPlusDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- DDX_Text(pDX, IDC_EDIT_DISPARITYRANGE, m_iDisparityRange);
- DDV_MinMaxInt(pDX, m_iDisparityRange, MIN_DISPARITY_RANGE, MAX_DISPARITY_RANGE);
- DDX_Text(pDX, IDC_EDIT_HOROPTER, m_iHoropter);
- DDV_MinMaxInt(pDX, m_iHoropter, MIN_HOROPTER, MAX_HOROPTER);
- DDX_Text(pDX, IDC_EDIT_CORRWINSIZE, m_iCorrelationWindow);
- DDV_MinMaxInt(pDX, m_iCorrelationWindow, MIN_CORRELATION_WINDOW, MAX_CORRELATION_WINDOW);
- DDX_Text(pDX, IDC_EDIT_SADTHRESH, m_iSADThreshold);
- DDV_MinMaxInt(pDX, m_iSADThreshold, MIN_SAD_THRESHOLD, MAX_SAD_THRESHOLD);
- DDX_Text(pDX, IDC_EDIT_DISPARITYSCALE, m_iDisparityImageScale);
- DDV_MinMaxInt(pDX, m_iDisparityImageScale, MIN_DISPARITY_IMAGE_SCALE, MAX_DISPARITY_IMAGE_SCALE);
- DDX_Check(pDX, IDC_CHECK_STEREOPROPAG, m_bPropagateStereo);
- DDX_Text(pDX, IDC_EDIT_BASE_LINE, m_fBaseLine);
- DDV_MinMaxFloat(pDX, m_fBaseLine, 0.00001f, 1000.0f);
- DDX_Text(pDX, IDC_EDIT_FOCAL_LENGTH, m_fFocalLength);
- DDV_MinMaxFloat(pDX, m_fFocalLength, 1.0f, 100000.0f);
- DDX_Check(pDX, IDC_CHECK_SWAP, m_bSwapLeftRight);
- DDX_Control(pDX, IDC_CHECK_SWAP, m_ctrlCheckSwapLeftRight);
- DDX_Control(pDX, IDC_CHECK_RESAMPLE, m_ctrlResampleImages);
- DDX_Text(pDX, IDC_EDIT_RESAMPLE_WIDTH, m_iImageResampleWidth);
- DDV_MinMaxInt(pDX, m_iImageResampleWidth, 64, 1280);
- DDX_Control(pDX, IDC_EDIT_RESAMPLE_WIDTH, m_ctrlEditResampleWidth);
- DDX_Text(pDX, IDC_EDIT_XTRANSLATE, m_iImageXTranslation);
- DDX_Text(pDX, IDC_EDIT_YTRANSLATE, m_iImageYTranslation);
- }
- BEGIN_MESSAGE_MAP(CStereoPlusDlg, CDialog)
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- //}}AFX_MSG_MAP
- ON_BN_CLICKED(IDC_BUTTON_LOAD_LEFT, OnBnClickedButtonLoadLeft)
- ON_WM_DESTROY()
- ON_WM_SIZE()
- ON_BN_CLICKED(IDC_BUTTON_LOAD_RIGHT, OnBnClickedButtonLoadRight)
- ON_BN_CLICKED(IDC_BUTTON_LOAD_PAIR, OnBnClickedButtonLoadPair)
- ON_BN_CLICKED(IDC_BUTTON_DO_STEREO, OnBnClickedButtonDoStereo)
- ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_DISPARITYRANGE, OnDeltaposSpinDisparityrange)
- ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_HOROPTER, OnDeltaposSpinHoropter)
- ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_CORRWINSIZE, OnDeltaposSpinCorrwinsize)
- ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_SADTHRESH, OnDeltaposSpinSadthresh)
- ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_DISPARITYSCALE, OnDeltaposSpinDisparityscale)
- ON_BN_CLICKED(IDC_CHECK_STEREOPROPAG, OnBnClickedCheckStereoPropagation)
- ON_WM_CLOSE()
- ON_BN_CLICKED(IDC_BUTTON_DO_RECONSTRUCTION_DIRECTX, OnBnClickedButtonDoReconstructionDirectx)
- ON_BN_CLICKED(IDC_CHECK_SWAP, OnBnClickedCheckSwap)
- ON_BN_CLICKED(IDC_CHECK_RESAMPLE, OnBnClickedCheckResample)
- ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_XTRANSLATE, OnDeltaposSpinXtranslate)
- ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_YTRANSLATE, OnDeltaposSpinYtranslate)
- ON_BN_CLICKED(IDC_BUTTON_SAVEDISPARITY, OnBnClickedButtonSavedisparity)
- END_MESSAGE_MAP()
- // CStereoPlusDlg message handlers
- BOOL CStereoPlusDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- m_strLeftImagePath = theApp.GetProfileString("Images","Left Image","C:\");
- m_strRightImagePath = theApp.GetProfileString("Images","Right Image","C:\");
- m_strImagePairPath = theApp.GetProfileString("Images","Image Pair","C:\");
- // 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
- m_ctrlEditResampleWidth.EnableWindow(m_bResample);
- if(m_bResample)
- m_ctrlResampleImages.SetCheck(BST_CHECKED);
- //this is so the controls maintain there position relative to IDC_STATIC_CONTROLS
- //when that window is moved by the user resizing the app window
- m_SADControls.AddControl(IDC_STATIC_SAD_FRAME);
- m_SADControls.AddControl(IDC_BUTTON_DO_STEREO);
- m_SADControls.AddControl(IDC_BUTTON_SAVEDISPARITY);
- m_SADControls.AddControl(IDC_STATIC_TEXT1);
- m_SADControls.AddControl(IDC_STATIC_TEXT2);
- m_SADControls.AddControl(IDC_STATIC_TEXT3);
- m_SADControls.AddControl(IDC_STATIC_TEXT4);
- m_SADControls.AddControl(IDC_SPIN_DISPARITYRANGE);
- m_SADControls.AddControl(IDC_SPIN_HOROPTER);
- m_SADControls.AddControl(IDC_SPIN_CORRWINSIZE);
- m_SADControls.AddControl(IDC_SPIN_SADTHRESH);
- m_SADControls.AddControl(IDC_SPIN_DISPARITYSCALE);
- m_SADControls.AddControl(IDC_CHECK_STEREOPROPAG);
- m_SADControls.AddControl(IDC_EDIT_DISPARITYRANGE);
- m_SADControls.AddControl(IDC_EDIT_HOROPTER);
- m_SADControls.AddControl(IDC_EDIT_CORRWINSIZE);
- m_SADControls.AddControl(IDC_EDIT_SADTHRESH);
- m_SADControls.AddControl(IDC_EDIT_DISPARITYSCALE);
- m_SADControls.AddControl(IDC_STATIC_PROPAGATION_FRAME);
- m_SADControls.AddControl(IDC_BUTTON_DO_RECONSTRUCTION_DIRECTX);
- m_SADControls.AddControl(IDC_STATIC_SCALE_FRAME);
- m_SADControls.AddControl(IDC_EDIT_BASE_LINE);
- m_SADControls.AddControl(IDC_EDIT_FOCAL_LENGTH);
- m_SADControls.AddControl(IDC_STATIC_FOCAL_LENGTH);
- m_SADControls.AddControl(IDC_STATIC_BASE_LINE);
- m_SADControls.AddControl(IDC_STATIC_RECON_FRAME);
- m_SADControls.AddControl(IDC_CHECK_SWAP);
- m_SADControls.AddControl(IDC_STATIC_IMAGE_FRAME);
- m_SADControls.AddControl(IDC_EDIT_RESAMPLE_WIDTH);
- m_SADControls.AddControl(IDC_STATIC_WIDTH);
- m_SADControls.AddControl(IDC_CHECK_RESAMPLE);
- m_SADControls.AddControl(IDC_STATIC_XTRANSLATE);
- m_SADControls.AddControl(IDC_EDIT_XTRANSLATE);
- m_SADControls.AddControl(IDC_SPIN_XTRANSLATE);
- m_SADControls.AddControl(IDC_STATIC_YTRANSLATE);
- m_SADControls.AddControl(IDC_EDIT_YTRANSLATE);
- m_SADControls.AddControl(IDC_SPIN_YTRANSLATE);
- 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 CStereoPlusDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
- SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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
- {
- CDialog::OnPaint();
- if(m_bSizeChanged)
- ArrangeControls();
- DrawBitmap(IDC_STATIC_LEFT,m_pLeftImageBitmap);
- DrawBitmap(IDC_STATIC_RIGHT,m_pRightImageBitmap);
- DrawBitmap(IDC_STATIC_DISPARITY,m_pDisparityBitmap);
- }
- }
- // The system calls this function to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CStereoPlusDlg::OnQueryDragIcon()
- {
- return static_cast<HCURSOR>(m_hIcon);
- }
- void CStereoPlusDlg::OnBnClickedButtonLoadLeft()
- {
- if(SelectImageFile(m_strLeftImagePath))
- {
- m_bGreyScaleDataLoaded = false;
- if(m_pLeftImageBitmap)
- {
- delete m_pLeftImageBitmap;
- m_pLeftImageBitmap = NULL;
- }
- WCHAR wString[MAX_PATH];
- MultiByteToWideChar (CP_ACP, 0,m_strLeftImagePath,-1,wString,MAX_PATH);
- // load bitmap
- m_pLeftImageBitmap = new Bitmap(wString);
- if(m_bResample)
- ResampleBitmap(m_pLeftImageBitmap,m_iImageResampleWidth);
- DrawBitmap(IDC_STATIC_LEFT,m_pLeftImageBitmap);
- m_bNewFilesLoaded = true;
- }
- }
- void CStereoPlusDlg::OnBnClickedButtonLoadRight()
- {
- if(SelectImageFile(m_strRightImagePath))
- {
- m_bGreyScaleDataLoaded = false;
- if(m_pRightImageBitmap)
- {
- delete m_pRightImageBitmap;
- m_pRightImageBitmap = NULL;
- }
- WCHAR wString[MAX_PATH];
- MultiByteToWideChar (CP_ACP, 0,m_strRightImagePath,-1,wString,MAX_PATH);
- m_pRightImageBitmap = new Bitmap(wString);
- if(m_bResample)
- ResampleBitmap(m_pRightImageBitmap,m_iImageResampleWidth);
- DrawBitmap(IDC_STATIC_RIGHT,m_pRightImageBitmap);
- m_bNewFilesLoaded = true;
- }
- }
- void CStereoPlusDlg::ResampleBitmap(Bitmap *&pBitmap,UINT iNewWidth)
- {
- UINT w = pBitmap->GetWidth();
- UINT h = pBitmap->GetHeight();
- float fAspect = (float)h/(float)w;
- UINT wSmall = iNewWidth;
- UINT hSmall = (int)(iNewWidth*fAspect);
- Bitmap *pNewBitmap = new Bitmap(wSmall,hSmall);
- Graphics* pGraphics = new Graphics(pNewBitmap);
- Rect rcDst(0,0,wSmall,hSmall);
- pGraphics->DrawImage(pBitmap,rcDst,0,0,w,h, UnitPixel,NULL,NULL,NULL);
- delete pGraphics;
- delete pBitmap;
- pBitmap = pNewBitmap;
- }
- void CStereoPlusDlg::OnBnClickedButtonLoadPair()
- {
- if(SelectImageFile(m_strImagePairPath))
- {
- m_bGreyScaleDataLoaded = false;
- if(m_pRightImageBitmap)
- {
- delete m_pRightImageBitmap;
- m_pRightImageBitmap = NULL;
- }
- if(m_pLeftImageBitmap)
- {
- delete m_pLeftImageBitmap;
- m_pLeftImageBitmap = NULL;
- }
- WCHAR wString[MAX_PATH];
- MultiByteToWideChar (CP_ACP, 0,m_strImagePairPath,-1,wString,MAX_PATH);
- Bitmap* pPairBitmap = new Bitmap(wString);
- if(pPairBitmap->GetLastStatus() == Ok)
- {
- int w = pPairBitmap->GetWidth();
- int h = pPairBitmap->GetHeight();
- m_pLeftImageBitmap = pPairBitmap->Clone(0,0,w/2,h,PixelFormat24bppRGB);
- if(m_bResample)
- ResampleBitmap(m_pLeftImageBitmap,m_iImageResampleWidth);
- m_pRightImageBitmap = pPairBitmap->Clone(w/2,0,w/2,h,PixelFormat24bppRGB);
- if(m_bResample)
- ResampleBitmap(m_pRightImageBitmap,m_iImageResampleWidth);
- m_bNewFilesLoaded = true;
- }
- delete pPairBitmap;
- }
- }
- BYTE* CStereoPlusDlg::GetGrayScaleData(Bitmap* pBitmap)
- {
- //Gdiplus sucks at manipulating greyscale images so it's simpler to just extract the luminance directly
- UINT uWidth = pBitmap->GetWidth();
- UINT uHeight = pBitmap->GetHeight();
- BitmapData *pBitmapData = new BitmapData;
- UINT nPixels = pBitmap->GetWidth()*pBitmap->GetHeight();
- BYTE* pGrayScaleData = new BYTE[nPixels];
- UINT i=0;
- Rect rect(0,0,uWidth,uHeight);
- if(pBitmap->LockBits(&rect,ImageLockModeRead,PixelFormat32bppARGB,pBitmapData) == Ok)
- {
- Color* pLine = (Color*)pBitmapData->Scan0;
- Color c;
- UINT uPixelsPerLine = pBitmapData->Stride / 4;
- for(UINT row=0;row<uHeight;row++)
- {
- for(UINT col=0;col<uWidth;col++)
- {
- c = pLine[col];
- pGrayScaleData[i++] =(BYTE) ((double)c.GetR()*0.3 + (double)c.GetG()*0.59 + (double)c.GetB()*0.11);
- }
- pLine += uPixelsPerLine;
- }
- pBitmap->UnlockBits(pBitmapData);
- }
- delete pBitmapData;
- return pGrayScaleData;
- }
- void CStereoPlusDlg::OnDestroy()
- {
- CDialog::OnDestroy();
- theApp.WriteProfileString("Images","Left Image",m_strLeftImagePath);
- theApp.WriteProfileString("Images","Right Image",m_strRightImagePath);
- theApp.WriteProfileString("Images","Image Pair",m_strImagePairPath);
- }
- void CStereoPlusDlg::DrawBitmap(UINT idWindow,Bitmap *pBitmap)
- {
- CWnd* pWnd = GetDlgItem(idWindow);
- if(pWnd)
- {
- CRect rcWindow;
- pWnd->GetClientRect(&rcWindow);
- int wWindow = rcWindow.Width()-10;
- int hWindow = rcWindow.Height()-20;
- Rect rcDest(5,15,wWindow,hWindow);
- CDC* pDC = pWnd->GetDC();
- if(pBitmap)
- {
- Graphics* pGraphics = new Graphics(pDC->m_hDC);
- pGraphics->ResetTransform();
- pGraphics->SetInterpolationMode(InterpolationModeHighQualityBicubic);
- pGraphics->DrawImage(pBitmap,rcDest,0,0,pBitmap->GetWidth(),pBitmap->GetHeight(),UnitPixel);
- pGraphics->ReleaseHDC(pDC->m_hDC);
- delete pGraphics;
- }
- else
- {
- CRect rcFill(CPoint(5,15),CSize(wWindow,hWindow));
- pDC->FillSolidRect(rcFill,RGB(0,0,0));
- }
- ReleaseDC(pDC);
- }
- }
- void CStereoPlusDlg::OnSize(UINT nType, int cx, int cy)
- {
- CDialog::OnSize(nType, cx, cy);
- m_bSizeChanged = true;
- Invalidate();
- }
- void CStereoPlusDlg::ArrangeControls(void)
- {//Set conrol positions optimally if window is resized
- CRect rcHost;
- GetClientRect(&rcHost);
- int wHost = rcHost.Width();
- int hHost = rcHost.Height();
- int hImageMax = (int)(0.5 + (double)(hHost-50)/2.0);
- int iSpacing = 10;
- int wImage = (wHost - 3*iSpacing)/2;
- double dAspectRatio = 1.0/1.33;
- int hImage = (int)(0.5 + dAspectRatio * (double)wImage );
- if(hImage > hImageMax)
- {
- wImage = (int)((double)hImageMax/dAspectRatio);
- iSpacing = (wHost - 2*wImage)/3;
- hImage = hImageMax;
- }
- int iYPos = 5;
- int xPos = iSpacing;
- CRect rcControl1(CPoint(xPos,iYPos),CSize(wImage,hImage));
- GetDlgItem(IDC_STATIC_LEFT)->MoveWindow(&rcControl1);
- int iButtonWidth = rcControl1.Width()/2-20;
- xPos += (wImage + iSpacing);
- CRect rcControl2(CPoint(xPos,iYPos),CSize(wImage,hImage));
- GetDlgItem(IDC_STATIC_RIGHT)->MoveWindow(&rcControl2);
- xPos = iSpacing;
- iYPos = rcControl1.bottom + 10;
- CRect rcControl3( CPoint(xPos,iYPos),CSize(iButtonWidth,20));
- GetDlgItem(IDC_BUTTON_LOAD_LEFT)->MoveWindow(&rcControl3);
- xPos += (wImage + iSpacing);
- CRect rcControl4(CPoint(rcControl2.right - rcControl1.Width()/2,iYPos),CSize(iButtonWidth,20));
- GetDlgItem(IDC_BUTTON_LOAD_RIGHT)->MoveWindow(&rcControl4);
- CRect rcControl6(CPoint(wHost/2-iButtonWidth/2,iYPos),CSize(iButtonWidth,20));
- GetDlgItem(IDC_BUTTON_LOAD_PAIR)->MoveWindow(&rcControl6);
- iYPos = rcControl4.bottom + 10;
- CRect rcControl5(CPoint(xPos,iYPos),CSize(wImage,hImage));
- GetDlgItem(IDC_STATIC_DISPARITY)->MoveWindow(&rcControl5);
- CRect rcControlHost;
- GetDlgItem(IDC_STATIC_CONTROLS)->GetWindowRect(&rcControlHost);
- ScreenToClient(&rcControlHost);
- xPos = iSpacing;
- iYPos = rcControl4.bottom + 10;
- CRect rcControl7(CPoint(xPos,iYPos),CSize(wImage,hImage));
- GetDlgItem(IDC_STATIC_CONTROLS)->MoveWindow(&rcControl7);
- //this moves all the controls grouped by IDC_STATIC_CONTROLS
- m_SADControls.PositionControls();
- }
- //The good stuff
- void CStereoPlusDlg::OnBnClickedButtonDoStereo()
- {
- if(m_bNewFilesLoaded)
- {
- if(m_pLeftBitmapData)
- delete [] m_pLeftBitmapData;
- if(m_pRightBitmapData)
- delete [] m_pRightBitmapData;
- m_pLeftBitmapData = GetGrayScaleData(m_pLeftImageBitmap);
- m_pRightBitmapData = GetGrayScaleData(m_pRightImageBitmap);
- m_bGreyScaleDataLoaded = true;
- m_bNewFilesLoaded = false;
- }
- if(m_bGreyScaleDataLoaded)
- {
- UINT wL = m_pLeftImageBitmap->GetWidth();
- UINT hL = m_pLeftImageBitmap->GetHeight();
- UINT wR = m_pRightImageBitmap->GetWidth();
- UINT hR = m_pRightImageBitmap->GetHeight();
- if(wL != wR || hL != hR)
- {
- AfxMessageBox("Images need to be of similar dimension",MB_ICONSTOP | MB_OK);
- return;
- }
- ComputeStereo();
- }
- }
- // save the 3D reconstruction in 'filename'
- bool CStereoPlusDlg::Save3DReconst(CString filename)
- {
- return true;
- }
- void CStereoPlusDlg::ComputeStereo(void)
- {
- UINT w = m_pLeftImageBitmap->GetWidth();
- UINT h = m_pLeftImageBitmap->GetHeight();
- // do (SAD-based) stereo processing
- estereo_process(m_pStereoContext,(m_bPropagateStereo==TRUE),&m_StereoData,
- //((char*)m_pLeftBitmapData)-m_iImageXTranslation,
- ((char*)m_pLeftBitmapData),
- (char*)m_pRightBitmapData, w,h, -m_iImageXTranslation, m_iImageYTranslation);
- int disp_h, disp_w; // size of disparity image (could be different from input images)
- disp_w = m_StereoData.width;
- disp_h = m_StereoData.height;
- if(!m_pDisparityBitmap)
- {
- m_pDisparityBitmap = new Bitmap(disp_w,disp_h,PixelFormat32bppARGB );//don't mess with palettes Gdi+ doesn't like them
- }
- else if(m_pDisparityBitmap->GetWidth() != disp_w || m_pDisparityBitmap->GetHeight() != disp_h)
- {
- delete m_pDisparityBitmap;
- m_pDisparityBitmap = new Bitmap(disp_w,disp_h,PixelFormat32bppARGB );
- }
- Rect rect(0,0,disp_w,disp_h);
- BitmapData *pBitmapData = new BitmapData;
- if(m_pDisparityBitmap->LockBits(&rect,ImageLockModeWrite,PixelFormat32bppARGB,pBitmapData) == Ok)
- {
- BYTE* ptd_src = (BYTE*)m_StereoData.imDepth8u;
- Color* pLine = (Color*)pBitmapData->Scan0;
- UINT uPixelsPerLine = pBitmapData->Stride / 4;
- for(int row=0;row<disp_h;row++)
- {
- for(int col=0;col<disp_w;col++)
- {
- if (*ptd_src>0)
- {
- BYTE lum = 50 + *ptd_src*4;
- pLine[col] = Color::MakeARGB(255,lum,lum,lum);
- }
- else
- {
- pLine[col] = Color::MakeARGB(255,0,0,0);
- }
- ptd_src++;
- }
- pLine += uPixelsPerLine;
- }
- m_pDisparityBitmap->UnlockBits(pBitmapData);
- }
- delete pBitmapData;
- DrawBitmap(IDC_STATIC_DISPARITY,m_pDisparityBitmap);
- }
- void CStereoPlusDlg::estereo_process(StereoMatching* stereo, bool propag_stereo,
- StereoImage* simage,
- const char* imLeft, const char* imRight,
- int width, int height, int xOffset, int yOffset)
- {
- // set the 'undefined depth' value to 0
- simage->setUndefinedDepthValue(0);
- // do the disparity calculation - SAD
- stereo->setInputImageSize(width, height);
- stereo->setLeftImageXOffset(xOffset);
- stereo->setLeftImageYOffset(yOffset);
- // set monoscale/multiscale processing
- stereo->setScaleProcessing(propag_stereo?StereoMatching::MULTISCALE
- : StereoMatching::MONOSCALE);
- stereo->doStereo(simage, (const unsigned char*)imLeft, (const unsigned char*)imRight);
- }
- void CStereoPlusDlg::InitStereoContext()
- {
- // *********************************************
- // ******** initialize disparity estimation context
- m_pStereoContext = new StereoMatching();
- // initialize SAD parameters
- m_pStereoContext->setHoropter(m_iHoropter);
- m_pStereoContext->setNumDepth(m_iDisparityRange);
- m_pStereoContext->setCorrelationWindowSize(m_iCorrelationWindow,m_iCorrelationWindow);
- m_pStereoContext->setInputImageSize(320,240);
- m_pStereoContext->setScale(m_iDisparityImageScale);
- m_pStereoContext->setAcceptDisparityThreshold((float)m_iSADThreshold);
- // allocate buffers for SAD computation
- m_pStereoContext->initializeContext();
- }
- void CStereoPlusDlg::OnDeltaposSpinDisparityrange(NMHDR *pNMHDR, LRESULT *pResult)
- {
- LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);
- if(UpdateData())
- {
- m_iDisparityRange -= 8*pNMUpDown->iDelta;
- m_iDisparityRange = max(MIN_DISPARITY_RANGE,min(MAX_DISPARITY_RANGE,m_iDisparityRange));
- UpdateData(FALSE);
- if(m_pStereoContext)
- m_pStereoContext->setNumDepth(m_iDisparityRange);
- if (m_bGreyScaleDataLoaded)
- {
- ComputeStereo();
- }
- }
- *pResult = 0;
- }
- void CStereoPlusDlg::OnDeltaposSpinHoropter(NMHDR *pNMHDR, LRESULT *pResult)
- {
- LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);
- if(UpdateData())
- {
- m_iHoropter -= pNMUpDown->iDelta;
- m_iHoropter = max(MIN_HOROPTER,min(MAX_HOROPTER,m_iHoropter));
- UpdateData(FALSE);
- if(m_pStereoContext)
- m_pStereoContext->setHoropter(m_iHoropter);
- if (m_bGreyScaleDataLoaded)
- {
- ComputeStereo();
- }
- }
- *pResult = 0;
- }
- void CStereoPlusDlg::OnDeltaposSpinCorrwinsize(NMHDR *pNMHDR, LRESULT *pResult)
- {
- LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);
- if(UpdateData())
- {
- m_iCorrelationWindow -= 2*pNMUpDown->iDelta;
- m_iCorrelationWindow = max(MIN_CORRELATION_WINDOW,min(MAX_CORRELATION_WINDOW,m_iCorrelationWindow));
- UpdateData(FALSE);
- if(m_pStereoContext)
- m_pStereoContext->setCorrelationWindowSize(m_iCorrelationWindow,m_iCorrelationWindow);
- if (m_bGreyScaleDataLoaded)
- {
- ComputeStereo();
- }
- }
- // TODO: Add your control notification handler code here
- *pResult = 0;
- }
- void CStereoPlusDlg::OnDeltaposSpinSadthresh(NMHDR *pNMHDR, LRESULT *pResult)
- {
- LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);
- if(UpdateData())
- {
- m_iSADThreshold -= pNMUpDown->iDelta;
- m_iSADThreshold = max(MIN_SAD_THRESHOLD,min(MAX_SAD_THRESHOLD,m_iSADThreshold));
- UpdateData(FALSE);
- if(m_pStereoContext)
- m_pStereoContext->setAcceptDisparityThreshold((float)m_iSADThreshold);
- if (m_bGreyScaleDataLoaded)
- {
- ComputeStereo();
- }
- }
- *pResult = 0;
- }
- void CStereoPlusDlg::OnDeltaposSpinDisparityscale(NMHDR *pNMHDR, LRESULT *pResult)
- {
- LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);
- if(UpdateData())
- {
- m_iDisparityImageScale -= pNMUpDown->iDelta;
- m_iDisparityImageScale = max(MIN_DISPARITY_IMAGE_SCALE,min(MAX_DISPARITY_IMAGE_SCALE,m_iDisparityImageScale));
- UpdateData(FALSE);
- if(m_pStereoContext)
- m_pStereoContext->setScale(m_iDisparityImageScale);
- if (m_bGreyScaleDataLoaded)
- {
- ComputeStereo();
- }
- }
- *pResult = 0;
- }
- void CStereoPlusDlg::OnBnClickedCheckStereoPropagation()
- {
- if(UpdateData())
- {
- if(m_bPropagateStereo)
- OutputDebugString("Stereo Propagation is ONn");
- else
- OutputDebugString("Stereo Propagation is OFFn");
- if (m_bGreyScaleDataLoaded)
- {
- ComputeStereo();
- }
- }
- }
- void CStereoPlusDlg::OnClose()
- {
- m_bClosing = true;
- CDialog::OnClose();
- }
- // build the 3D reconstruction and display it using directX
- void CStereoPlusDlg::OnBnClickedButtonDoReconstructionDirectx()
- {
- if(!UpdateData())
- return;
- // create a '3D reconstruction' structure
- if(m_pPts3d)
- delete m_pPts3d;
- m_pPts3d = new ReconstPoints3D();
- // set virtual camera parameters
- m_Reconst3d.setCameraParameters(m_fFocalLength,m_fBaseLine,
- (float)(m_StereoData.width/2),
- (float)(m_StereoData.height/2));
- // perform the 3D reconstruction
- switch(m_Reconst3d.doReconstruction(m_pPts3d,&m_StereoData,m_pRightImageBitmap))
- {
- case Reconst3D::RECONST_OK: {
- CDirectXDialog DirectXDlg(this);
- DirectXDlg.SetData( m_pPts3d->getNumPoints(),
- m_pPts3d->getXlist(),
- m_pPts3d->getYlist(),
- m_pPts3d->getZlist(),
- m_pPts3d->getARGBlist(),
- m_pPts3d->getPointSizelist());
- DirectXDlg.DoModal();
- }
- break;
- //
- case Reconst3D::CAMERA_PARAM_NOT_SET:
- AfxMessageBox("Set cam parameters before attempting reconstruction",MB_ICONEXCLAMATION);
- break;
- case Reconst3D::IMAGE_BUFFER_NULL:
- AfxMessageBox("Set the image buffer before attempting reconstruction",MB_ICONEXCLAMATION);
- break;
- case Reconst3D::WRONG_IMAGE_SIZE:
- AfxMessageBox("Image size doesn't match the SAD data",MB_ICONEXCLAMATION);
- break;
- }
- }
- void CStereoPlusDlg::OnBnClickedCheckSwap()
- {
- if(UpdateData())
- {
- if(m_bImagesAreSwapped != m_bSwapLeftRight)
- SwapImages();
- m_bImagesAreSwapped = m_bSwapLeftRight;
- }
- }
- void CStereoPlusDlg::SwapImages(void)
- {
- if(m_pLeftImageBitmap && m_pRightImageBitmap)
- {
- Bitmap* pLeft = m_pLeftImageBitmap;
- Bitmap* pRight = m_pRightImageBitmap;
- BYTE* pLeftData = m_pLeftBitmapData;
- BYTE* pRightData = m_pRightBitmapData;
- m_pLeftImageBitmap = pRight;
- m_pRightImageBitmap = pLeft;
- m_pLeftBitmapData = pRightData;
- m_pRightBitmapData = pLeftData;
- DrawBitmap(IDC_STATIC_LEFT,m_pLeftImageBitmap);
- DrawBitmap(IDC_STATIC_RIGHT,m_pRightImageBitmap);
- ComputeStereo();
- }
- }
- void CStereoPlusDlg::OnBnClickedCheckResample()
- {
- m_bResample = (m_ctrlResampleImages.GetCheck() == BST_CHECKED);
- m_ctrlEditResampleWidth.EnableWindow(m_bResample);
- if(m_bResample && (m_pLeftImageBitmap || m_pRightImageBitmap) )
- AfxMessageBox("Re-Load images to apply resampling",MB_ICONINFORMATION);
- }
- void CStereoPlusDlg::OnDeltaposSpinXtranslate(NMHDR *pNMHDR, LRESULT *pResult)
- {
- LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);
- if(UpdateData())
- {
- m_iImageXTranslation -= pNMUpDown->iDelta;
- m_iImageXTranslation = max(-128,min(128,m_iImageXTranslation));
- UpdateData(FALSE);
- if (m_bGreyScaleDataLoaded) {
- ComputeStereo();
- }
- }
- // TODO: Add your control notification handler code here
- *pResult = 0;
- }
- void CStereoPlusDlg::OnDeltaposSpinYtranslate(NMHDR *pNMHDR, LRESULT *pResult)
- {
- LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);
- if(UpdateData())
- {
- m_iImageYTranslation -= pNMUpDown->iDelta;
- m_iImageYTranslation = max(-128,min(128,m_iImageYTranslation));
- UpdateData(FALSE);
- if (m_bGreyScaleDataLoaded) {
- ComputeStereo();
- }
- }
- // TODO: Add your control notification handler code here
- *pResult = 0;
- }
- int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
- {
- UINT num = 0; // number of image encoders
- UINT size = 0; // size of the image encoder array in bytes
- ImageCodecInfo* pImageCodecInfo = NULL;
- GetImageEncodersSize(&num, &size);
- if(size == 0)
- return -1; // Failure
- pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
- if(pImageCodecInfo == NULL)
- return -1; // Failure
- GetImageEncoders(num, size, pImageCodecInfo);
- for(UINT j = 0; j < num; ++j)
- {
- if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
- {
- *pClsid = pImageCodecInfo[j].Clsid;
- free(pImageCodecInfo);
- return j; // Success
- }
- }
- free(pImageCodecInfo);
- return -1; // Failure
- }
- void CStereoPlusDlg::OnBnClickedButtonSavedisparity()
- {
- // TODO: Add your control notification handler code here
- // save m_pDisparityBitmap
- CString FilePath = m_strLeftImagePath;
- CFileDialog* pFileDlg = new CThumbnailFileDialog(FALSE,"jpg",NULL,
- OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_FILEMUSTEXIST,
- "Image Files (*.jpg) |*.jpg| All Files (*.*) |*.*||",
- // "Image Files (*.jpg;*.jps,*.bmp) |*.jpg;*.jps;*.bmp| All Files (*.*) |*.*||",
- AfxGetMainWnd() );
- char drive[_MAX_DRIVE];
- char dir[_MAX_DIR];
- char fname[_MAX_FNAME];
- char ext[_MAX_EXT];
- CString FileDir;
- _splitpath( FilePath, drive, dir, fname, ext );
- if(strlen(drive) != 0)FileDir = drive;
- if(strlen(dir) != 0)FileDir += dir;
- pFileDlg->m_ofn.nFilterIndex = 0;
- pFileDlg->m_ofn.lpstrInitialDir = FileDir;
- pFileDlg->m_ofn.lpstrTitle = "Select Image";
- CString InitialFileName = fname;
- lstrcpy(pFileDlg->m_ofn.lpstrFile,InitialFileName);
- BOOL FileSelected = FALSE;
- if(pFileDlg->DoModal() == IDOK)
- {
- FilePath = pFileDlg->GetPathName();
- FileSelected = TRUE;
- CString strPathName = FilePath;
- CLSID clsid;
- GetEncoderClsid(L"image/jpeg", &clsid);
- Status status = m_pDisparityBitmap->Save(strPathName.AllocSysString(), &clsid);
- if(Ok != status)
- {
- TRACE2(_T("nFailed to save image in '%s' filen")
- _T("GDI+ Error: %u"),
- strPathName,
- status);
- }
- }
- delete pFileDlg;
- pFileDlg = NULL;
- }