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

📄 vmdemodlg.cpp

📁 微软的基于HMM的人脸识别原代码, 非常经典的说
💻 CPP
📖 第 1 页 / 共 2 页
字号:


        if (    (RasterLeft -> GetWidth()	!= RasterRight -> GetWidth()) ||
                (RasterLeft -> GetHeight()	!= RasterRight -> GetHeight())
                ) {
            CanMorph	= false;
            m_imagesize.SetWindowText("");

        } else {
            CanMorph	= true;
            char st[300];
            sprintf(st,"%dx%d",RasterLeft -> GetWidth(),RasterLeft -> GetHeight());
            m_imagesize.SetWindowText(st);


            RasterResult	-> CreateRaster(    RasterLeft -> GetWidth(),
                                                RasterLeft -> GetHeight(),
                                                24);

            ResultImage		-> Invalidate();
        }



        /* Read Fundamental matrix */

        char strf[102];

        ::GetPrivateProfileString(  "FundMatrix","F[0][0]","0.0",strf,100,projectName);
        FundMatrix.m[0][0] = (float)atof(strf);

        ::GetPrivateProfileString(  "FundMatrix","F[0][1]","0.0",strf,100,projectName);
        FundMatrix.m[0][1] = (float)atof(strf);

        ::GetPrivateProfileString(  "FundMatrix","F[0][2]","0.0",strf,100,projectName);
        FundMatrix.m[0][2] = (float)atof(strf);

        ::GetPrivateProfileString(  "FundMatrix","F[1][0]","0.0",strf,100,projectName);
        FundMatrix.m[1][0] = (float)atof(strf);

        ::GetPrivateProfileString(  "FundMatrix","F[1][1]","0.0",strf,100,projectName);
        FundMatrix.m[1][1] = (float)atof(strf);

        ::GetPrivateProfileString(  "FundMatrix","F[1][2]","0.0",strf,100,projectName);
        FundMatrix.m[1][2] = (float)atof(strf);

        ::GetPrivateProfileString(  "FundMatrix","F[2][0]","0.0",strf,100,projectName);
        FundMatrix.m[2][0] = (float)atof(strf);

        ::GetPrivateProfileString(  "FundMatrix","F[2][1]","0.0",strf,100,projectName);
        FundMatrix.m[2][1] = (float)atof(strf);

        ::GetPrivateProfileString(  "FundMatrix","F[2][2]","0.0",strf,100,projectName);
        FundMatrix.m[2][2] = (float)atof(strf);




        /////////////////////////////


        FreeMemory();
        AllocMemory();
        HaveMorph   = false;



        }/* in project > 0 pictures*/
    }/* if dialog */

}


/*======================================================================================*/

void CVMDemoDlg::SetWindowsToTop()
{
    CDC *dc;
    dc      = GetDC();

    int width,height;

    width   = GetDeviceCaps( dc->m_hDC, HORZRES);
    height  = GetDeviceCaps( dc->m_hDC, VERTRES);

    ReleaseDC(dc);

    LeftImage   -> MoveWindow(0, 0, width / 3, height / 3);
    ResultImage -> MoveWindow(width / 3, 0, width / 3, height / 3);
    RightImage  -> MoveWindow(width - width / 3, 0, width / 3,height / 3);
}


/*======================================================================================*/

