Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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); // Not working
    //Rect2d origRoi(95, 380, 345, 424); // Working 
    Rect2d roi;
    scaleRoiNotWorking(origRoi, roi, 0.25);
    //scaleRoiWorking(origRoi, roi, 0.25);

    Ptr<Tracker> tracker = Tracker::create("KCF");
    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);
    }

    return 0;
}

Environment
Linux 64-bit, gcc version 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC), opencv 3.1.0

Build info
General configuration for OpenCV 3.1.0 ===================================== Version control: unknown

Platform: Host: Linux 3.10.0-327.22.2.el7.x86_64 x86_64 CMake: 3.2.2 CMake generator: Unix Makefiles CMake build tool: /usr/bin/gmake Configuration: RELEASE

C/C++: Built as dynamic libs?: YES C++ Compiler: /usr/lib64/ccache/c++ (ver 4.8.5) C++ flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -ffunction-sections -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG C++ flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -ffunction-sections -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG C Compiler: /usr/lib64/ccache/cc C flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wno-narrowing -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -ffunction-sections -fvisibility=hidden -O3 -DNDEBUG -DNDEBUG C flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wno-narrowing -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -ffunction-sections -fvisibility=hidden -g -O0 -DDEBUG -D_DEBUG Linker flags (Release): Linker flags (Debug): Precompiled headers: NO Extra dependencies: Qt5::Test Qt5::Concurrent Qt5::OpenGL /lib64/libwebp.so /lib64/libpng.so /lib64/libtiff.so /lib64/libjasper.so /lib64/libjpeg.so /lib64/libImath.so /lib64/libIlmImf.so /lib64/libIex.so /lib64/libHalf.so /lib64/libIlmThread.so gstvideo-1.0 gstapp-1.0 gstbase-1.0 gstriff-1.0 gstpbutils-1.0 gstreamer-1.0 gobject-2.0 glib-2.0 dc1394 v4l1 v4l2 avcodec avformat avutil swscale avresample gphoto2 gphoto2_port exif /lib64/libbz2.so Qt5::Core Qt5::Gui Qt5::Widgets /lib64/libhdf5.so /lib64/libz.so /lib64/libdl.so /lib64/libm.so correspondence multiview numeric glog gflags dl m pthread rt /lib64/libGLU.so /lib64/libGL.so tbb 3rdparty dependencies:

OpenCV modules: To be built: core flann hdf imgproc ml photo reg surface_matching video dnn fuzzy imgcodecs shape videoio highgui objdetect plot superres ts xobjdetect xphoto bgsegm bioinspired dpm face features2d line_descriptor saliency text calib3d ccalib cvv datasets rgbd stereo structured_light tracking videostab xfeatures2d ximgproc aruco optflow sfm stitching matlab python2 Disabled: java world contrib_world Disabled by dependency: - Unavailable: cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev python3 viz

GUI: QT 5.x: YES (ver 5.6.1) QT OpenGL support: YES (Qt5::OpenGL 5.6.1) OpenGL support: YES (/lib64/libGLU.so /lib64/libGL.so) VTK support: NO

Media I/O: ZLib: /lib64/libz.so (ver 1.2.7) JPEG: /lib64/libjpeg.so (ver ) WEBP: /lib64/libwebp.so (ver encoder: 0x0201) PNG: /lib64/libpng.so (ver 1.5.13) TIFF: /lib64/libtiff.so (ver 42 - 4.0.3) JPEG 2000: /lib64/libjasper.so (ver 1.900.1) OpenEXR: /lib64/libImath.so /lib64/libIlmImf.so /lib64/libIex.so /lib64/libHalf.so /lib64/libIlmThread.so (ver 1.7.1) GDAL: NO

Video I/O: DC1394 1.x: NO DC1394 2.x: YES (ver 2.2.2) FFMPEG: YES codec: YES (ver 56.26.100) format: YES (ver 56.25.101) util: YES (ver 54.20.100) swscale: YES (ver 3.1.101) resample: YES (ver 2.1.0) gentoo-style: YES GStreamer:
base: YES (ver 1.4.5) video: YES (ver 1.4.5) app: YES (ver 1.4.5) riff: YES (ver 1.4.5) pbutils: YES (ver 1.4.5) OpenNI: NO OpenNI PrimeSensor Modules: NO OpenNI2: NO PvAPI: NO GigEVisionSDK: NO UniCap: NO UniCap ucil: NO V4L/V4L2: Using libv4l1 (ver 0.9.5) / libv4l2 (ver 0.9.5) XIMEA: NO Xine: NO gPhoto2: YES

Parallel framework: TBB (ver 4.1 interface 6103)

