1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
40 //M*/// VMDemoDlg.cpp : implementation file
45 #include "VMDemoDlg.h"
55 static char THIS_FILE[] = __FILE__;
58 /////////////////////////////////////////////////////////////////////////////
59 // CAboutDlg dialog used for App About
61 class CAboutDlg : public CDialog
67 //{{AFX_DATA(CAboutDlg)
68 enum { IDD = IDD_ABOUTBOX };
71 // ClassWizard generated virtual function overrides
72 //{{AFX_VIRTUAL(CAboutDlg)
74 virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
79 //{{AFX_MSG(CAboutDlg)
85 /*======================================================================================*/
87 CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
89 //{{AFX_DATA_INIT(CAboutDlg)
94 /*======================================================================================*/
96 void CAboutDlg::DoDataExchange(CDataExchange* pDX)
98 CDialog::DoDataExchange(pDX);
99 //{{AFX_DATA_MAP(CAboutDlg)
104 /*======================================================================================*/
106 BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
107 //{{AFX_MSG_MAP(CAboutDlg)
108 // No message handlers
112 /////////////////////////////////////////////////////////////////////////////
115 CVMDemoDlg::CVMDemoDlg(CWnd* pParent /*=NULL*/)
116 : CDialog(CVMDemoDlg::IDD, pParent)
118 //{{AFX_DATA_INIT(CVMDemoDlg)
119 // NOTE: the ClassWizard will add member initialization here
121 // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
122 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
125 RasterLeft = new CRaster;
126 RasterRight = new CRaster;
127 RasterResult = new CRaster;
160 /*======================================================================================*/
162 void CVMDemoDlg::DoDataExchange(CDataExchange* pDX)
164 CDialog::DoDataExchange(pDX);
165 //{{AFX_DATA_MAP(CVMDemoDlg)
166 DDX_Control(pDX, IDC_SHOWSCANLINES, m_showscanlines);
167 DDX_Control(pDX, IDC_IMAGESIZE, m_imagesize);
168 DDX_Control(pDX, IDC_CAMERAPOS, m_camerapos);
173 /*======================================================================================*/
175 BEGIN_MESSAGE_MAP(CVMDemoDlg, CDialog)
176 //{{AFX_MSG_MAP(CVMDemoDlg)
179 ON_WM_QUERYDRAGICON()
180 ON_BN_CLICKED(IDC_OPENPROJECT, OnOpenproject)
182 ON_BN_CLICKED(IDC_ORIGINALSIZE, OnOriginalsize)
183 ON_BN_CLICKED(IDC_MAKEMORPHING, OnMakemorphing)
184 ON_BN_CLICKED(IDC_SHOWSCANLINES, OnShowscanlines)
188 /////////////////////////////////////////////////////////////////////////////
189 // CVMDemoDlg message handlers
191 BOOL CVMDemoDlg::OnInitDialog()
193 CDialog::OnInitDialog();
195 // Add "About..." menu item to system menu.
197 // IDM_ABOUTBOX must be in the system command range.
198 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
199 ASSERT(IDM_ABOUTBOX < 0xF000);
201 CMenu* pSysMenu = GetSystemMenu(FALSE);
202 if (pSysMenu != NULL)
204 CString strAboutMenu;
205 strAboutMenu.LoadString(IDS_ABOUTBOX);
206 if (!strAboutMenu.IsEmpty())
208 pSysMenu->AppendMenu(MF_SEPARATOR);
209 pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
213 // Set the icon for this dialog. The framework does this automatically
214 // when the application's main window is not a dialog
215 SetIcon(m_hIcon, TRUE); // Set big icon
216 SetIcon(m_hIcon, FALSE); // Set small icon
218 // TODO: Add extra initialization here
232 className = AfxRegisterWndClass(NULL);
238 LeftImage = new CVirtualImage;
240 LeftImage ->CreateEx( WS_EX_OVERLAPPEDWINDOW,
243 WS_VISIBLE | WS_OVERLAPPED |WS_THICKFRAME,
248 RightImage = new CVirtualImage;
250 RightImage ->CreateEx( WS_EX_OVERLAPPEDWINDOW,
253 WS_VISIBLE | WS_OVERLAPPED |WS_THICKFRAME,
261 ResultImage = new CVirtualImage;
263 ResultImage ->CreateEx( WS_EX_OVERLAPPEDWINDOW,
266 WS_VISIBLE | WS_OVERLAPPED |WS_THICKFRAME,
271 LeftImage -> SetScanlines(&scanlines_2,&numScanlines);
272 LeftImage -> SetRasterImage(RasterLeft);
273 LeftImage -> ShowWindow(SW_SHOW);
275 RightImage -> SetScanlines(&scanlines_1,&numScanlines);
276 RightImage -> SetRasterImage(RasterRight);
277 RightImage -> ShowWindow(SW_SHOW);
279 ResultImage -> SetScanlines(&scanlines_a,&numScanlines);
280 ResultImage -> SetRasterImage(RasterResult);
281 ResultImage -> ShowWindow(SW_SHOW);
286 m_camerapos.SetRange(0,300);// ition.SetScrollRange(0,300);
288 m_imagesize.SetWindowText("");
290 return TRUE; // return TRUE unless you set the focus to a control
294 /*======================================================================================*/
296 void CVMDemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
298 if ((nID & 0xFFF0) == IDM_ABOUTBOX)
305 CDialog::OnSysCommand(nID, lParam);
310 /*======================================================================================*/
312 void CVMDemoDlg::OnPaint()
316 CPaintDC dc(this); // device context for painting
318 SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
320 // Center icon in client rectangle
321 int cxIcon = GetSystemMetrics(SM_CXICON);
322 int cyIcon = GetSystemMetrics(SM_CYICON);
324 GetClientRect(&rect);
325 int x = (rect.Width() - cxIcon + 1) / 2;
326 int y = (rect.Height() - cyIcon + 1) / 2;
329 dc.DrawIcon(x, y, m_hIcon);
338 /*======================================================================================*/
340 HCURSOR CVMDemoDlg::OnQueryDragIcon()
342 return (HCURSOR) m_hIcon;
346 /*======================================================================================*/
348 void CVMDemoDlg::OnOpenproject()
352 CFileDialog filedialog(
354 NULL,//Default extension
355 NULL,//Initial file name
356 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,//Flag
357 "Project Files (*.ini;*.cpj)|*.ini;*.cpj|All Files (*.*)|*.*|");//Fiter
359 if (filedialog.DoModal()==IDOK) {
360 CString projectName=filedialog.GetPathName();
362 //char path_buffer[_MAX_PATH];
363 char drive[_MAX_DRIVE];
365 char fname[_MAX_FNAME];
368 _splitpath(projectName,drive,dir,fname,ext );
370 if (stricmp(ext,".cpj") == 0)
384 numPicts = ::GetPrivateProfileInt( "Project",
385 "NumberOfPicturePairs",
392 /* Load Left Image */
393 char buff[_MAX_PATH];
394 ::GetPrivateProfileString( "Pair1",
401 RasterLeft ->LoadBMP(buff);
408 step = RasterLeft->GetSizeString();
409 h = RasterLeft->GetHeight();
411 src = (char*)(RasterLeft->GetImage());
412 dest = src + step * (h - 1);
414 tmpBuff = (char*)calloc(step,1);
416 for (y = 0; y < h/2; y++) {
417 memcpy(tmpBuff,src,step);
418 memcpy(src,dest,step);
419 memcpy(dest,tmpBuff,step);
428 LeftImage ->Invalidate();
432 ::GetPrivateProfileString( "Pair1",
439 RasterRight ->LoadBMP(buff) ;
446 step = RasterRight->GetSizeString();
447 h = RasterRight->GetHeight();
449 src = (char*)(RasterRight->GetImage());
450 dest = src + step * (h - 1);
452 tmpBuff = (char*)calloc(step,1);
454 for (y = 0; y < h/2; y++) {
455 memcpy(tmpBuff,src,step);
456 memcpy(src,dest,step);
457 memcpy(dest,tmpBuff,step);
466 RightImage ->Invalidate();
469 if ( (RasterLeft -> GetWidth() != RasterRight -> GetWidth()) ||
470 (RasterLeft -> GetHeight() != RasterRight -> GetHeight())
473 m_imagesize.SetWindowText("");
478 sprintf(st,"%dx%d",RasterLeft -> GetWidth(),RasterLeft -> GetHeight());
479 m_imagesize.SetWindowText(st);
482 RasterResult -> CreateRaster( RasterLeft -> GetWidth(),
483 RasterLeft -> GetHeight(),
486 ResultImage -> Invalidate();
491 /* Read Fundamental matrix */
495 ::GetPrivateProfileString( "FundMatrix","F[0][0]","0.0",strf,100,projectName);
496 FundMatrix.m[0][0] = (float)atof(strf);
498 ::GetPrivateProfileString( "FundMatrix","F[0][1]","0.0",strf,100,projectName);
499 FundMatrix.m[0][1] = (float)atof(strf);
501 ::GetPrivateProfileString( "FundMatrix","F[0][2]","0.0",strf,100,projectName);
502 FundMatrix.m[0][2] = (float)atof(strf);
504 ::GetPrivateProfileString( "FundMatrix","F[1][0]","0.0",strf,100,projectName);
505 FundMatrix.m[1][0] = (float)atof(strf);
507 ::GetPrivateProfileString( "FundMatrix","F[1][1]","0.0",strf,100,projectName);
508 FundMatrix.m[1][1] = (float)atof(strf);
510 ::GetPrivateProfileString( "FundMatrix","F[1][2]","0.0",strf,100,projectName);
511 FundMatrix.m[1][2] = (float)atof(strf);
513 ::GetPrivateProfileString( "FundMatrix","F[2][0]","0.0",strf,100,projectName);
514 FundMatrix.m[2][0] = (float)atof(strf);
516 ::GetPrivateProfileString( "FundMatrix","F[2][1]","0.0",strf,100,projectName);
517 FundMatrix.m[2][1] = (float)atof(strf);
519 ::GetPrivateProfileString( "FundMatrix","F[2][2]","0.0",strf,100,projectName);
520 FundMatrix.m[2][2] = (float)atof(strf);
525 /* Try to read points and compute fundamental matrix */
526 /* Get number of points */
528 ::GetPrivateProfileString( "Pair1","NumberOfPoints","0",strf,100,projectName);
529 numPoints = atoi(strf);
537 points1 = cvCreateMat(2,numPoints,CV_32F);
538 points2 = cvCreateMat(2,numPoints,CV_32F);
539 fundMatr = cvCreateMat(3,3,CV_32F);
545 for( i = 0; i < numPoints; i++ )
547 sprintf(str,"LX%d",i);
548 ::GetPrivateProfileString( "Points1",str,"0",strf,100,projectName);
551 sprintf(str,"LY%d",i);
552 ::GetPrivateProfileString( "Points1",str,"0",strf,100,projectName);
555 sprintf(str,"RX%d",i);
556 ::GetPrivateProfileString( "Points1",str,"0",strf,100,projectName);
559 sprintf(str,"RY%d",i);
560 ::GetPrivateProfileString( "Points1",str,"0",strf,100,projectName);
563 cvmSet(points1,0,i,x1);
564 cvmSet(points1,1,i,y1);
566 cvmSet(points2,0,i,x2);
567 cvmSet(points2,1,i,y2);
572 numMatrs = cvFindFundamentalMat(points1,points2,fundMatr,CV_FM_RANSAC,1,0.99,0);
576 numMatrs = cvFindFundamentalMat(points1,points2,fundMatr,CV_FM_7POINT,0,0,0);
584 FundMatrix.m[i/3][i%3] = (float)cvmGet(fundMatr,i/3,i%3);
589 cvReleaseMat(&points1);
590 cvReleaseMat(&points2);
591 cvReleaseMat(&fundMatr);
600 FundMatrix.m[i/3][i%3] = 0;
612 /////////////////////////////
621 }/* in project > 0 pictures*/
627 /*======================================================================================*/
629 void CVMDemoDlg::SetWindowsToTop()
636 width = GetDeviceCaps( dc->m_hDC, HORZRES);
637 height = GetDeviceCaps( dc->m_hDC, VERTRES);
641 LeftImage -> MoveWindow(0, 0, width / 3, height / 3);
642 ResultImage -> MoveWindow(width / 3, 0, width / 3, height / 3);
643 RightImage -> MoveWindow(width - width / 3, 0, width / 3,height / 3);
647 /*======================================================================================*/
649 void CVMDemoDlg::OnMakemorphing()
664 image1 = cvCreateImageHeader( cvSize(1,1), 8, 3 );
665 image2 = cvCreateImageHeader( cvSize(1,1), 8, 3 );
666 imageRes = cvCreateImageHeader( cvSize(1,1), 8, 3 );
669 size1.width = RasterLeft->GetWidth();
670 size1.height = RasterLeft->GetHeight();
672 size2.width = RasterRight->GetWidth();
673 size2.height = RasterRight->GetHeight();
675 sizeRes.width = RasterRight->GetWidth();
676 sizeRes.height = RasterRight->GetHeight();
678 cvInitImageHeader( image1,
683 cvSetImageData( image1,
684 RasterLeft->GetImage(),
685 RasterLeft->GetSizeString() );
687 cvInitImageHeader( image2,
692 cvSetImageData( image2,
693 RasterRight->GetImage(),
694 RasterRight->GetSizeString() );
696 cvInitImageHeader( imageRes,
701 cvSetImageData( imageRes,
702 RasterResult->GetImage(),
703 RasterResult->GetSizeString() );
705 imgsize.width = RasterLeft->GetWidth();
706 imgsize.height = RasterLeft->GetHeight();
709 float pos = float(m_camerapos.GetPos());
710 pos = pos / (m_camerapos.GetRangeMax() - m_camerapos.GetRangeMin());
716 m_morpher.SetPan( alpha );
718 //#define SCANLINE_VARIANT 1
720 if( useScanlines == 1 )
722 //#if SCANLINE_VARIANT
723 cvMakeScanlines( &matrix,
735 if( useScanlines == 1 )
737 //#if SCANLINE_VARIANT
738 cvMakeScanlines( &matrix,
745 /* Prewarp first image */
746 cvPreWarpImage( numScanlines,
753 /* Prewarp second image */
754 cvPreWarpImage( numScanlines,
761 /* Create Runs on first Image */
762 cvFindRuns( numScanlines,
763 buffer_1, /* prewarp image */
765 lens_1, /* prewarp line lens in pixels */
767 runs_1, /* first runs */
769 num_runs_1, /* number of first runs */
772 cvDynamicCorrespondMulti(
773 numScanlines, // number of scanlines
774 runs_1, // s0|w0|s1|w1|...
775 num_runs_1, // numbers of runs
778 corr_1, // s0'|e0'|s1'|e1'|...
786 m_morpher.SetPointCount( 200 );
788 m_morpher.SetLeftImage( image1 );
789 m_morpher.SetRightImage( image2 );
796 if( useScanlines == 1 )
798 //#if SCANLINE_VARIANT
799 cvMakeAlphaScanlines( scanlines_1,
807 cvMorphEpilinesMulti(
808 numScanlines, /* number of lines */
809 buffer_1, /* raster epilines from the first image */
810 lens_1, /* number of pixel in first line */
811 buffer_2, /* raster epilines from the second image */
812 lens_2, /* number of pixel in second line */
813 morph_buffer, /* raster epiline from the destination image */
814 /* (it's an output parameter) */
815 morph_lens, /* numbers of pixel in output line */
816 alpha, /* relative position of camera */
817 runs_1, /* first sequence of runs */
818 num_runs_1, /* it's length */
819 runs_2, /* second sequence of runs */
821 corr_1, /* correspond information for the 1st seq */
822 corr_2); /* correspond information for the 2nd seq */
826 memset(RasterResult->GetImage(),0,imgsize.width * imgsize.height*3);
829 numScanlines, /* number of scanlines */
830 morph_buffer, /* source buffers */
831 morph_lens, /* lens of buffers */
832 imageRes, /* dest image */
833 scanlines_a /* scanline */
836 cvDeleteMoire( imageRes);
841 m_morpher.SetPan( alpha );
843 IplImage* img = m_morpher.GetVirtualImage();
844 //IplImage* img = m_morpher.GetDisparityImage(); // if disparity image needed
845 //iplCopy( m_morpher.GetVirtualImage(), imageRes );
846 cvCopy( img, imageRes );
850 ResultImage->RedrawWindow();
852 cvReleaseImageHeader(&image1);
853 cvReleaseImageHeader(&image2);
854 cvReleaseImageHeader(&imageRes);
859 /*======================================================================================*/
861 CVMDemoDlg::~CVMDemoDlg()
863 if (RasterLeft != 0) delete RasterLeft;
864 if (RasterRight != 0) delete RasterRight;
865 if (RasterResult != 0) delete RasterResult;
866 if (LeftImage != 0) delete LeftImage;
867 if (RightImage != 0) delete RightImage;
868 if (ResultImage != 0) delete ResultImage;
873 /*======================================================================================*/
875 void CVMDemoDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
878 ((CSliderCtrl*)(pScrollBar) == &m_camerapos)
880 if (abs(prevpos - m_camerapos.GetPos())>4 ) {
885 CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
889 /*======================================================================================*/
891 void CVMDemoDlg::OnOriginalsize()
897 /*======================================================================================*/
899 void CVMDemoDlg::FreeMemory()
902 if (scanlines_1 != 0) free (scanlines_1);
903 if (scanlines_2 != 0) free (scanlines_2);
904 if (scanlines_a != 0) free (scanlines_a);
906 if (lens_1 != 0) free (lens_1);
907 if (lens_2 != 0) free (lens_2);
908 if (morph_lens != 0) free (morph_lens);
910 if (buffer_1 != 0) free (buffer_1);
911 if (buffer_2 != 0) free (buffer_2);
912 if (morph_buffer != 0) free (morph_buffer);
914 if (runs_1 != 0) free (runs_1);
915 if (runs_2 != 0) free (runs_2);
917 if (corr_1 != 0) free (corr_1);
918 if (corr_2 != 0) free (corr_2);
920 if (num_runs_1 != 0) free (num_runs_1);
921 if (num_runs_2 != 0) free (num_runs_2);
948 /*======================================================================================*/
950 void CVMDemoDlg::AllocMemory()
954 imgsize.width = RasterLeft->GetWidth()*3;
955 imgsize.height = RasterLeft->GetHeight()*3;
968 scanlines_1 = (int*)(calloc( numlines * 2, sizeof(int) * 4));
969 scanlines_2 = (int*)(calloc( numlines * 2, sizeof(int) * 4));
970 scanlines_a = (int*)(calloc( numlines * 2, sizeof(int) * 4));
972 lens_1 = (int*)(calloc( numlines * 2, sizeof(int)*4));
973 lens_2 = (int*)(calloc( numlines * 2, sizeof(int)*4));
974 morph_lens = (int*)(calloc( numlines * 2, sizeof(int)*4));
977 (uchar*)(malloc(imgsize.width * (imgsize.height+1) * 3 * sizeof(uchar)));
980 (uchar*)(malloc(imgsize.width * (imgsize.height+1) * 3 * sizeof(uchar)));
983 (uchar*)(calloc(imgsize.width * (imgsize.height+1), 3 * sizeof(uchar)));
985 runs_1 = (int*)(calloc(imgsize.width * imgsize.height * 2, 2 * sizeof(int)));
986 runs_2 = (int*)(calloc(imgsize.width * imgsize.height * 2, 2 * sizeof(int)));
988 corr_1 = (int*)(calloc(imgsize.width * imgsize.height * 2, 2 * sizeof(int)));
989 corr_2 = (int*)(calloc(imgsize.width * imgsize.height * 2, 2 * sizeof(int)));
991 num_runs_1 = (int*)(calloc(imgsize.width * imgsize.height * 2, 2 * sizeof(int)));
992 num_runs_2 = (int*)(calloc(imgsize.width * imgsize.height * 2, 2 * sizeof(int)));
996 /*======================================================================================*/
998 void CVMDemoDlg::OnShowscanlines()
1000 bool canshow = (m_showscanlines.GetCheck()==1);
1001 LeftImage ->showScanlines = canshow;
1002 RightImage ->showScanlines = canshow;
1003 ResultImage ->showScanlines = canshow;
1005 LeftImage ->Invalidate();
1006 RightImage ->Invalidate();
1007 ResultImage ->Invalidate();
1009 // TODO: Add your control notification handler code here