opencv-Video Processing-draw line (cross line, mix line)

Source: Internet
Author: User
Tags scalar



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)


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.