DoubleLi

qq: 517712484 wx: ldbgliet

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

源代码地址: 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");

       }

      

}

 

 

from:http://blog.csdn.net/nupt123456789/article/details/7100114

posted on 2014-01-02 09:23  DoubleLi  阅读(434)  评论(0编辑  收藏  举报