视频处理中,经常有做一些行人、车辆或者其它运动物体越线检测,越界检测。
原视频流:
下面用opencv介绍两种方式,画直线(越线、拌线):
第一种:固定第一帧,或者暂停视频,在固定的一帧中完成画直线的功能
#include<iostream>
using namespace std;
#include<opencv2\core\core.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\imgproc\imgproc.hpp>
using namespace cv;
/*--------------------------------定义鼠标事件--画直线--------------------------*/
bool got_line = false;
//全局变量
Point beginPoint=Point(0,0);//--注意这个有一个初始化的(0,0)
bool got_beigin_point = false;
Point endPoint=Point(0,0);//--注意这个有一个自己默认的初始化(0,0)
void mouseLineHandler(int event, int x, int y, int flags, void *param)
{
switch(event)
{
case CV_EVENT_LBUTTONDOWN:
beginPoint = Point(x,y);
endPoint = beginPoint;
got_beigin_point = true;
break;
case CV_EVENT_MOUSEMOVE:
if(got_beigin_point)
{
endPoint = Point(x,y);
}
break;
case CV_EVENT_LBUTTONUP:
got_line = true;
endPoint = Point(x,y);
break;
default:
break;
}
}
/*---------------------------------------------------------------------------------------------*/
int main(int argc,char*argv[])
{
//读取视频
VideoCapture video(argv[1]);
//判断视频是否打开
if( !video.isOpened())
return 0;
//视频中的第一帧
Mat firstFrame;
Mat frame;
//读取视频的第一帧
video>>frame;
//复制到firstFrame中
frame.copyTo(firstFrame);
//register
namedWindow("video",1);
setMouseCallback("video",mouseLineHandler,NULL);
//画线
while(!got_line)
{
firstFrame.copyTo(frame);
line(frame,beginPoint,endPoint,Scalar(255,0,0),2);
imshow("video",frame);
if(waitKey(50) == 'q')//---------很重要
break;
}
//remove callback
setMouseCallback("video",NULL,NULL);
//视频继续
for(;;)
{
video>>frame;
line(frame,beginPoint,endPoint,Scalar(255,255,0),2);
imshow("video",frame);
if(waitKey(33) == 'q')
break;
}
return 0;
}
/*-----------------------------------------------------------------*/
【结果】:最好亲手试一下,才能看出区别
第二种:不影响视频播放的情况下,完成画直线的功能
#include<iostream>
using namespace std;
#include<opencv2\core\core.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\imgproc\imgproc.hpp>
using namespace cv;
/*--------------------------------定义鼠标事件--画直线--------------------------*/
bool got_line = false;
//全局变量
Point beginPoint=Point(0,0);//--注意这个有一个初始化的(0,0)
bool got_beigin_point = false;
Point endPoint=Point(0,0);//--注意这个有一个自己默认的初始化(0,0)
void mouseLineHandler(int event, int x, int y, int flags, void *param)
{
switch(event)
{
case CV_EVENT_LBUTTONDOWN:
beginPoint = Point(x,y);
endPoint = beginPoint;
got_beigin_point = true;
break;
case CV_EVENT_MOUSEMOVE:
if(got_beigin_point)
{
endPoint = Point(x,y);
}
break;
case CV_EVENT_LBUTTONUP:
got_line = true;
endPoint = Point(x,y);
break;
default:
break;
}
}
/*---------------------------------------------------------------------------------------------*/
int main(int argc,char*argv[])
{
//读取视频
VideoCapture video(argv[1]);
//判断视频是否打开
if( !video.isOpened())
return 0;
//视频帧
Mat frame;
//register
namedWindow("video",1);
setMouseCallback("video",mouseLineHandler,NULL);
//画直线
for(;;)
{
video>>frame;
line(frame,beginPoint,endPoint,Scalar(0,255,255),2);
imshow("video",frame);
if(got_line)
break;
if(waitKey(50)=='q')
break;
}
//remove callback
setMouseCallback("video",NULL,NULL);
//视频继续
for(;;)
{
video>>frame;
line(frame,beginPoint,endPoint,Scalar(0,255,255),2);
imshow("video",frame);
if(waitKey(33) == 'q')
break;
}
return 0;
}
/*-----------------------------------------------------------------*/
【结果】:
根据这一篇得到了视频中的直线,然后再根据前景检测,就可以得到运动物体的越线检测,拌线检测