Example #1
0
 private IBoundingBox CurrentFrameBoundingBox(IBoundingBox bb)
 {
     return(bb.CreateInstance(
                AdaptPointForCurrentFrame(bb.Center),
                bb.Size
                ));
 }
Example #2
0
        public static Image <Gray, byte> GenerateSimilarPatch(Image <Gray, byte> frame, IBoundingBox bb, float maxRelShift, float maxRelScale, double maxAngle)
        {
            // calculate new center
            PointF newCenter;

            if (maxRelShift != 0)
            {
                float  relShift = (float)(_rand.NextDouble() * 2 - 1) * maxRelShift;
                PointF absShift = new PointF(relShift * bb.Size.Width, relShift * bb.Size.Height);
                newCenter = bb.Center.Add(absShift);
            }
            else
            {
                newCenter = bb.Center;
            }

            // calculate new size
            SizeF newSize;

            if (maxRelScale != 0)
            {
                float scale = 1.0f + (float)(_rand.NextDouble() * 2 - 1) * maxRelScale;
                newSize = bb.Size.Multiply(scale, scale);
            }
            else
            {
                newSize = bb.Size;
            }

            // calculate new bounding box
            IBoundingBox newBb = bb.CreateInstance(newCenter, newSize);

            // create patch
            Image <Gray, byte> patch = frame.GetPatch(newCenter, Size.Round(newSize));

            // rotate patch
            if (maxAngle != 0)
            {
                float angle = (float)((_rand.NextDouble() * 2 - 1) * maxAngle);
                float[,] m = new float[2, 3];
                int   w            = patch.Width;
                int   h            = patch.Height;
                float angleRadians = angle * ((float)Math.PI / 180.0f);
                m[0, 0] = (float)(Math.Cos(angleRadians));
                m[0, 1] = (float)(Math.Sin(angleRadians));
                m[1, 0] = -m[0, 1];
                m[1, 1] = m[0, 0];
                m[0, 2] = w * 0.5f;
                m[1, 2] = h * 0.5f;
                Matrix <float> M = new Matrix <float>(m);

                Image <Gray, byte> result = new Image <Gray, byte>(patch.Width, patch.Height);
                CvInvoke.cvGetQuadrangleSubPix(patch, result, M.Ptr);
                return(result);
            }

            return(patch);
        }
Example #3
0
        public IBoundingBox[] Generate(Size frameSize, IBoundingBox initialBb)
        {
            _frameSize          = frameSize;
            _initialBoundingBox = initialBb;
            _windowsGenerated   = true;

            List <IBoundingBox> _scanningWindowsList = new List <IBoundingBox>();

            // define minimum bounding box size
            float minSideSize = Math.Min(initialBb.Size.Width, initialBb.Size.Height);
            float ratio       = minSideSize / _minSize;
            Size  minBbSize   = new Size
                                (
                (int)Math.Ceiling(initialBb.Size.Width / ratio),
                (int)Math.Ceiling(initialBb.Size.Height / ratio)
                                );

            // create bounding boxes
            Size bbSize = minBbSize;

            while (bbSize.Width <= frameSize.Width && bbSize.Height <= frameSize.Height)
            {
                // define x and y step
                float xStep = bbSize.Width * _xRelStep;
                float yStep = bbSize.Height * _yRelStep;

                for (float bbCenterY = bbSize.Height / 2.0f - 0.5f; bbCenterY + bbSize.Height / 2.0f <= frameSize.Height - 0.5f; bbCenterY += yStep)
                {
                    for (float bbCenterX = bbSize.Width / 2.0f - 0.5f; bbCenterX + bbSize.Width / 2.0f <= frameSize.Width - 0.5f; bbCenterX += xStep)
                    {
                        IBoundingBox bb = initialBb.CreateInstance(
                            new PointF(bbCenterX, bbCenterY),
                            bbSize
                            );
                        bb.ScanningWindow = bb.CreateScanningWindow();

                        _scanningWindowsList.Add(bb);
                    }
                }

                bbSize = new Size((int)(bbSize.Width * _scaleStep), (int)(bbSize.Height * _scaleStep));
            }

            _scanningWindows = _scanningWindowsList.ToArray();
            return(_scanningWindows);
        }
Example #4
0
        public static List <IBoundingBox> GenerateSimilarBoundingBoxes(Size frameSize, IBoundingBox refBb, SimilarBoundingBoxGenerationInfo info)
        {
            List <IBoundingBox> boundingBoxes = new List <IBoundingBox>();

            for (int i = 0; i < info.Count; i++)
            {
                // calculate new center
                PointF newCenter;
                if (info.MaxShift != 0)
                {
                    float  relShift = (float)(_rand.NextDouble() * 2 - 1) * info.MaxShift;
                    PointF absShift = new PointF(relShift * refBb.Size.Width, relShift * refBb.Size.Height);
                    newCenter = refBb.Center.Add(absShift);
                }
                else
                {
                    newCenter = refBb.Center;
                }

                // calculate new size
                SizeF newSize;
                if (info.MaxScale != 0)
                {
                    float scale = 1.0f + (float)(_rand.NextDouble() * 2 - 1) * info.MaxScale;
                    newSize = refBb.Size.Multiply(scale, scale);
                }
                else
                {
                    newSize = refBb.Size;
                }

                // create new bounding box
                IBoundingBox newBb = refBb.CreateInstance(newCenter, newSize);

                // if bounding box is inside the frame, add it to collection
                if (newBb.InsideFrame(frameSize))
                {
                    boundingBoxes.Add(newBb);
                }
            }

            return(boundingBoxes);
        }
