EnoxSoftware / OpenCVForUnity

OpenCV for Unity (Untiy Asset Plugin)
https://assetstore.unity.com/packages/tools/integration/opencv-for-unity-21088
557 stars 175 forks source link

legacy_TrackerMOSSE doubt and implementation example request. #152

Open andresilmor opened 2 years ago

andresilmor commented 2 years ago

Hi, does anyone have the example "TrackingExample" (or something alike) working with the now legacy_TrackerMOSSE, Im having problems in the part of the update.

About my doubt, why is MOSSE now legacy, isnt it one of the bests modules, at least in terms of speed? (im new with this area, and asset xd)

Thanks in advance for the attention.

EnoxSoftware commented 2 years ago

Thanks for the useful input. We thought we needed an example of TrackingModule.legacy_Tracker too, so we created a new one for it. Please give it a try. LegacyTrackingExample.zip

andresilmor commented 2 years ago

Thanks for the useful input. We thought we needed an example of TrackingModule.legacy_Tracker too, so we created a new one for it. Please give it a try. LegacyTrackingExample.zip

Hi again, thanks for provide an example, yes it works as expected and i seem to have no problem to adapt it to my needs for now, it was simpler than I expected, thanks again.

andresilmor commented 2 years ago

Hi, seems like I was wrong and still have the same problem when updating, like, in the example provided it works well but after i incorporate in my app the boundingBox after the tracker.update() returns only 0's, note that it works fine with the CSRT and the legacy_CSRT, also, just for note, now i just have a debug, not really a visual boundingbox or alike.

This is my code I hope it can help to help me xd:

Point top = new Point(faceRect.x1, faceRect.y1);
Point bottom = new Point(faceRect.x2, faceRect.y2);
RectCV region = new RectCV(top, bottom);

RectCV is the Rect from the OpenCV faceRect is a Class with the bounding box values and replaces the points from the click mouse

Now this is the code to create the tracker CSRT

TrackerCSRT trackerCSRT = TrackerCSRT.create(new TrackerCSRT_Params());
trackerCSRT.init(frame, region);

That i changed to this

legacy_TrackerMOSSE trackerMOSSE = legacy_TrackerMOSSE.create();
Rect2d _region = new Rect2d(region.tl(), region.size());
trackerMOSSE.init(frame, _region);

frame is the frame retrieved with the Windows API and is already converted to Mat as CvType.CV_8UC1, but seems to work with CSRT fine xd

This is my TrackingSettings, it his placed in another Class, it was the same with CSRT

public struct TrackerSetting
    {
        public legacy_Tracker tracker;
        public Scalar lineColor;
        public Rect2d boundingBox;

        public TrackerSetting(legacy_Tracker tracker, Scalar lineColor = null)
        {
            this.tracker = tracker;
            this.lineColor = lineColor == null ? new Scalar(0, 255, 0) : lineColor;
            this.boundingBox = new Rect2d();
        }

        public void Dispose()
        {
            if (tracker != null)
            {
                tracker.Dispose();
                tracker = null;
            }
        }
    }

And this is the update CSRT and MOSSE:

/* Tracker CSRT
        for (int i = 0; i < trackers.Count; i++)
        {
            debugger.AddText("1" );
            Tracker tracker = trackers[i].trackerSetting.tracker;
            RectCV boundingBox = trackers[i].trackerSetting.boundingBox;
            debugger.AddText("2");

            tracker.update(frameMat, boundingBox);

            debugger.AddText(boundingBox.ToString());

        }
        */

        // Tracker MOSSE
        for (int i = 0; i < trackers.Count; i++)
        {
            debugger.AddText("1");
            legacy_Tracker tracker = trackers[i].trackerSetting.tracker;
            Rect2d boundingBox = trackers[i].trackerSetting.boundingBox;
            debugger.AddText("2");

            tracker.update(frameMat, boundingBox);

            debugger.AddText(boundingBox.ToString());

        }

The CSRT and legacy_CSRT in the debug shows something like {589, 133, 54x108}, while the legacy_MOSSE shows {0, 0, 0x0}. Also in the creation of the tracker legacy_MOSSE, the Rect that we pass as _region also have values , not 0's and when we get the tracker and rect2d in the update from the trackerSetting, they do not return as null. I hope that a didn't forget any important code.