public void RejectPatches() { // arrange - create object model ObjectModelStub objectModel = new ObjectModelStub(); // arrange - create scanning window generator with dummy windows ScanningWindowGeneratorStub swg = new ScanningWindowGeneratorStub(); swg.ScanningWindows = new IBoundingBox[] { new BoundingBox(new PointF(10, 10), new SizeF(5, 5)), new BoundingBox(new PointF(10, 10), new SizeF(5, 5)), new BoundingBox(new PointF(10, 10), new SizeF(5, 5)), }; // arrange - create nn classifier NnClassifier classifier = new NnClassifier(swg, objectModel, 0.75f, 0.6f); classifier.PostInstantiation(); classifier.Initialize(new Image <Gray, byte>(new Size(0, 0)), new BoundingBox(new PointF(), new SizeF())); // arrange - hard code relative similarities of patches in scanning windows // 1. relative similarity of the patch in the first scanning window // is below the threshold -> scanning window is rejected // 2. relative similarity of the patch in the second scanning window // is the same as the threshold -> scanning window is rejected // 3. relative similarity of the patch in the third scanning window // is above the threshold -> scanning window is accepted objectModel.RelativeSimilarities = new List <float>() { 0.1f, 0.6f, 0.9f }; objectModel.PnnSimilarities = new List <float>() { 0, 0.9f, 0.9f }; objectModel.NnnSimilarities = new List <float>() { 1, 0.9f, 0 }; // define expected accepted scanning windows List <int> expected = new List <int>() { 2 }; // get actual accepted scanning windows List <int> actual = classifier.AcceptedWindows(new Image <Gray, byte>(new Size(1, 1)), new List <int>() { 0, 1, 2 }); // assert CollectionAssert.AreEqual(expected, actual); }
public void CalculateIntegerBinaryCodeForWindow_1Window() { // arrange - load frame Image <Gray, byte> frame = new Image <Gray, byte>(new Size(2, 2)); frame[0, 0] = new Gray(1); frame[0, 1] = new Gray(2); frame[1, 0] = new Gray(3); frame[1, 1] = new Gray(4); // arrange - create scanning window generator with 1 scanning window ScanningWindowGeneratorStub swg = new ScanningWindowGeneratorStub(); IBoundingBox bb = new BoundingBox(new PointF(0.5f, 0.5f), new SizeF(2, 2)); bb.ScanningWindow = bb.CreateScanningWindow(); swg.ScanningWindows = new IBoundingBox[] { bb, }; // arrange - create pixel comparison scheduler with for 1 base classifier with 4 comparisons PixelComparisonSchedulerStub pcs = new PixelComparisonSchedulerStub(); PixelComparisonGroupF pcg = new PixelComparisonGroupF(); pcg.Value = new PixelComparisonF[] { new PixelComparisonF(new PointF(0, 0), new PointF(1, 0)), new PixelComparisonF(new PointF(0, 1), new PointF(1, 1)), new PixelComparisonF(new PointF(0, 1), new PointF(0, 0)), new PixelComparisonF(new PointF(1, 1), new PointF(1, 0)) }; pcs.ScheduledPixelComparisons = new PixelComparisonGroupF[] { pcg }; // arrange - create base classifier BaseClassifier classifier = new BaseClassifier(swg, 0, pcs); classifier.PostInstantiation(); classifier.Initialize(); // define expected int expected = 3; // get actual classifier.SetCurrentFrame(frame); int actual = classifier.CalculateIntegerBinaryCodeForWindow(0); // assert Assert.AreEqual(expected, actual); }
public void StretchComparisonsOnWindow_LessSimple() { // arrange - create scanning window generator with 2 scanning windows ScanningWindowGeneratorStub swg = new ScanningWindowGeneratorStub(); // first window is not important IBoundingBox bb1 = new BoundingBox(new PointF(), new SizeF()); // second window is important IBoundingBox bb2 = new BoundingBox(new PointF(1.5f, 3.0f), new SizeF(2, 3)); bb2.ScanningWindow = bb2.CreateScanningWindow(); swg.ScanningWindows = new IBoundingBox[] { bb1, bb2 }; // arrange - create pixel comparison scheduler with for 1 base classifier with 2 comparisons PixelComparisonSchedulerStub pcs = new PixelComparisonSchedulerStub(); PixelComparisonGroupF pcg = new PixelComparisonGroupF(); pcg.Value = new PixelComparisonF[] { new PixelComparisonF(new PointF(0, 0), new PointF(0, 0.5f)), new PixelComparisonF(new PointF(1, 0.5f), new PointF(1, 1)) }; pcs.ScheduledPixelComparisons = new PixelComparisonGroupF[] { pcg }; // arrange - create base classifier BaseClassifier classifier = new BaseClassifier(swg, 0, pcs); // define expected Point[][] expected = new Point[2][]; expected[0] = new Point[] { new Point(1, 2), new Point(1, 3) }; expected[1] = new Point[] { new Point(2, 3), new Point(2, 4) }; // get actual Point[][] actual = classifier.StretchComparisonsOnWindow(1); // assert Assert.AreEqual(2, actual.Length); Assert.AreEqual(2, actual[0].Length); Assert.AreEqual(2, actual[1].Length); Assert.AreEqual(expected[0][0], actual[0][0]); Assert.AreEqual(expected[0][1], actual[0][1]); Assert.AreEqual(expected[1][0], actual[1][0]); Assert.AreEqual(expected[1][1], actual[1][1]); }
public void CalculateBinaryCodeForWindow_1Window() { // arrange - load frame Image <Gray, byte> frame = new Image <Gray, byte>(Path.Combine(_resourceDir, "patch_12x10.jpg")); // arrange - create scanning window generator with 1 scanning window ScanningWindowGeneratorStub swg = new ScanningWindowGeneratorStub(); IBoundingBox bb = new BoundingBox(new PointF(6.5f, 4.0f), new SizeF(1, 2)); bb.ScanningWindow = bb.CreateScanningWindow(); swg.ScanningWindows = new IBoundingBox[] { bb, }; // arrange - create pixel comparison scheduler with for 1 base classifier with 4 comparisons PixelComparisonSchedulerStub pcs = new PixelComparisonSchedulerStub(); PixelComparisonGroupF pcg = new PixelComparisonGroupF(); pcg.Value = new PixelComparisonF[] { new PixelComparisonF(new PointF(0, 0), new PointF(1, 0)), new PixelComparisonF(new PointF(0, 1), new PointF(1, 1)), new PixelComparisonF(new PointF(0, 0), new PointF(0, 1)), new PixelComparisonF(new PointF(1, 0), new PointF(1, 1)) }; pcs.ScheduledPixelComparisons = new PixelComparisonGroupF[] { pcg }; // arrange - create base classifier BaseClassifier classifier = new BaseClassifier(swg, 0, pcs); classifier.PostInstantiation(); classifier.Initialize(); // define expected bool[] expected = new bool[] { false, false, true, true }; // get actual classifier.SetCurrentFrame(frame); bool[] actual = classifier.CalculateBinaryCodeForWindow(0).Data; // assert CollectionAssert.AreEqual(expected, actual); }
public void RejectPatches() { // arrange - create object model IObjectModel objectModel = new ObjectModel(new Size(15, 15)); objectModel.PostInstantiation(); // -> create 2 positive and 2 negative patches Image <Gray, byte> helpImage = new Image <Gray, byte>(Path.Combine(_resourceDir, "violeta_1.jpg")); Image <Gray, byte> posPatch1 = helpImage.GetPatch(new PointF(168, 178), new Size(80, 40)).Resize(objectModel.PatchSize.Width, objectModel.PatchSize.Height, INTER.CV_INTER_LINEAR); Image <Gray, byte> posPatch2 = helpImage.GetPatch(new PointF(169, 179), new Size(81, 41)).Resize(objectModel.PatchSize.Width, objectModel.PatchSize.Height, INTER.CV_INTER_LINEAR); Image <Gray, byte> negPatch1 = helpImage.GetPatch(new PointF(300, 200), new Size(50, 50)).Resize(objectModel.PatchSize.Width, objectModel.PatchSize.Height, INTER.CV_INTER_LINEAR); Image <Gray, byte> negPatch2 = helpImage.GetPatch(new PointF(200, 300), new Size(50, 50)).Resize(objectModel.PatchSize.Width, objectModel.PatchSize.Height, INTER.CV_INTER_LINEAR); // -> add patches to the object model objectModel.AddPositivePatch(posPatch1); objectModel.AddPositivePatch(posPatch2); objectModel.AddNegativePatch(negPatch1); objectModel.AddNegativePatch(negPatch2); // arrange - create scanning window generator with stub windows ScanningWindowGeneratorStub swg = new ScanningWindowGeneratorStub(); swg.ScanningWindows = new IBoundingBox[] { new BoundingBox(new PointF(167, 177), new SizeF(79, 39)), new BoundingBox(new PointF(300, 300), new SizeF(80, 40)) }; // arrange - create classifier NnClassifier classifier = new NnClassifier(swg, objectModel, 0.75f, 0.6f); classifier.PostInstantiation(); classifier.Initialize(new Image <Gray, byte>(new Size(0, 0)), new BoundingBox(new PointF(), new SizeF())); // define expected accepted patches List <int> expected = new List <int>() { 0 }; // get actual accepted patches List <int> actual = classifier.AcceptedWindows(helpImage, new List <int>() { 0, 1 }); // assert CollectionAssert.AreEqual(expected, actual); }
public void StretchComparisonsOnWindow_Simple() { // arrange - create scanning window generator with 1 scanning window ScanningWindowGeneratorStub swg = new ScanningWindowGeneratorStub(); IBoundingBox bb = new BoundingBox(new PointF(1.5f, 1.5f), new SizeF(2, 2)); bb.ScanningWindow = bb.CreateScanningWindow(); swg.ScanningWindows = new IBoundingBox[] { bb }; // arrange - create pixel comparison scheduler with for 1 base classifier with 2 comparisons PixelComparisonSchedulerStub pcs = new PixelComparisonSchedulerStub(); PixelComparisonGroupF pcg = new PixelComparisonGroupF(); pcg.Value = new PixelComparisonF[] { new PixelComparisonF(new PointF(0, 0), new PointF(1, 0)), new PixelComparisonF(new PointF(0, 0), new PointF(0, 1)) }; pcs.ScheduledPixelComparisons = new PixelComparisonGroupF[] { pcg }; // arrange - create base classifier BaseClassifier classifier = new BaseClassifier(swg, 0, pcs); // define expected Point[][] expected = new Point[2][]; expected[0] = new Point[] { new Point(1, 1), new Point(2, 1) }; expected[1] = new Point[] { new Point(1, 1), new Point(1, 2) }; // get actual Point[][] actual = classifier.StretchComparisonsOnWindow(0); // assert Assert.AreEqual(2, actual.Length); Assert.AreEqual(2, actual[0].Length); Assert.AreEqual(2, actual[1].Length); Assert.AreEqual(expected[0][0], actual[0][0]); Assert.AreEqual(expected[0][1], actual[0][1]); Assert.AreEqual(expected[1][0], actual[1][0]); Assert.AreEqual(expected[1][1], actual[1][1]); }
public void RejectPatches_FrameRoiIsNotSetAfterProcessing() { // arrange - create object model IObjectModel objectModel = new ObjectModel(new Size(15, 15)); objectModel.PostInstantiation(); // -> create 1 patch Image <Gray, byte> helpImage = new Image <Gray, byte>(Path.Combine(_resourceDir, "violeta_1.jpg")); Image <Gray, byte> patch = helpImage.GetPatch(new PointF(168, 178), new Size(80, 40)).Resize(objectModel.PatchSize.Width, objectModel.PatchSize.Height, INTER.CV_INTER_LINEAR); // -> add patch to the object model objectModel.AddPositivePatch(patch); // arrange - create scanning window generator with stub windows ScanningWindowGeneratorStub swg = new ScanningWindowGeneratorStub(); swg.ScanningWindows = new IBoundingBox[] { new BoundingBox(new PointF(167, 177), new SizeF(79, 39)) }; // arrange - create classifier NnClassifier classifier = new NnClassifier(swg, objectModel, 0.75f, 0.6f); classifier.PostInstantiation(); classifier.Initialize(new Image <Gray, byte>(new Size(0, 0)), new BoundingBox(new PointF(), new SizeF())); // define expected frame ROI after processing Rectangle expected = new Rectangle(new Point(), helpImage.Size); // get actual frame ROI after processing classifier.AcceptedWindows(helpImage, new List <int>() { 0 }); Rectangle actual = helpImage.ROI; // assert Assert.AreEqual(expected, actual); Assert.IsTrue(!helpImage.IsROISet); // just in case }
public void StretchComparisonsOnWindows_2Windows() { // arrange - create scanning window generator with 2 scanning windows ScanningWindowGeneratorStub swg = new ScanningWindowGeneratorStub(); IBoundingBox bb1 = new BoundingBox(new PointF(1.5f, 1.5f), new SizeF(2, 2)); bb1.ScanningWindow = bb1.CreateScanningWindow(); IBoundingBox bb2 = new BoundingBox(new PointF(1.5f, 3.0f), new SizeF(2, 3)); bb2.ScanningWindow = bb2.CreateScanningWindow(); swg.ScanningWindows = new IBoundingBox[] { bb1, bb2 }; // arrange - create pixel comparison scheduler with for 1 base classifier with 2 comparisons PixelComparisonSchedulerStub pcs = new PixelComparisonSchedulerStub(); PixelComparisonGroupF pcg = new PixelComparisonGroupF(); pcg.Value = new PixelComparisonF[] { new PixelComparisonF(new PointF(0, 0), new PointF(1, 0)), new PixelComparisonF(new PointF(0, 0), new PointF(0, 1)) }; pcs.ScheduledPixelComparisons = new PixelComparisonGroupF[] { pcg }; // arrange - create base classifier BaseClassifier classifier = new BaseClassifier(swg, 0, pcs); // define expected for 1st window Point[][] expected1 = new Point[2][]; expected1[0] = new Point[] { new Point(1, 1), new Point(2, 1) }; expected1[1] = new Point[] { new Point(1, 1), new Point(1, 2) }; // define expected for 2nd window Point[][] expected2 = new Point[2][]; expected2[0] = new Point[] { new Point(1, 2), new Point(2, 2) }; expected2[1] = new Point[] { new Point(1, 2), new Point(1, 4) }; // get actual classifier.Initialize(); Point[][][] AllComparisons = classifier.ComparisonsForWindow; Point[][] actual1 = AllComparisons[0]; Point[][] actual2 = AllComparisons[1]; // assert for 1st window Assert.AreEqual(2, actual1.Length); Assert.AreEqual(2, actual1[0].Length); Assert.AreEqual(2, actual1[1].Length); Assert.AreEqual(expected1[0][0], actual1[0][0]); Assert.AreEqual(expected1[0][1], actual1[0][1]); Assert.AreEqual(expected1[1][0], actual1[1][0]); Assert.AreEqual(expected1[1][1], actual1[1][1]); // assert for 2nd window Assert.AreEqual(2, actual2.Length); Assert.AreEqual(2, actual2[0].Length); Assert.AreEqual(2, actual2[1].Length); Assert.AreEqual(expected2[0][0], actual2[0][0]); Assert.AreEqual(expected2[0][1], actual2[0][1]); Assert.AreEqual(expected2[1][0], actual2[1][0]); Assert.AreEqual(expected2[1][1], actual2[1][1]); }
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); }