Exemplo n.º 1
0
        public void Generate_SimpleCase()
        {
            // arrange - instantiate generator
            ScanningWindowGenerator generator = new ScanningWindowGenerator
                                                (
                2.0f,
                1.0f,
                1.0f,
                1
                                                );

            // arrange - define frame size and initial bounding box
            Size         frameSize = new Size(2, 2);
            IBoundingBox initialBb = new BoundingBox(new PointF(0, 0), new SizeF(1, 1));

            // define expected
            IBoundingBox[] expected = new IBoundingBox[]
            {
                initialBb.CreateInstance(new PointF(0, 0), new SizeF(1, 1)),
                initialBb.CreateInstance(new PointF(1, 0), new SizeF(1, 1)),
                initialBb.CreateInstance(new PointF(0, 1), new SizeF(1, 1)),
                initialBb.CreateInstance(new PointF(1, 1), new SizeF(1, 1)),
                initialBb.CreateInstance(new PointF(0.5f, 0.5f), new SizeF(2, 2)),
            };

            // get actual
            IBoundingBox[] actual = generator.Generate(frameSize, initialBb);

            // assert
            Assert.IsTrue(BoundingBoxListsAreEqual(expected.ToList(), actual.ToList()));
        }
Exemplo n.º 2
0
        public void SwgAndBaseClassifier()
        {
            // arrange - define frame
            Image <Gray, byte> frame = new Image <Gray, byte>(new Size(4, 3));

            // arrange - create scanning window generator
            ScanningWindowGenerator swg = new ScanningWindowGenerator(2.0f, 0.1f, 0.1f, 1);

            // arrange - generate scanning windows
            swg.Generate(frame.Size, new BoundingBox(new PointF(2.5f, 1.5f), new SizeF(2, 1)));

            // arrange - create pixel comparison scheduler with for 1 base classifier with 1 comparisons
            PixelComparisonSchedulerStub pcs = new PixelComparisonSchedulerStub();
            PixelComparisonGroupF        pcg = new PixelComparisonGroupF();

            pcg.Value = new PixelComparisonF[]
            {
                new PixelComparisonF(new PointF(0, 0), new PointF(1, 0)),
            };
            pcs.ScheduledPixelComparisons = new PixelComparisonGroupF[]
            {
                pcg
            };

            // arrange - create base classifier
            BaseClassifier classifier = new BaseClassifier(swg, 0, pcs);

            classifier.PostInstantiation();
            classifier.Initialize();
            classifier.SetCurrentFrame(frame);

            // calculate binary code for every window - this part must not throw exception
            for (int i = 0; i < swg.ScanningWindows.Length; i++)
            {
                classifier.CalculatePosteriorForWindow(i);
            }
        }
Exemplo n.º 3
0
        private static IDetector LoadDetector(IObjectModel objectModel)
        {
            // scanning window generator
            IScanningWindowGenerator scanningWindowGenerator = new ScanningWindowGenerator
                                                               (
                1.2f,
                0.1f,
                0.1f,
                100
                                                               );

            // cascaded classifier

            //      variance classifier
            IClassifier varianceClassifier = new VarianceClassifier(scanningWindowGenerator, 0.5);

            //      ensemble classifier
            int width  = objectModel.PatchSize.Width;
            int height = objectModel.PatchSize.Height;
            IPixelComparisonScheduler pcs  = new PixelComparisonScheduler(12, 3, width, height);
            IClassifier ensembleClassifier = new EnsembleClassifier(scanningWindowGenerator, pcs, 2);

            //      nn classifier
            NnClassifier nnClassifier = new NnClassifier(scanningWindowGenerator, objectModel, 0.75f, 0.55f);

            // instantiate cascaded classifier
            IClassifier cascadedClassifier = new CascadedClassifier(varianceClassifier, ensembleClassifier, nnClassifier);

            IDetector detector = new Detector
                                 (
                scanningWindowGenerator,
                cascadedClassifier,
                Service.NonMaximalBoundingBoxSuppress
                                 );

            return(detector);
        }
