1.0.x-学习Opencv与MFC混合编程之---视频运动检测-阿里云开发者社区

开发者社区> 人工智能> 正文
登录阅读全文

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

简介: 源代码地址: http://download.csdn.net/detail/nuptboyzhb/3961668 版本1.0.x新增内容 视频运动检测 Ø 新建菜单项,Learning OpenCV——> OpenCVr入门——>视频运动检测 Ø 菜单项设置如下: Ø 建立类向导 ...

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

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
人工智能
使用钉钉扫一扫加入圈子
+ 订阅

了解行业+人工智能最先进的技术和实践,参与行业+人工智能实践项目

其他文章
最新文章
相关文章