Esempio n. 1
0
        //public List<BoundingBox> Update(byte[] imageData, int width, int height, List<BoundingBox> boxes)
        //{

        //    if(boxes.Count > 0 && m_trackedObjects.Count == 0)
        //    {
        //        BoundingBox box = boxes[0];
        //        CVTracker tracker = new CVTracker();
        //        bool success = tracker.Init("CSRT");
        //        if (success)
        //        {
        //            int x1 = (int)(box.x1 * (float)width);
        //            int y1 = (int)(box.y1 * (float)height);
        //            int x2 = (int)(box.x2 * (float)width);
        //            int y2 = (int)(box.y2 * (float)height);
        //            int w = x2 - x1 + 1;
        //            int h = y2 - y1 + 1;
        //            tracker.StartTracking(imageData, width, height, x1, y1, w, h);
        //            TrackedObject obj = new TrackedObject(tracker, box);
        //            m_objectCount++;
        //            m_trackedObjects.Add(m_objectCount, obj);
        //        }
        //    }

        //    List<int> removeList = new List<int>();

        //    // pass new frame to all existing trackers
        //    foreach (KeyValuePair<int, TrackedObject> obj in m_trackedObjects)
        //    {
        //        int id = obj.Key;
        //        TrackedObject to = obj.Value;
        //        BoundingBox box = to.box;
        //        int x1 = (int)(box.x1 * (float)width);
        //        int y1 = (int)(box.y1 * (float)height);
        //        int x2 = (int)(box.x2 * (float)width);
        //        int y2 = (int)(box.y2 * (float)height);
        //        int w = x2 - x1 + 1;
        //        int h = y2 - y1 + 1;

        //        bool success = to.tracker.Update(imageData, width, height, ref x1, ref y1, ref w, ref h);

        //        if (success)
        //        {
        //            box.x1 = (float)x1 / (float)width;
        //            box.y1 = (float)y1 / (float)height;
        //            box.x2 = (float)(x1+w-1) / (float)width;
        //            box.y2 = (float)(y1+h-1) / (float)height;

        //            to.box = box;
        //        }
        //        else
        //        {
        //            removeList.Add(id);
        //        }
        //    }

        //    // remove failed tracks
        //    foreach (int id in removeList) m_trackedObjects.Remove(id);

        //    // return list of BoundingBoxes
        //    List<BoundingBox> trackedBoxes = new List<BoundingBox>();
        //    foreach (KeyValuePair<int, TrackedObject> obj in m_trackedObjects)
        //    {
        //        trackedBoxes.Add(obj.Value.box);
        //    }

        //    return trackedBoxes;

        //}



        public List <BoundingBox> Update(byte[] imageData, int width, int height, List <BoundingBox> boxes)
        {
            // Find new boxes
            Dictionary <int, int> matches;
            List <int>            noMatches;
            List <BoundingBox>    newBoxes;

            FindNewBoxes(boxes, 0.3f, out matches, out noMatches, out newBoxes);

            // start a new tracker for each new box
            foreach (BoundingBox box in newBoxes)
            {
                //CorrelationTracker tracker = new CorrelationTracker();
                CVTracker tracker = new CVTracker();
                bool      success = tracker.Init(TrackerType.KCF);
                if (success)
                {
                    int x1 = (int)(box.x1 * (float)width);
                    int y1 = (int)(box.y1 * (float)height);
                    int x2 = (int)(box.x2 * (float)width);
                    int y2 = (int)(box.y2 * (float)height);
                    int w  = x2 - x1 + 1;
                    int h  = y2 - y1 + 1;
                    tracker.StartTracking(imageData, width, height, x1, y1, w, h);
                    TrackedObject obj = new TrackedObject(tracker, box);
                    m_objectCount++;
                    m_trackedObjects.Add(m_objectCount, obj);
                }
            }

            // handle no matches AND box too small
            List <int> toDelete = new List <int>();

            foreach (int id in noMatches)
            {
                TrackedObject obj = m_trackedObjects[id];
                obj.numFramesWithoutMatch++;
                m_trackedObjects[id] = obj;

                float bw = obj.box.x2 - obj.box.x1;
                float bh = obj.box.y2 - obj.box.y1;

                if (obj.numFramesWithoutMatch > m_maxNumFramesWithoutMatch)
                {
                    toDelete.Add(id);
                }
            }

            // remove trackers that haven't had a match within m_maxNumFramesWithoutMatch
            foreach (int id in toDelete)
            {
                m_trackedObjects.Remove(id);
            }


            // clear no match count for those with a match
            foreach (KeyValuePair <int, int> obj in matches)
            {
                int           id         = obj.Key;
                TrackedObject trackedObj = m_trackedObjects[id];
                trackedObj.numFramesWithoutMatch = 0;
                m_trackedObjects[id]             = trackedObj;
            }


            // pass new frame to all existing trackers
            List <int> removeList = new List <int>();

            foreach (KeyValuePair <int, TrackedObject> obj in m_trackedObjects)
            {
                int           id  = obj.Key;
                TrackedObject to  = obj.Value;
                BoundingBox   box = to.box;
                int           x1  = (int)(box.x1 * (float)width);
                int           y1  = (int)(box.y1 * (float)height);
                int           x2  = (int)(box.x2 * (float)width);
                int           y2  = (int)(box.y2 * (float)height);
                int           w   = x2 - x1 + 1;
                int           h   = y2 - y1 + 1;

                bool success = to.tracker.Update(imageData, width, height, ref x1, ref y1, ref w, ref h);

                if (success)
                {
                    box.x1 = (float)x1 / (float)width;
                    box.y1 = (float)y1 / (float)height;
                    box.x2 = (float)(x1 + w - 1) / (float)width;
                    box.y2 = (float)(y1 + h - 1) / (float)height;

                    to.box = box;
                }
                else
                {
                    removeList.Add(id);
                }
            }

            // remove failed tracks
            foreach (int id in removeList)
            {
                m_trackedObjects.Remove(id);
            }

            // return list of BoundingBoxes
            List <BoundingBox> trackedBoxes = new List <BoundingBox>();

            foreach (KeyValuePair <int, TrackedObject> obj in m_trackedObjects)
            {
                trackedBoxes.Add(obj.Value.box);
            }

            return(trackedBoxes);
        }