Other third-party libraries: Use IPP: 9.0.1 [9.0.1] at: /home/gregork/libs/opencv-3.1.0/src/opencv/3rdparty/ippicv/unpack/ippicv_lnx Use IPP Async: NO Use VA: NO Use Intel VA-API/OpenCL: NO Use Eigen: YES (ver 3.2.5) Use Cuda: NO Use OpenCL: YES Use custom HAL: NO

OpenCL: Version: dynamic Include path: /home/gregork/libs/opencv-3.1.0/src/opencv/3rdparty/include/opencl/1.2 Use AMDFFT: NO Use AMDBLAS: NO

Python 2: Interpreter: /usr/bin/python2.7 (ver 2.7.5) Libraries: /lib64/libpython2.7.so (ver 2.7.5) numpy: /usr/lib64/python2.7/site-packages/numpy/core/include (ver 1.7.1) packages path: lib/python2.7/site-packages

Python 3: Interpreter: /usr/bin/python3.4 (ver 3.4.3)

Python (for build): /usr/bin/python2.7

Java: ant: /bin/ant (ver 1.9.2) JNI: /usr/lib/jvm/java/include /usr/lib/jvm/java/include/linux /usr/lib/jvm/java/include Java wrappers: NO Java tests: NO

Matlab: mex: /usr/local/MATLAB/R2015b/bin/mex Compiler/generator: Working

Documentation: Doxygen: /usr/bin/doxygen (ver 1.8.5) PlantUML: NO

Tests and samples: Tests: YES Performance tests: YES C/C++ Examples: NO

Install path: /home/gregork/libs/opencv-3.1.0/bin/release

cvconfig.h is in: /home/gregork/libs/opencv-3.1.0/src/build

Error with Backtrace
* Error in `/home/gregork/workspace/Program/dist/Debug/GNU-Linux/program': free(): invalid next size (fast): 0x00000000035e26e0 * ======= Backtrace: ========= /lib64/libc.so.6(+0x7d053)[0x7fdc51c15053] /lib64/libc.so.6(+0x7ede0)[0x7fdc51c16de0] /lib64/libc.so.6(__libc_memalign+0x75)[0x7fdc51c18b25] /lib64/libc.so.6(posix_memalign+0x4c)[0x7fdc51c1a80c] /lib64/libavutil.so.54(av_malloc+0x60)[0x7fdc4bea5ad0] /lib64/libavutil.so.54(av_buffer_alloc+0x28)[0x7fdc4be9a008] /lib64/libavutil.so.54(av_buffer_allocz+0xd)[0x7fdc4be9a07d] /lib64/libavutil.so.54(av_buffer_pool_get+0x96)[0x7fdc4be9a496] /lib64/libavcodec.so.56(avcodec_default_get_buffer2+0x20b)[0x7fdc4c8dacfb] /lib64/libavcodec.so.56(+0x4467be)[0x7fdc4c8db7be] /lib64/libavcodec.so.56(+0x3ae5a6)[0x7fdc4c8435a6] /lib64/libavcodec.so.56(+0x353231)[0x7fdc4c7e8231] /lib64/libavcodec.so.56(+0x354b2d)[0x7fdc4c7e9b2d] /lib64/libavcodec.so.56(+0x324792)[0x7fdc4c7b9792] /lib64/libavcodec.so.56(+0x324d9b)[0x7fdc4c7b9d9b] /lib64/libavcodec.so.56(avcodec_decode_video2+0x296)[0x7fdc4c8dcf16] ../../libs/opencv-3.1.0/bin/release/lib/libopencv_videoio.so.3.1(+0x2a05f)[0x7fdc532e405f] ../../libs/opencv-3.1.0/bin/release/lib/libopencv_videoio.so.3.1(cvGrabFrame_FFMPEG+0x9)[0x7fdc532e40f9] ../../libs/opencv-3.1.0/bin/release/lib/libopencv_videoio.so.3.1(+0x28265)[0x7fdc532e2265] ../../libs/opencv-3.1.0/bin/release/lib/libopencv_videoio.so.3.1(cvGrabFrame+0x11)[0x7fdc532c68d1] ../../libs/opencv-3.1.0/bin/release/lib/libopencv_videoio.so.3.1(_ZN2cv12VideoCapture4grabEv+0x25)[0x7fdc532c6905] ../../libs/opencv-3.1.0/bin/release/lib/libopencv_videoio.so.3.1(_ZN2cv12VideoCapture4readERKNS_12_OutputArrayE+0x12)[0x7fdc532c63d2] /home/gregork/workspace/Program/dist/Debug/GNU-Linux/program[0x40907f] /lib64/libc.so.6(__libc_start_main+0xf5)[0x7fdc51bb9b15] /home/gregork/workspace/Program/dist/Debug/GNU-Linux/program[0x406439]