1.0.x-學習Opencv與MFC混合編程之—視頻運動檢測

來源:互聯網
上載者:User

原始碼地址: http://download.csdn.net/detail/nuptboyzhb/3961668

版本1.0.x新增內容

視頻運動檢測

Ø 建立功能表項目,Learning OpenCV——> OpenCVr入門——>視頻運動檢測

Ø 功能表項目設定如下:

Ø 建立類嚮導

Ø 編輯代碼

voidCCVMFCView::OnMyTestSport()

{

       // TODO: Add your command handler codehere

       //Alt+F8 整理代碼

       CvCapture* capture;

       capture = cvCreateCameraCapture(0);//開啟網路攝影機

       if(capture==NULL)

       {

              MessageBox("未檢測到網路攝影機,請檢查網路攝影機安裝是否正確");

       }

       else

       {

             

              CString WindowName1="攝像機";

              IplImage *frame1 = 0;//第一幀

              IplImage *frame2 = 0;//第二幀

              IplImage *frame_sub= 0;//相減之後的幀

              IplImage* pImg8u = NULL;//灰階圖

        //

              assert(capture != NULL );//如果沒有開啟,中斷

              cvNamedWindow(WindowName1,CV_WINDOW_AUTOSIZE);

              frame1= cvQueryFrame(capture);

              frame_sub=cvCloneImage(frame1);

              while(1)

              {

                     frame2=cvQueryFrame(capture);

                     if(!frame2)

                     {

                            break;

                     }

            cvAbsDiff(frame1,frame2,frame_sub);

                     frame1=cvCloneImage(frame2);

                 cvMoveWindow(WindowName1, 150, 200);

                     cvShowImage(WindowName1,frame_sub);

                    

                     //  建立位元影像

                     pImg8u =cvCreateImage(cvGetSize(frame_sub),IPL_DEPTH_8U,1);

                     cvCvtColor(frame_sub,pImg8u,CV_BGR2GRAY);  //彩色變灰階

            //--------------一下是計算長條圖-----------------------------

                     IplImage *src;

                     IplImage *histimg = 0;

                     CvHistogram *hist = 0;

                     int hdims = 256;     // 劃分HIST的個數,越高越精確

                     src=cvCloneImage(pImg8u);

                     float hranges_arr[] ={0,255};

                     float* hranges =hranges_arr;

                     int bin_w; 

                     float max_val;

                     int i;

                     cvNamedWindow("Histogram", 0 );

                    

                     hist = cvCreateHist( 1,&hdims, CV_HIST_ARRAY, &hranges, 1 ); // 計算長條圖

                     histimg= cvCreateImage( cvSize(320,200), 8, 3 );

                     cvZero( histimg );

                     cvCalcHist( &src, hist,0, 0 ); // 計算長條圖

                     cvGetMinMaxHistValue( hist,0, &max_val, 0, 0 );  // 只找最大值

                     cvConvertScale(hist->bins, hist->bins,

                            max_val ? 255. /max_val : 0., 0 ); // 縮放 bin 到區間 [0,255]

                     cvZero( histimg );

                     bin_w = histimg->width /hdims;  // hdims: 條的個數,則 bin_w 為條的寬度

                    

                     // 畫長條圖

                     for( i = 0; i < hdims;i++ )

                     {

                            double val = (cvGetReal1D(hist->bins,i)*histimg->height/255 );

                            CvScalar color =CV_RGB(255,255,0); //(hsv2rgb(i*180.f/hdims);

                            cvRectangle(histimg, cvPoint(i*bin_w,histimg->height),

                                   cvPoint((i+1)*bin_w,(int)(histimg->height- val)),

                                   color, 1, 8,0 );

                     }

                     cvMoveWindow("Histogram",550, 200);

                     cvShowImage( "Histogram",histimg );

                    

                     //------------------------------------------------------

                     char c = cvWaitKey(1);

                     if( c == 27 )

                     {

                            break;

                     }     

              }

              cvReleaseCapture( &capture );

              cvDestroyWindow(WindowName1);

              cvDestroyWindow("Histogram");

       }

      

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.