Exemplo n.º 4
0
        public void GetPosteriors_InitializationAndOneMethodCall()
        {
            // arrange - define initialization frame and bb
            Image <Gray, byte> initFrame = new Image <Gray, byte>(Path.Combine(_resourceDir, "LP_up_left.jpg"));
            IBoundingBox       initBb    = new BoundingBox(new PointF(170, 150), new SizeF(269, 189));

            // arrange - generate positive and negative patches
            // -> positive patches
            List <Image <Gray, byte> > positivePatches = new List <Image <Gray, byte> >();
            int positiveCount = 100;

            for (int i = 0; i < positiveCount; i++)
            {
                Image <Gray, byte> patch = Service.GenerateSimilarPatch(initFrame, initBb, 0, 0, 0);
                CvInvoke.cvSmooth(patch, patch, SMOOTH_TYPE.CV_GAUSSIAN, 0, 0, 1.5, 1.5);
                positivePatches.Add(patch);
            }

            // -> negative patches
            ScanningWindowGenerator swg = new ScanningWindowGenerator(1.2f, 0.1f, 0.1f, 20);

            IBoundingBox[]      scanningWindows         = swg.Generate(initFrame.Size, initBb);
            List <IBoundingBox> sortedScanningWindows   = Service.SortScanningWindowsByOverlap(scanningWindows.ToList(), initBb);
            List <IBoundingBox> negativeScanningWindows = Service.GetScanningWindowsWithOverlapLessThan(0.2f, initBb, sortedScanningWindows);

            List <Image <Gray, byte> > negativePatches = new List <Image <Gray, byte> >();
            int negativeCount = 100;
            int step          = negativeScanningWindows.Count / negativeCount;

            for (int i = 0; i < negativeCount; i++)
            {
                IBoundingBox bb = negativeScanningWindows[i * step];
                negativePatches.Add(initFrame.GetPatch(bb.Center, Size.Round(bb.Size)));
            }

            // arrange - instantiate and initialize base classifier
            PixelComparisonGroupF pcg = new PixelComparisonGroupF();

            pcg.Value = new PixelComparisonF[]
            {
                new PixelComparisonF(new PointF(0, 0.5f), new PointF(0.5f, 0.5f)),
                new PixelComparisonF(new PointF(0.5f, 0), new PointF(0.5f, 0.5f)),
                new PixelComparisonF(new PointF(1, 0.5f), new PointF(0.5f, 0.5f)),
                new PixelComparisonF(new PointF(0.5f, 1), new PointF(0.5f, 0.5f))
            };
            IPixelComparisonScheduler pxs = new PixelComparisonSchedulerStub(
                new PixelComparisonGroupF[] {
                pcg
            }
                );
            BaseClassifier classifier = new BaseClassifier(swg, 0, pxs);

            classifier.PostInstantiation();
            classifier.Update(positivePatches, negativePatches);

            // arrange - define current frame used scanning windows
            Image <Gray, byte> currentFrame = new Image <Gray, byte>(Path.Combine(_resourceDir, "LP_down_right.jpg"));

            CvInvoke.cvSmooth(currentFrame, currentFrame, SMOOTH_TYPE.CV_GAUSSIAN, 0, 0, 1.5, 1.5);
            List <IBoundingBox> usedScanningWindows = new List <IBoundingBox>()
            {
                new BoundingBox(new PointF(163, 132), new SizeF(288, 220)),
                new BoundingBox(new PointF(478, 127), new SizeF(300, 199)),
                new BoundingBox(new PointF(150, 355), new SizeF(308, 214)),
                new BoundingBox(new PointF(480, 364), new SizeF(277, 217)),
            };

            // get actual
            List <double> posteriors = new List <double>();

            foreach (IBoundingBox bb in usedScanningWindows)
            {
                posteriors.Add(classifier.GetPosterior(currentFrame, bb));
            }

            // assert
            Assert.AreEqual(posteriors.Count, usedScanningWindows.Count);
            Assert.IsTrue(posteriors[0] < 0.5);
            Assert.IsTrue(posteriors[1] < 0.5);
            Assert.IsTrue(posteriors[2] < 0.5);
            Assert.IsTrue(posteriors[3] > 0.5);
        }