Example #5
0
        public IBoundingBox DetermineBoundingBox(IBoundingBox trackerBoundingBox, List <IBoundingBox> detectorBoundingBoxes, Image <Gray, byte> frame, out bool reinitializeTracker, bool prevValid, out bool currValid)
        {
            IBoundingBox output = null;

            reinitializeTracker = false;
            currValid           = false;

            /* [Zdenek] If neither the tracker nor the detector output a bounding box,
             * the object is declared as not visible
             */
            if (trackerBoundingBox == null && detectorBoundingBoxes == null)
            {
                return(null);
            }

            // calculate relative similarities for all bounding boxes
            Dictionary <IBoundingBox, float> relativeSimilarities = new Dictionary <IBoundingBox, float>();

            // -> tracker
            if (trackerBoundingBox != null)
            {
                frame.ROI = Rectangle.Round(trackerBoundingBox.GetRectangle());
                Size patchSize = _objectModel.PatchSize;
                Image <Gray, byte> trackerPatch = frame.Resize(patchSize.Width, patchSize.Height, INTER.CV_INTER_LINEAR);
                relativeSimilarities.Add(trackerBoundingBox, _objectModel.RelativeSimilarity(trackerPatch));
                frame.ROI = Rectangle.Empty;
            }

            // -> detector
            if (detectorBoundingBoxes != null)
            {
                for (int i = 0; i < detectorBoundingBoxes.Count; i++)
                {
                    IBoundingBox bb = detectorBoundingBoxes[i];
                    relativeSimilarities.Add(bb, CurrentState.RelativeSimilarities[bb]);
                }
            }

            // if tracker is defined
            if (trackerBoundingBox != null)
            {
                output = trackerBoundingBox;

                if (detectorBoundingBoxes != null)
                {
                    float bigOverlap = 0.8f;

                    // get detector bounding boxes
                    // that are far from tracker
                    // and are more confident then the tracker
                    List <IBoundingBox> bbs = new List <IBoundingBox>();
                    foreach (IBoundingBox bb in detectorBoundingBoxes)
                    {
                        if (bb.GetOverlap(trackerBoundingBox) < bigOverlap &&
                            relativeSimilarities[bb] > relativeSimilarities[trackerBoundingBox])
                        {
                            bbs.Add(bb);
                        }
                    }

                    // if there is only only one such bounding box,
                    // reinitialize the tracker
                    if (bbs.Count == 1)
                    {
                        output = bbs[0];
                        reinitializeTracker = true;
                        currValid           = false;
                    }

                    // otherwise calculate weighted average with close detections
                    else
                    {
                        int    trackerRepeat = 1;
                        PointF center        = trackerBoundingBox.Center.Multiply(trackerRepeat, trackerRepeat);
                        SizeF  size          = trackerBoundingBox.Size.Multiply(trackerRepeat, trackerRepeat);

                        // consider detector bounding boxes that are close to the tracker
                        int bbCount = trackerRepeat;
                        foreach (IBoundingBox bb in detectorBoundingBoxes)
                        {
                            if (bb.GetOverlap(trackerBoundingBox) >= bigOverlap)
                            {
                                center = center.Add(bb.Center);
                                size   = size.Add(bb.Size);
                                bbCount++;
                            }
                        }

                        output = trackerBoundingBox.CreateInstance(
                            center.Divide(bbCount, bbCount),
                            size.Divide(bbCount, bbCount)
                            );
                    }
                }
            }

            // if tracker is not defined
            else
            {
                if (detectorBoundingBoxes != null)
                {
                    List <IBoundingBox> suppressedBbs = Service.NonMaximalBoundingBoxSuppress(detectorBoundingBoxes);

                    // if there is a single detection, reinitialize the tracker
                    if (suppressedBbs.Count == 1)
                    {
                        output = suppressedBbs[0];
                        reinitializeTracker = true;
                        currValid           = false;
                    }
                }
            }

            if (output != null)
            {
                if (prevValid == true)
                {
                    currValid = true;
                }
                else
                {
                    Image <Gray, byte> outputPatch = frame.GetPatch(output.Center, Size.Round(output.Size))
                                                     .Resize(_objectModel.PatchSize.Width, _objectModel.PatchSize.Height, INTER.CV_INTER_LINEAR);

                    float pnn, nnn;
                    float conservativeSimilarity = _objectModel.ConservativeSimilarity(outputPatch, out pnn, out nnn);
                    if (pnn > 0.8f && conservativeSimilarity >= ConsSimValidThreshold)
                    {
                        currValid = true;
                    }
                }
            }

            return(output);
        }