Video processing, often do some pedestrian, vehicle or other moving objects cross-line detection, cross-border detection.
Original video stream:
Below with OPENCV introduction two ways, Draw straight line (cross line, mix line):
First: Fixed the first frame, or paused the video, in a fixed frame to complete the line drawing function
#include <iostream>
using namespace std;
#include <opencv2 \ core \ core.hpp>
#include <opencv2 \ highgui \ highgui.hpp>
#include <opencv2 \ imgproc \ imgproc.hpp>
using namespace cv;
/ * -------------------------------- define mouse events--draw straight lines --------- ----------------- * /
bool got_line = false;
// Global variables
Point beginPoint = Point (0,0); //-Note that there is an initialized (0,0)
bool got_beigin_point = false;
Point endPoint = Point (0,0); //-Note that this has its own default initialization (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 [])
{
// Read video
VideoCapture video (argv [1]);
Ranch
// Determine if the video is open
if (! video.isOpened ())
return 0;
// The first frame in the video
Mat firstFrame;
Ranch
Mat frame;
// Read the first frame of the video
video >> frame;
// Copy to firstFrame
frame.copyTo (firstFrame);
Ranch
// register
namedWindow ("video", 1);
setMouseCallback ("video", mouseLineHandler, NULL);
Ranch
// Draw lines
while (! got_line)
{
firstFrame.copyTo (frame);
line (frame, beginPoint, endPoint, Scalar (255,0,0), 2);
imshow ("video", frame);
if (waitKey (50) == ‘q‘) // --------- very important
break;
}
// remove callback
setMouseCallback ("video", NULL, NULL);
Ranch
// video continues
for (;;)
{
video >> frame;
line (frame, beginPoint, endPoint, Scalar (255,255,0), 2);
imshow ("video", frame);
if (waitKey (33) == ‘q’)
break;
}
return 0;
}
/ * ------------------------------------------------ ----------------- * /
"Result":You'd better try it yourself to see the difference.
The second type: without affecting the video playback, complete the function of drawing straight line
#include <iostream>
using namespace std;
#include <opencv2 \ core \ core.hpp>
#include <opencv2 \ highgui \ highgui.hpp>
#include <opencv2 \ imgproc \ imgproc.hpp>
using namespace cv;
/ * -------------------------------- define mouse events--draw straight lines --------- ----------------- * /
bool got_line = false;
// Global variables
Point beginPoint = Point (0,0); //-Note that there is an initialized (0,0)
bool got_beigin_point = false;
Point endPoint = Point (0,0); //-Note that this has its own default initialization (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 [])
{
// Read video
VideoCapture video (argv [1]);
Ranch
// Determine if the video is open
if (! video.isOpened ())
return 0;
// Video frame
Mat frame;
Ranch
// register
namedWindow ("video", 1);
setMouseCallback ("video", mouseLineHandler, NULL);
Ranch
// Draw straight lines
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);
Ranch
// video continues
for (;;)
{
video >> frame;
line (frame, beginPoint, endPoint, Scalar (0,255,255), 2);
imshow ("video", frame);
if (waitKey (33) == ‘q’)
break;
}
return 0;
}
/ * ------------------------------------------------ ----------------- * /
"Result":
opencv-Video Processing-draw line (cross line, mix line)