0

我正在尝试使用 opencv 2.4.2 进行对象检测。这是我的移动前景减法的代码。现在我想检测原始帧中的移动对象并在其周围绘制边界框。有人可以帮帮我吗?怎么做?

   #include "stdafx.h"
   #include "opencv2/imgproc/imgproc.hpp"
   #include "opencv2/highgui/highgui.hpp"
   #include "opencv2/contrib/contrib.hpp"
   #include "conio.h"

   #include "time.h"
   #include "opencv/cvaux.hpp"
   #include "opencv2/core/core.hpp"
  #include "opencv2/calib3d/calib3d.hpp"

  using namespace std;
 using namespace cv;
   int main(int argc, char *argv[])
  {  


  int key = 0; 

    CvSize imgSize;
    CvCapture* capture = cvCaptureFromFile( "S:\\offline object detection   database\\TwoEnterShop2cor.MPG" );

         IplImage* frame = cvQueryFrame( capture );
         imgSize = cvGetSize(frame);

        IplImage* grayImage = cvCreateImage( imgSize, IPL_DEPTH_8U, 1);
        IplImage* currframe = cvCreateImage(imgSize,IPL_DEPTH_8U,3);
         IplImage* destframe = cvCreateImage(imgSize,IPL_DEPTH_8U,3);
    if ( !capture ) 

  {  
    fprintf( stderr, "Cannot open AVI!\n" );  
    return 1;  
  }


    int fps = ( int )cvGetCaptureProperty( capture, CV_CAP_PROP_FPS );

   cvNamedWindow( "dest", CV_WINDOW_AUTOSIZE );

   while( key != 'y' )
  {
  frame = cvQueryFrame( capture );

   currframe = cvCloneImage( frame );// copy frame to current
   frame = cvQueryFrame( capture );// grab frame
   cvAbsDiff(frame,currframe,destframe);

cvCvtColor(destframe,grayImage,CV_RGB2GRAY);
cvSmooth(grayImage,grayImage,CV_MEDIAN,3,3,0);
   cvAdaptiveThreshold(grayImage,grayImage,230,CV_THRESH_BINARY,CV_ADAPTIVE_THRESH_GAUSSIAN_C,3,5);

     cvDilate(grayImage, grayImage, 0,1);
 cvErode(grayImage,grayImage, 0, 0);
  if(key==27 )break;
  cvShowImage( "fram",currframe);
  cvShowImage( "dest",grayImage);
   key = cvWaitKey( 100 );
   }  
  cvDestroyWindow( "dest" );
  cvReleaseCapture( &capture );
  return 0;
    }
4

1 回答 1

0

帧差是最简单的背景减法方法,但是它对你使用的阈值非常敏感,可能不会得到好的结果。更好的方法是估计背景模型,比较每一帧和背景模型来确定运动物体。

您可以从以下链接中找到更多信息:
OpenCV(C++) 中的
背景减法 OpenCV 2中的背景减法

于 2013-03-09T11:27:07.863 回答