Ask Your Question

kurene13's profile - activity

2016-12-07 01:58:46 -0600 received badge  Student (source)
2016-12-07 01:28:22 -0600 asked a question Segmentation fault when initializing tracker. Possible bug?

Hello,

I'm reporting the issue for the first time, so below is all information I think you need to check for a bug. There is a possible bug in tracking API. Below is a detailed description.

In my project I'm using opencv2/tracking.hpp header and Ptr<cv::Tracker> tracker object. Tracker is created with line: tracker = cv::Tracker::create("KCF");. It's initialized with following code:

// If tracker is not empty we want it to reinitialize.
if(tracker->getModel()){
    tracker->clear();
    tracker = cv::Tracker::create("KCF");
}
// Initialize tracker
if(!tracker->init(frame, roi)){
    cerr << "Could not initialize tracker" << endl;
    exit(EXIT_FAILURE);
}

When initializing I'm using a cv::Mat frame from a video and cv::Rect2d roi as bounding box that is read from a file. These two parameters are downscaled before usage. They are downscaled by factor 25%. Frame is scaled with cv::resize(), and bounding box is scaled with following code:

outputRoi = inputRoi;

double scaledWidth = (inputRoi.width*(scaleFactor));
double scaledHeight = (inputRoi.height*(scaleFactor));

// using ratio
outputRoi.x = (scaledWidth/inputRoi.width*inputRoi.x);
outputRoi.y = (scaledHeight/inputRoi.height*inputRoi.y);
outputRoi.width = scaledWidth;
outputRoi.height = scaledHeight;

When I don't scale, code is working fine. If I scale and for bounding box use cvRound for x, y, width and height it is working. If I scale and don't use rounding I get segmentation fault for specific bounding box on specific frame. For other frames it is working fine. The fact is that init method is defined as bool init( const Mat& image, const Rect2d& boundingBox ) and should be working with "unrounded" double values.

Here is main.cpp code for testing. Uncomment or comment scaleRoiNotWorking and scaleRoiWorking and different roi values to see the difference.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/* 
 * File:   main.cpp
 * Author: gregork
 *
 * Created on December 7, 2016, 7:39 AM
 */

#include <cstdlib>
#include <opencv2/tracking.hpp>

using namespace std;
using namespace cv;


static void scaleFrame(const InputArray& inputImage, OutputArray& outputImage, double scaleFactor){
    cv::resize(inputImage, outputImage, Size(), scaleFactor, scaleFactor);
}

static void scaleRoiNotWorking(const Rect2d& inputRoi, Rect2d& outputRoi, double scaleFactor){
    outputRoi = inputRoi;
    double scaledWidth = (inputRoi.width*(scaleFactor));
    double scaledHeight = (inputRoi.height*(scaleFactor));

    // Using ratio / proportion
    outputRoi.x = (scaledWidth/inputRoi.width*inputRoi.x);
    outputRoi.y = (scaledHeight/inputRoi.height*inputRoi.y);
    outputRoi.width = scaledWidth;
    outputRoi.height = scaledHeight;
}

static void scaleRoiWorking(const Rect2d& inputRoi, Rect2d& outputRoi, double scaleFactor){
    outputRoi = inputRoi;

    double scaledWidth = cvRound(inputRoi.width*(scaleFactor));
    double scaledHeight = cvRound(inputRoi.height*(scaleFactor));

    // Using ratio / proportion
    outputRoi.x = cvRound(scaledWidth/inputRoi.width*inputRoi.x);
    outputRoi.y = cvRound(scaledHeight/inputRoi.height*inputRoi.y);
    outputRoi.width = scaledWidth;
    outputRoi.height = scaledHeight;
}

int main(int argc, char** argv) {
    Mat frame = Mat::ones(Size(1920,1080), CV_8UC3);
    /*frame = imread(argv[1], CV_LOAD_IMAGE_COLOR);
    if(! frame.data )                              // Check for invalid input
    {
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }*/
    scaleFrame(frame, frame, 0.25);

//
    Rect2d origRoi(817, 251, 340, 613 ...
(more)