Ask Your Question

Revision history [back]

please try the code below ( i hope it is not too cryptographic )

#include <iostream>
#include <opencv2/highgui.hpp>
#include <opencv2/tracking.hpp>
#include "samples_utility.hpp"

using namespace std;
using namespace cv;

void addPatch(Mat& canvas, Mat& frame, Rect rect, Size sz, int index)
{
    Rect r_patch(10+(10*index)+(sz.width*index), frame.rows + 20, sz.width, sz.height);
    Mat m_patch = canvas(r_patch);
    resize(frame(rect), m_patch, sz);
}

int main()
{
    Mat frame;

    MultiTracker trackers;
    String trackingAlg = "CSRT";
    vector<Rect> ROIs;
    vector<Rect2d> objects;
    vector<Ptr<Tracker> > algorithms;

    string video = "vtest.avi";
    VideoCapture cap(video);

    cap >> frame;

    selectROIs("tracker", frame, ROIs, 0, 0);

    for (size_t i = 0; i < ROIs.size(); i++)
    {
        algorithms.push_back(createTrackerByName(trackingAlg));
        objects.push_back(ROIs[i]);
    }

    trackers.add(algorithms, frame, objects);

    while (true)
    {
        cap >> frame;
        Mat canvas(frame.rows + 150, frame.cols + 50, CV_8UC3, Scalar(127, 127, 127));

        Rect r(10, 10, frame.cols, frame.rows);
        frame.copyTo(canvas(r));

        trackers.update(frame);

        for (unsigned i = 0; i < trackers.getObjects().size(); i++)
        {
            rectangle(canvas, trackers.getObjects()[i], Scalar::all(255), 2);
        }

        Size sz(100, 100);
        Mat rois;
        vector<Rect2d> rects = trackers.getObjects();

        for (unsigned i = 0; i < rects.size(); i++)
        {
            addPatch(canvas,frame,rects[i], sz, i);
        }

        imshow("Frame", frame);
        imshow("Canvas", canvas);
        waitKey(1);
    }
    destroyAllWindows();
    return 0;
}