如何实现运动对象的计数
目前在使用opencv对视频中运动人体进行识别、提取。目前实现了单一人体的识别与检测,现在需要对视频中的人进行计数。主要的应用场景是:对一个一个或长或短的视频进行处理,在每一帧中只有单一的人在运动,但是不同时刻运动的人不一样,需要对人进行编号存储。
请教如何对运动的人体进行计数,并且不存储没有人在镜头里的帧
以下是目前以实现的代码,请高人指点如何实现运动对象的计数功能,并仅存储包含对象的帧
int main(int argc, const char** argv)
{
VideoCapture cap;
bool update_bg_model = true;
void writefile(Mat,int);
cap.open(argv[1]);
if( !cap.isOpened() )
{
printf("can not open camera or video file\n");
return -1;
}
BackgroundSubtractorMOG2 bg_model;
Mat img, fgmask, fgimg;
int i=0;
for(;;)
{
cap >> img;
if( img.empty() )
break;
if( fgimg.empty() )
fgimg.create(img.size(), img.type());
//update the model
bg_model(img, fgmask, update_bg_model ? -1 : 0);
fgimg = Scalar::all(0);
img.copyTo(fgimg, fgmask);
Mat bgimg;
bg_model.getBackgroundImage(bgimg);
imshow( "image", img );
imshow("single",fgmask);
imshow("color",fgimg);
imshow("background image",bgimg);
writefile(fgimg,i);
writefile(fgmask,i);
i++;
char k = (char)waitKey(30);
if( k == 27 ) break;
if( k == ' ' )
{
update_bg_model = !update_bg_model;
if(update_bg_model)
printf("Background update is on\n");
else
printf("Background update is off\n");
}
}
return 0;
}
void writefile(Mat img,int num)
{
string filename;
stringstream stream;
stream<<num;
if (img.channels()==1)
{
filename = "single_pipe"+stream.str()+".jpg";
filename = "E:\\opencv\\软件所需\\对象提取与存储\\single_pipe\"+filename;
imwrite(filename,img);
}
else
{
filename = "color_pipe"+stream.str()+".jpg";
filename = "E:\\opencv\\软件所需\\对象提取与存储\\color_pipe\"+filename;
imwrite(filename,img);
}
}