获取已在OpenCV C ++中的跟踪算法中跟踪的ROI的x和y像素坐标

时间:2018-09-26 11:18:55

标签: opencv computer-vision video-tracking

我有一个如下所示的跟踪程序。我在视频的第一帧中绘制了两个ROI(矩形框),并且在整个视频中跟踪了这些ROI中的对象。我想获取将在视频中跟踪的ROI的x&y坐标(即,视频所有帧中2个ROI的位置)。我可以在调试时看到这些值,但是当我尝试使用bboxes[0].x,bboxes[0].y,bboxes[1].x,bboxes[1].y为每帧打印它们时,所有帧的值都相同。我在这里做什么错了?

#include <opencv2/opencv.hpp>
#include <opencv2/tracking.hpp>
#include "opencv2/highgui.hpp"
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"

using namespace cv;
using namespace std;

// Convert to string
//#define SSTR( x ) static_cast< std::ostringstream & >( \
( std::ostringstream() << std::dec << x ) ).str()

vector<string> trackerTypes = { "BOOSTING", "MIL", "KCF", "TLD", "MEDIANFLOW", "GOTURN", "MOSSE", "CSRT" };

// create tracker by name
Ptr<Tracker> createTrackerByName(string trackerType)
{
    Ptr<Tracker> tracker;
    if (trackerType == trackerTypes[0])
        tracker = TrackerBoosting::create();
    else if (trackerType == trackerTypes[1])
        tracker = TrackerMIL::create();
    else if (trackerType == trackerTypes[2])
        tracker = TrackerKCF::create();
    else if (trackerType == trackerTypes[3])
        tracker = TrackerTLD::create();
    else if (trackerType == trackerTypes[4])
        tracker = TrackerMedianFlow::create();
    else if (trackerType == trackerTypes[5])
        tracker = TrackerGOTURN::create();
    else if (trackerType == trackerTypes[6])
        tracker = TrackerMOSSE::create();
    else if (trackerType == trackerTypes[7])
        tracker = TrackerCSRT::create();
    else {
        cout << "Incorrect tracker name" << endl;
        cout << "Available trackers are: " << endl;
        for (vector<string>::iterator it = trackerTypes.begin(); it != trackerTypes.end(); ++it)
            std::cout << " " << *it << endl;
    }
    return tracker;
}

// Fill the vector with random colors
void getRandomColors(vector<Scalar> &colors, int numColors)
{
    RNG rng(0);
    for (int i = 0; i < numColors; i++)
        colors.push_back(Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)));
}

int main(int argc, char * argv[])
{
    cout << "Available tracking algorithms are:" << endl;
    for (vector<string>::iterator it = trackerTypes.begin(); it != trackerTypes.end(); ++it)
        std::cout << " " << *it << endl;

    string trackerType = "KCF";
    cout << "The Selected tracker is " << trackerType << endl;

    string videoPath = "SS-100_zoom_Trim.mp4";

    // Initialize MultiTracker with tracking algo
    vector<Rect> bboxes;
    Mat frame;
    cv::VideoCapture cap(videoPath);
    //cap.set(CV_CAP_PROP_FRAME_WIDTH, 1280);
    //cap.set(CV_CAP_PROP_FRAME_HEIGHT, 720);

    if (!cap.isOpened())
    {
        cout << "Error opening video file " << videoPath << endl;
        return -1;
    }
    cap >> frame;

    bool showCrosshair = true;
    bool fromCenter = false;

    cv::selectROIs("MultiTracker", frame, bboxes, showCrosshair, fromCenter);

    if (bboxes.size() < 1)
        return 0;

    vector<Scalar> colors;
    getRandomColors(colors, bboxes.size());

    // Create multitracker
    Ptr<MultiTracker> multiTracker = cv::MultiTracker::create();

    // initialize multitracker
    for (int i = 0; i < bboxes.size(); i++)
        multiTracker->add(createTrackerByName(trackerType), frame, Rect2d(bboxes[i]));

    cout << "Started tracking, press ESC to quit." << endl;

    while (cap.isOpened())
    {
        cap >> frame;
        if (frame.empty()) break;

        //update the tracking result with new frame
        multiTracker->update(frame);

        putText(frame, trackerType + " Tracker", Point(100, 20), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(50, 170, 50), 2);

        // draw tracked objects
        for (unsigned i = 0; i < multiTracker->getObjects().size(); i++)
        {                                                                               
            rectangle(frame, multiTracker->getObjects()[i], colors[i], 2, 1);
        }

        cout << "\nPosition of box1 in X-axis :" << bboxes[0].x << endl;
        cout << "\nPosition of box1 in Y-axis :" << bboxes[0].y << endl;   
        cout << "\nPosition of box2 in X-axis :" << bboxes[1].x << endl;
        cout << "\nPosition of box2 in Y-axis :" << bboxes[1].y << endl;

        resize(frame, frame, Size(1280, 720), 0, 0, INTER_CUBIC);
        imshow("MultiTracker", frame);
        if (waitKey(1) == 27) break; 

    }



}

bboxes.size()为2,因为我仅绘制2个ROI。我正在使用OpenCV 3.4.1和Visual Studio 2015

这是我得到的Output

输出示例

2 个答案:

答案 0 :(得分:0)

似乎您的bboxes[]变量用于初始化,但之后没有更新。您可以像在绘制矩形时一样,尝试将multiTracker->getObjects()的输出用于x和y坐标。

答案 1 :(得分:0)

如@Nicolas Gaborel所述,您没有更新bboxes的值。有两种方法可以做到这一点

  1. 如果顺序无关紧要,则只需在您的 for循环

  2. 中执行bboxes[i] = multiTracker->getObjects()[i]
  3. 如果您还想跟踪订单,则需要为检测到的盒子分配某种ID。一种快速的方法是首先计算矩形的质心并存储它们。一旦检测到对象,就可以计算对象矩形的质心。此后,计算检测到的对象质心和bboxes中存储的质心的欧几里得距离。在bboxes中具有最小距离的矩形是正确的矩形。