void CVMDemoDlg::OnMakemorphing()
{
    if (CanMorph) {
        CvSize imgsize;
        CvMatrix3  matrix;

        IplImage* image1;
        CvSize    size1;
        
        IplImage* image2;
        CvSize    size2;

        IplImage* imageRes;
        CvSize    sizeRes;

        image1   = cvCreateImageHeader( cvSize(1,1), 8, 3 );
        image2   = cvCreateImageHeader( cvSize(1,1), 8, 3 );
        imageRes = cvCreateImageHeader( cvSize(1,1), 8, 3 );


        size1.width   = RasterLeft->GetWidth();
        size1.height  = RasterLeft->GetHeight();

        size2.width   = RasterRight->GetWidth();
        size2.height  = RasterRight->GetHeight();

        sizeRes.width   = RasterRight->GetWidth();
        sizeRes.height  = RasterRight->GetHeight();

        cvInitImageHeader(  image1,
                            size1,
                            8,
                            3,
                            0,
                            4,
                            0);
        
        cvSetImageData( image1,
                        RasterLeft->GetImage(),
                        RasterLeft->GetSizeString() ); 
        
        cvInitImageHeader(  image2,
                            size2,
                            8,
                            3,
                            0,
                            4,
                            0);
        
        cvSetImageData( image2,
                        RasterRight->GetImage(),
                        RasterRight->GetSizeString() ); 

        cvInitImageHeader(  imageRes,
                            sizeRes,
                            8,
                            3,
                            0,
                            4,
                            0);
        
        cvSetImageData( imageRes,
                        RasterResult->GetImage(),
                        RasterResult->GetSizeString() ); 

        imgsize.width   = RasterLeft->GetWidth();
        imgsize.height  = RasterLeft->GetHeight();


        float pos = float(m_camerapos.GetPos());
        pos = pos / (m_camerapos.GetRangeMax() - m_camerapos.GetRangeMin());

        float alpha = pos;

        matrix = FundMatrix;

        m_morpher.SetPan( alpha );

//#define SCANLINE_VARIANT 1

        if( useScanlines == 1 )
        {
//#if SCANLINE_VARIANT
        cvMakeScanlines(    &matrix,
                                    imgsize,
                                    0,
                                    0,
                                    0,
                                    0,
                                    &numScanlines);
        }
//#endif

        if (!HaveMorph) {

        if( useScanlines == 1 )
        {
//#if SCANLINE_VARIANT
            cvMakeScanlines(    &matrix,
                                        imgsize,
                                        scanlines_1,
                                        scanlines_2,
                                        lens_1,
                                        lens_2,
                                        &numScanlines);
            /* Prewarp first image */
            cvPreWarpImage( numScanlines,
                                        image1,
                                        buffer_1,
                                        lens_1,
                                        scanlines_1);
            

            /* Prewarp second image */
            cvPreWarpImage( numScanlines,
                                        image2,
                                        buffer_2,
                                        lens_2,
                                        scanlines_2);
            

            /* Create Runs on first Image */
            cvFindRuns(          numScanlines,
                                        buffer_1,   /* prewarp image				*/
                                        buffer_2,
                                        lens_1,     /* prewarp line lens in pixels	*/
                                        lens_2,
                                        runs_1,     /* first runs					*/
                                        runs_2,
                                        num_runs_1, /* number of first runs			*/
                                        num_runs_2);

            cvDynamicCorrespondMulti(
                                                numScanlines,         // number of scanlines
                                                runs_1,         // s0|w0|s1|w1|...
                                                num_runs_1,    // numbers of runs
                                                runs_2,
                                                num_runs_2,
                                                corr_1,    // s0'|e0'|s1'|e1'|...

                                                        corr_2 );
        }
        else
        {
//#else
            #ifndef PIXELTOPIXEL
            m_morpher.SetPointCount( 200 );
            #endif
            m_morpher.SetLeftImage( image1 );
            m_morpher.SetRightImage( image2 );
//#endif
        }
            HaveMorph   = true;

        }

        if( useScanlines == 1 )
        {
//#if SCANLINE_VARIANT
        cvMakeAlphaScanlines(    scanlines_1,
                                        scanlines_2,
                                        scanlines_a,
                                        morph_lens,
                                        numScanlines,
                                        alpha);


        cvMorphEpilinesMulti(
                        numScanlines,       /* number of lines                              */
                        buffer_1,       /* raster epilines from the first image         */
                        lens_1,         /* number of pixel in first line                */
                        buffer_2,       /* raster epilines from the second image        */
                        lens_2,         /* number of pixel in second line               */
                        morph_buffer,   /* raster epiline from the destination image    */
                                        /* (it's an output parameter)                   */
                        morph_lens,     /* numbers of pixel in output line              */
                        alpha,          /* relative position of camera                  */
                        runs_1,         /* first sequence of runs                       */
                        num_runs_1,     /* it's length                                  */
                        runs_2,         /* second sequence of runs                      */
                        num_runs_2,     /*                                              */
                        corr_1,         /* correspond information for the 1st seq       */
                        corr_2);        /* correspond information for the 2nd seq       */
        


        memset(RasterResult->GetImage(),0,imgsize.width * imgsize.height*3);

        cvPostWarpImage(
                    numScanlines,                       /* number of scanlines  */
                    morph_buffer,                   /* source buffers       */
                    morph_lens,                     /* lens of buffers      */
                    imageRes,       /* dest image           */
                    scanlines_a                     /* scanline             */
            );
        
        cvDeleteMoire(  imageRes);
        }
        else
        {
//#else
        m_morpher.SetPan( alpha );
        {
            IplImage* img = m_morpher.GetVirtualImage();
            //IplImage* img = m_morpher.GetDisparityImage(); // if disparity image needed
            //iplCopy( m_morpher.GetVirtualImage(), imageRes );
            iplCopy( img, imageRes );
        }
//#endif
        }
        ResultImage->RedrawWindow();

        cvReleaseImageHeader(&image1);
        cvReleaseImageHeader(&image2);
        cvReleaseImageHeader(&imageRes);
    }/* if CanMorph */
}


/*======================================================================================*/

