我正在尝试使用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;
}
帧差是最简单的背景减法,但它对您使用的阈值非常敏感,可能得不到很好的效果。更好的方法是估计背景模型,比较每帧和背景模型来确定运动物体。
您可以从以下链接找到更多信息:
OpenCV(c++)中的背景减法
使用OpenCV 2进行背景减法