Ask Your Question

Revision history [back]

You need to call your mouse call back function outside the loop. Capture the first frame, call the mouse callback function to select your ROI, initialize your tracker and in the main function, get the second frame and update your tracker. OpenCV has examples for different trackers. Here's an example.

But here's the code for selecting ROI

#include <opencv2/opencv.hpp>
#include<iostream>

using namespace cv;
using namespace std;

Point point1, point2;
int drag = 0;
Rect roi;
Mat frame;

void mouse(int event, int x, int y, int flags, void* param)
{
    if (event == CV_EVENT_LBUTTONDOWN && !drag)
    {
        point1 = Point(x, y);
        drag = 1;
    }
    if (event == CV_EVENT_MOUSEMOVE && drag)
    {
        Mat img = frame.clone();
        point2 = Point(x, y);
        rectangle(img, point1, point2, Scalar(0, 0, 255), 1, 8, 0);
        imshow("Video", img);
    }
    if (event == CV_EVENT_LBUTTONUP && drag)
    {
        point2 = Point(x, y);
        drag = 0;
        roi = Rect(point1.x, point1.y, x - point1.x, y - point1.y);
    }
    if (event == CV_EVENT_LBUTTONUP)
    {
        drag = 0;
    }
}

int main()
{

    cout << "Select ROI and press any key to continue\n";
    VideoCapture cap(0);

    cap >> frame;

    imshow("Video", frame);
    cvSetMouseCallback("Video", mouse, NULL);

    /*initialize your tracker*/

    waitKey(0);

    while (1)
    {
        cap >> frame;

        /*Update your Tracker*/

        rectangle(frame, roi, Scalar(0, 0, 255), 1, 8, 0);
        imshow("Video", frame);

        if (waitKey(30) >= 0)
            break;
    }
    return 0;
}

You need to call your mouse call back function outside the loop. Capture the first frame, call the mouse callback function to select your ROI, initialize your tracker and in the main function, get the second frame and update your tracker. OpenCV has examples for different trackers. Here's an example.

But here's the code for selecting ROI

#include <opencv2/opencv.hpp>
#include<iostream>

using namespace cv;
using namespace std;

Point point1, point2;
int drag = 0;
Rect roi;
Mat frame;

void mouse(int event, int x, int y, int flags, void* param)
{
    if (event == CV_EVENT_LBUTTONDOWN && !drag)
    {
        point1 = Point(x, y);
        drag = 1;
    }
    if (event == CV_EVENT_MOUSEMOVE && drag)
    {
        Mat img = frame.clone();
        point2 = Point(x, y);
        rectangle(img, point1, point2, Scalar(0, 0, 255), 1, 8, 0);
        imshow("Video", img);
    }
    if (event == CV_EVENT_LBUTTONUP && drag)
    {
        point2 = Point(x, y);
        drag = 0;
        roi = Rect(point1.x, point1.y, x - point1.x, y - point1.y);
    }
    if (event == CV_EVENT_LBUTTONUP)
    {
        drag = 0;
    }
}

int main()
{

    cout << "Select ROI and press any key to continue\n";
    VideoCapture cap(0);

    cap >> frame;
frame;//get the first frame

    imshow("Video", frame);
    cvSetMouseCallback("Video", mouse, NULL);
NULL);//call the MouseCallBack function to select ROI

    /*initialize your tracker*/

    waitKey(0);

    while (1)
    {
        cap >> frame;
frame;//capture second frame

        /*Update your Tracker*/

        rectangle(frame, roi, Scalar(0, 0, 255), 1, 8, 0);
        imshow("Video", frame);

        if (waitKey(30) >= 0)
            break;
    }
    return 0;
}