天天看点

1.0.x-学习Opencv与MFC混合编程之---视频运动检测

源代码地址: http://download.csdn.net/detail/nuptboyzhb/3961668

版本1.0.x新增内容

视频运动检测

Ø 新建菜单项,Learning OpenCV——> OpenCVr入门——>视频运动检测

Ø 菜单项设置如下:

1.0.x-学习Opencv与MFC混合编程之---视频运动检测

Ø 建立类向导

Ø 编辑代码

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]

                     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 )

                     }     

              }

              cvReleaseCapture( &capture );

              cvDestroyWindow(WindowName1);

              cvDestroyWindow("Histogram");

}

继续阅读