3

I'm trying to create MEDIANFLOW tracker with OpenCV3.3 using opencv-python with Python3.6. I need to pass some arguments into the constructor according to this page of OpenCV docs.

The problem is I don't know how to pass available arguments into this function correctly? I wasn't able to find any information about it.

What I do (and it works):

tracker = cv2.TrackerMedianFlow_create()

What I want to do:

tracker = cv2.TrackerMedianFlow_create(maxLevel=3)

But it doesn't work and gives me an error:

SystemError: <built-in function TrackerMedianFlow_create> returned NULL without setting an error

Could you help?

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
  • 1
    Note that the documentation you linked is the constructor *for the params* structure. I don't have 3.3 to test how to do it, but you need to instantiate a `TrackeMedianFlowr::Params()` object, and then pass *that* object into `TrackerMedianFlow_create()`. Check the [docs for `TrackerMedianFlow()`](https://docs.opencv.org/3.3.0/d7/d86/classcv_1_1TrackerMedianFlow.html) to see that the constructor accepts only a `TrackerMedianFlow::Params()` object. – alkasm Dec 09 '17 at 00:07
  • 1
    see https://stackoverflow.com/a/52026745/5294258 – sturkmen Sep 21 '18 at 21:01

2 Answers2

4

I search the intermediate code generated by cmake/make when compiling the OpenCV 3.3 for Python 3.5, just cannot find the methods to set the parameters for cv2.TrackerXXX.

In modules/python3/pyopencv_generated_funcs.h, I find this function:

static PyObject* pyopencv_cv_TrackerMedianFlow_create(PyObject* , PyObject* args, PyObject* kw)
{
    using namespace cv;

    Ptr<TrackerMedianFlow> retval;

    if(PyObject_Size(args) == 0 && (kw == NULL || PyObject_Size(kw) == 0))
    {
        ERRWRAP2(retval = cv::TrackerMedianFlow::create());
        return pyopencv_from(retval);
    }

    return NULL;
}

This is to say, you cann't pass any parameter to cv::TrackerMedianFlow_create().

In modules/python3/pyopencv_generated_types.h, I find this one:

static PyGetSetDef pyopencv_TrackerMedianFlow_getseters[] =
{
    {NULL}  /* Sentinel */
};

This to say, you have no way to change the parameters for Python wrapper by default, Unless you modified the source code and recompile it.

Kinght 金
  • 17,681
  • 4
  • 60
  • 74
4

You can customize Tracker parameters via FileStorage interface.

import cv2

# write
tracker = cv2.TrackerMedianFlow_create()
tracker.save('params.json')

# write (another way)
fs = cv2.FileStorage("params.json", cv2.FileStorage_WRITE)
tracker.write(fs)
fs.release()

# read
tracker2 = cv2.TrackerMedianFlow_create()
fs = cv2.FileStorage("params.json", cv2.FileStorage_READ)
tracker2.read(fs.getFirstTopLevelNode())
Junya
  • 83
  • 7