CVMDemoDlg::~CVMDemoDlg()
{
    if (RasterLeft      != 0)   delete RasterLeft;
    if (RasterRight     != 0)   delete RasterRight;
    if (RasterResult    != 0)   delete RasterResult;
    if (LeftImage       != 0)   delete LeftImage;
    if (RightImage      != 0)   delete RightImage;
    if (ResultImage     != 0)   delete ResultImage;
    FreeMemory();
}


/*======================================================================================*/

void CVMDemoDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
    if (
        ((CSliderCtrl*)(pScrollBar) == &m_camerapos)
        ) {
        if (abs(prevpos - m_camerapos.GetPos())>4 ) {

            OnMakemorphing();
        }
    }
    CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}


/*======================================================================================*/

void CVMDemoDlg::OnOriginalsize()
{
    SetWindowsToTop();
}


/*======================================================================================*/

void CVMDemoDlg::FreeMemory()
{

    if (scanlines_1     != 0)   free (scanlines_1);
    if (scanlines_2     != 0)   free (scanlines_2);
    if (scanlines_a     != 0)   free (scanlines_a);

    if (lens_1          != 0)   free (lens_1);
    if (lens_2          != 0)   free (lens_2);
    if (morph_lens      != 0)   free (morph_lens);

    if (buffer_1        != 0)   free (buffer_1);
    if (buffer_2        != 0)   free (buffer_2);
    if (morph_buffer    != 0)   free (morph_buffer);

    if (runs_1          != 0)   free (runs_1);
    if (runs_2          != 0)   free (runs_2);

    if (corr_1          != 0)   free (corr_1);
    if (corr_2          != 0)   free (corr_2);

    if (num_runs_1      != 0)   free (num_runs_1);
    if (num_runs_2      != 0)   free (num_runs_2);


    scanlines_1     = 0;
    scanlines_2     = 0;
    scanlines_a     = 0;

    lens_1          = 0;
    lens_2          = 0;
    morph_lens      = 0;

    runs_1          = 0;
    runs_2          = 0;

    buffer_1        = 0;
    buffer_2        = 0;
    morph_buffer    = 0;

    num_runs_1      = 0;
    num_runs_2      = 0;

    corr_1          = 0;
    corr_2          = 0;

}


/*======================================================================================*/

void CVMDemoDlg::AllocMemory()
{
    CvSize     imgsize;

    imgsize.width   = RasterLeft->GetWidth()*3;
    imgsize.height  = RasterLeft->GetHeight()*3;

    int numlines;

    cvMakeScanlines(     0,
                                imgsize,
                                0,
                                0,
                                0,
                                0,
                                &numlines);

    numlines=2400;
    scanlines_1     = (int*)(calloc( numlines * 2, sizeof(int) * 4));
    scanlines_2     = (int*)(calloc( numlines * 2, sizeof(int) * 4));
    scanlines_a     = (int*)(calloc( numlines * 2, sizeof(int) * 4));

    lens_1          = (int*)(calloc( numlines * 2, sizeof(int)*4));
    lens_2          = (int*)(calloc( numlines * 2, sizeof(int)*4));
    morph_lens      = (int*)(calloc( numlines * 2, sizeof(int)*4));

    buffer_1        =
        (uchar*)(malloc(imgsize.width * (imgsize.height+1) * 3 * sizeof(uchar)));

    buffer_2        =
        (uchar*)(malloc(imgsize.width * (imgsize.height+1) * 3 * sizeof(uchar)));

    morph_buffer    =
        (uchar*)(calloc(imgsize.width * (imgsize.height+1), 3 * sizeof(uchar)));

    runs_1          = (int*)(calloc(imgsize.width * imgsize.height * 2, 2 * sizeof(int)));
    runs_2          = (int*)(calloc(imgsize.width * imgsize.height * 2, 2 * sizeof(int)));

    corr_1          = (int*)(calloc(imgsize.width * imgsize.height * 2, 2 * sizeof(int)));
    corr_2          = (int*)(calloc(imgsize.width * imgsize.height * 2, 2 * sizeof(int)));

    num_runs_1      = (int*)(calloc(imgsize.width * imgsize.height * 2, 2 * sizeof(int)));
    num_runs_2      = (int*)(calloc(imgsize.width * imgsize.height * 2, 2 * sizeof(int)));

}

/*======================================================================================*/

void CVMDemoDlg::OnShowscanlines() 
{
    bool canshow = (m_showscanlines.GetCheck()==1);
    LeftImage    ->showScanlines = canshow;
    RightImage   ->showScanlines = canshow;
    ResultImage ->showScanlines = canshow;

    LeftImage    ->Invalidate();
    RightImage   ->Invalidate();
    ResultImage ->Invalidate();

    // TODO: Add your control notification handler code here
	
}

⌨️ 快捷键说明

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