zoukankan      html  css  js  c++  java
  • 1.0.x-学习Opencv与MFC混合编程之---视频运动检测

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

  • 相关阅读:
    使用RSA实现客户端和后台的非对称加密
    PHP异步并行网络扩展swoole安装
    rvm is not a function的解决方法
    python中的sort(),sorted()的用法
    RESTful框架中的Mixin中的五个类视图
    RESTful框架的API请求流程
    理解RESTful架构
    Django中的admin管理工具
    一个程序员的自我介绍--英文版
    Django中的认证系统
  • 原文地址:https://www.cnblogs.com/lidabo/p/3501303.html
Copyright © 2011-2022 走看看