Exemplo n.º 5
0
        public void GetPosteriors_InitializationAndOneMethodCall()
        {
            // arrange - generate pixel comparisons
            PixelComparisonGroupF pcg1 = new PixelComparisonGroupF();

            pcg1.Value = new PixelComparisonF[]
            {
                new PixelComparisonF(new PointF(0, 0.5f), new PointF(0.5f, 0.5f)),
                new PixelComparisonF(new PointF(0.5f, 0), new PointF(0.5f, 0.5f)),
                new PixelComparisonF(new PointF(1, 0.5f), new PointF(0.5f, 0.5f)),
                new PixelComparisonF(new PointF(0.5f, 1), new PointF(0.5f, 0.5f))
            };
            PixelComparisonGroupF pcg2 = new PixelComparisonGroupF();

            pcg2.Value = new PixelComparisonF[]
            {
                new PixelComparisonF(new PointF(0, 0.5f), new PointF(0.5f, 0.5f)),
                new PixelComparisonF(new PointF(0.5f, 0), new PointF(0.5f, 0.5f)),
                new PixelComparisonF(new PointF(1, 0.5f), new PointF(0.5f, 0.5f)),
                new PixelComparisonF(new PointF(0.5f, 1), new PointF(0.5f, 0.5f))
            };
            IPixelComparisonScheduler pxs = new PixelComparisonSchedulerStub(
                new PixelComparisonGroupF[] {
                pcg1,
                pcg2
            }
                );

            // arrange - instantiate ensamble classifier
            ScanningWindowGeneratorStub swgEnsemble = new ScanningWindowGeneratorStub();
            IBoundingBox positiveScanningWindow     = new BoundingBox(new PointF(480, 364), new SizeF(277, 217));

            swgEnsemble.ScanningWindows = new IBoundingBox[]
            {
                new BoundingBox(new PointF(163, 132), new SizeF(288, 220)),
                new BoundingBox(new PointF(478, 127), new SizeF(300, 199)),
                new BoundingBox(new PointF(150, 355), new SizeF(290, 214)),
                positiveScanningWindow,
            };
            foreach (IBoundingBox bb in swgEnsemble.ScanningWindows)
            {
                bb.ScanningWindow = bb.CreateScanningWindow();
            }
            EnsembleClassifier classifier = new EnsembleClassifier(swgEnsemble, pxs, 1.5);

            classifier.PostInstantiation();

            // arrange - define initialization frame and bb
            Image <Gray, byte> initFrame = new Image <Gray, byte>(Path.Combine(_resourceDir, "LP_up_left.jpg"));
            IBoundingBox       initBb    = new BoundingBox(new PointF(170, 150), new SizeF(269, 189));

            // arrange - initialize ensemble classifier
            classifier.Initialize(initFrame, initBb);

            // arrange - generate positive and negative patches
            // -> positive patches
            List <Image <Gray, byte> > positivePatches = new List <Image <Gray, byte> >();
            int positiveCount = 100;

            for (int i = 0; i < positiveCount; i++)
            {
                Image <Gray, byte> patch = Service.GenerateSimilarPatch(initFrame, initBb, 0, 0, 0);
                CvInvoke.cvSmooth(patch, patch, SMOOTH_TYPE.CV_GAUSSIAN, 0, 0, 1.5, 1.5);
                positivePatches.Add(patch);
            }

            // -> negative patches
            IScanningWindowGenerator swg                     = new ScanningWindowGenerator(1.5f, 0.7f, 0.7f, 30);
            List <IBoundingBox>      scanningWindows         = swg.Generate(initFrame.Size, initBb).ToList();
            List <IBoundingBox>      sortedScanningWindows   = Service.SortScanningWindowsByOverlap(scanningWindows, initBb);
            List <IBoundingBox>      negativeScanningWindows = Service.GetScanningWindowsWithOverlapLessThan(0.2f, initBb, sortedScanningWindows);

            List <Image <Gray, byte> > negativePatches = new List <Image <Gray, byte> >();
            int negativeCount = 100;
            int step          = negativeScanningWindows.Count / negativeCount;

            for (int i = 0; i < negativeCount; i++)
            {
                IBoundingBox bb = negativeScanningWindows[i * step];
                negativePatches.Add(initFrame.GetPatch(bb.Center, Size.Round(bb.Size)));
            }

            // update classifier with new positive and negative patches
            classifier.Update(positivePatches, negativePatches);

            // arrange - define current frame
            Image <Gray, byte> currentFrame = new Image <Gray, byte>(Path.Combine(_resourceDir, "LP_down_right.jpg"));

            // define expected
            List <int> expected = new List <int>()
            {
                3
            };

            // get actual
            List <int> actual = classifier.AcceptedWindows(currentFrame, new List <int> {
                0, 1, 2, 3
            });

            // assert
            CollectionAssert.AreEqual(expected, actual);
        }