3

I am using TrackerCSRT for object tracking in a video, and currently I use the init method on the tracker to set the region of interest

tracker = cv2.TrackerCSRT_create()
tracker.init(frame, region_of_interest)

The region of interst include my object, but it also include irrelevant pixels from the background or other objects.

I would like to use a mask to refine which pixels I'm interested in. Looking over the documentation, I can see method setInitialMask on the C++ version, but I cannot find the equivalent method in the Python wrapper.

How do I set a mask in TrackerCSRT on openCV for Python?

RMalke
  • 4,048
  • 29
  • 42

4 Answers4

12

i made a Pull Request to enable use of cv::TrackerCSRT::setInitialMask() for Python and Java.

additionally may the following python code will be useful to play with params.

tracker = cv2.TrackerCSRT_create()
tracker.save("default_csrt.xml") // saves default values of the Tracker
                                    you can rename default_csrt.xml-> custom_csrt.xml 
                                    and change values in it and use it load params

fs = cv2.FileStorage("custom_csrt.xml",cv2.FILE_STORAGE_READ)
fn = fs.getFirstTopLevelNode()
tracker.read(fn)
sturkmen
  • 3,495
  • 4
  • 27
  • 63
2

At the moment, you can't. But you could do it if you are prepared to rebuild OpenCV.

The creation of Python bindings for OpenCV C++ modules is controlled by markup in the C++ files, as documented here. At the time of writing, the C++ declaration of setInitialMask() does not have the markup which causes a Python binding to be created for it, so it is inaccessible. I expect that is because the TrackerCSRT implementation is only a few months old, and that Python bindings will follow as it matures.

However, if you are prepared to rebuild OpenCV on your system (which can be a little challenging if you haven't done it before), making setInitialMask() accessible from Python should be as simple as adding the CV_WRAP macro to that declaration (as explained in the above documentation) and rebuilding.

There are quite a few guides online for how to build OpenCV, but since I haven't done this myself in a while and the methods are somewhat platform dependent, I won't recommend one.

Hope this helps.

Chungzuwalla
  • 1,038
  • 6
  • 17
1

I do not know of setInitialMask but in python you can select your region of interest using cv2.selectROI() method.

A blog post on tracking objects in a video can be found here along with relevant code in a step-by-step approach.

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
0

Unfortunately, you can't.

Just complementing @chungzuwalla answer, the tracker c++ code only uses mask if you set params.use_segmentation, but the Tracker python interface does not let you set params.

Fred Guth
  • 1,537
  • 1
  • 15
  • 27