public void EqualizeHistogram() { const string testName = nameof(EqualizeHistogram); var path = this.GetDataFile($"{LoadTarget}.bmp"); var tests = new[] { new { Type = ImageTypes.BgrPixel, ExpectResult = true }, new { Type = ImageTypes.RgbPixel, ExpectResult = true }, new { Type = ImageTypes.RgbAlphaPixel, ExpectResult = false }, new { Type = ImageTypes.UInt8, ExpectResult = true }, new { Type = ImageTypes.UInt16, ExpectResult = true }, new { Type = ImageTypes.UInt32, ExpectResult = false }, new { Type = ImageTypes.Int8, ExpectResult = false }, new { Type = ImageTypes.Int16, ExpectResult = false }, new { Type = ImageTypes.Int32, ExpectResult = false }, new { Type = ImageTypes.HsiPixel, ExpectResult = true }, new { Type = ImageTypes.Float, ExpectResult = false }, new { Type = ImageTypes.Double, ExpectResult = false } }; var type = this.GetType().Name; foreach (var input in tests) { var expectResult = input.ExpectResult; var imageObj = DlibTest.LoadImageHelp(input.Type, path); var outputImageAction = new Func <bool, Array2DBase>(expect => { Dlib.EqualizeHistogram(imageObj); return(imageObj); }); var successAction = new Action <Array2DBase>(image => { Dlib.SaveBmp(image, $"{Path.Combine(this.GetOutDir(type, testName), $"{LoadTarget}_{input.Type}.bmp")}"); }); var failAction = new Action(() => { Assert.True(false, $"{testName} should throw exception for InputType: {input.Type}."); }); var finallyAction = new Action(() => { if (imageObj != null) { this.DisposeAndCheckDisposedState(imageObj); } }); var exceptionAction = new Action(() => { Console.WriteLine($"Failed to execute {testName} to InputType: {input.Type}, Type: {input.Type}."); }); DoTest(outputImageAction, expectResult, successAction, finallyAction, failAction, exceptionAction); } }
public void ExtractImageChip() { var path = this.GetDataFile($"{LoadTarget}.bmp"); const int loop = 1000; var sizeArray = new long[loop]; var first = GetCurrentMemory(); var start = GetCurrentMemory() - first; using (var image = DlibTest.LoadImageHelp(ImageTypes.RgbPixel, path)) using (var dims = new ChipDims(227, 227)) using (var chip = new ChipDetails(new Rectangle(0, 0, 100, 100), dims)) for (var count = 0; count < loop; count++) { using (Dlib.ExtractImageChip <RgbPixel>(image, chip)) sizeArray[count] = GetCurrentMemory(); } // Important!! GC.Collect(2, GCCollectionMode.Forced, true); var end = GetCurrentMemory() - first; Console.WriteLine(" Start Total Memory = {0} KB", start / 1024); Console.WriteLine(" End Total Memory = {0} KB", end / 1024); Console.WriteLine("Delta (End - Start) Memory = {0} KB", (end - start) / 1024); // Rough estimate whether occur memory leak (less than 10240KB) Assert.True((end - start) / 1024 < 10240); }
public void ExtractFHogFeatures2() { var path = this.GetDataFile($"{LoadTarget}.bmp"); var tests = new[] { new { Type = MatrixElementTypes.Float, ExpectResult = true }, new { Type = MatrixElementTypes.Double, ExpectResult = true } }; foreach (var output in tests) { Array2DBase imageObj = null; Array2DMatrixBase outputObj = null; try { imageObj = DlibTest.LoadImageHelp(ImageTypes.RgbPixel, path); switch (output.Type) { case MatrixElementTypes.Float: outputObj = Dlib.ExtractFHogFeatures <float>(imageObj); break; case MatrixElementTypes.Double: outputObj = Dlib.ExtractFHogFeatures <double>(imageObj); break; default: throw new ArgumentOutOfRangeException(); } MatrixBase matrix = Dlib.DrawFHog(outputObj); if (this.CanGuiDebug) { var window = new ImageWindow(matrix); window.WaitUntilClosed(); } } catch (Exception e) { Console.WriteLine(e); throw; } finally { if (imageObj != null) { this.DisposeAndCheckDisposedState(imageObj); } if (outputObj != null) { this.DisposeAndCheckDisposedState(outputObj); } } } }
public void DetectFace2() { if (this._ShapePredictor == null) { Assert.True(false, "ShapePredictor is not initialized!!"); } const string testName = "DetectFace2"; var path = this.GetDataFile("Lenna_mini.bmp"); var tests = new[] { new { Type = ImageTypes.BgrPixel, ExpectResult = true }, new { Type = ImageTypes.RgbPixel, ExpectResult = true }, new { Type = ImageTypes.UInt8, ExpectResult = true }, new { Type = ImageTypes.UInt16, ExpectResult = true }, new { Type = ImageTypes.UInt32, ExpectResult = true }, new { Type = ImageTypes.Int8, ExpectResult = true }, new { Type = ImageTypes.Int16, ExpectResult = true }, new { Type = ImageTypes.Int32, ExpectResult = true }, new { Type = ImageTypes.HsiPixel, ExpectResult = true }, new { Type = ImageTypes.LabPixel, ExpectResult = true }, new { Type = ImageTypes.Float, ExpectResult = true }, new { Type = ImageTypes.Double, ExpectResult = true }, new { Type = ImageTypes.RgbAlphaPixel, ExpectResult = false } }; using (var faceDetector = Dlib.GetFrontalFaceDetector()) foreach (var input in tests) { var expectResult = input.ExpectResult; var imageObj = DlibTest.LoadImageHelp(input.Type, path); Rectangle[] dets = null; var outputImageAction = new Func <bool, Array2DBase>(expect => { dets = faceDetector.Operator(imageObj); return(imageObj); }); var successAction = new Action <Array2DBase>(image => { var rects = new List <Rectangle>(); const int offset = 1; var shapes = dets.Select(r => this._ShapePredictor.Detect(image, r)).ToList(); foreach (var shape in shapes) { var r = shape.Rect; var parts = shape.Parts; for (uint i = 0; i < parts; i++) { var part = shape.GetPart(i); var pr = new Rectangle( part.X - offset, part.Y - offset, part.X + offset, part.Y + offset); rects.Add(pr); } rects.Add(r); } // This test does NOT check whether output image and detect face area are correct //Dlib.SaveJpeg(image, $"{Path.Combine(this.GetOutDir(type, testName), $"2008_001322_{input.Type}.jpg")}"); }); var failAction = new Action(() => { Assert.True(false, $"{testName} should throw exception for InputType: {input.Type}."); }); var finallyAction = new Action(() => { this.DisposeAndCheckDisposedState(imageObj); }); var exceptionAction = new Action(() => { Console.WriteLine($"Failed to execute {testName} to InputType: {input.Type}."); }); DoTest(outputImageAction, expectResult, successAction, finallyAction, failAction, exceptionAction); } }
public void ExtractFHogFeatures3() { const string testName = nameof(this.ExtractFHogFeatures3); var path = this.GetDataFile($"{LoadTarget}.bmp"); var tests = new[] { new { Type = MatrixElementTypes.Float, ExpectResult = true }, new { Type = MatrixElementTypes.Double, ExpectResult = true }, new { Type = MatrixElementTypes.RgbPixel, ExpectResult = true }, new { Type = MatrixElementTypes.RgbAlphaPixel, ExpectResult = true }, new { Type = MatrixElementTypes.HsiPixel, ExpectResult = true }, new { Type = MatrixElementTypes.LabPixel, ExpectResult = true }, new { Type = MatrixElementTypes.UInt32, ExpectResult = true }, new { Type = MatrixElementTypes.UInt8, ExpectResult = true }, new { Type = MatrixElementTypes.UInt16, ExpectResult = true }, new { Type = MatrixElementTypes.Int8, ExpectResult = true }, new { Type = MatrixElementTypes.Int16, ExpectResult = true }, new { Type = MatrixElementTypes.Int32, ExpectResult = true } }; foreach (ImageTypes inputType in Enum.GetValues(typeof(ImageTypes))) { foreach (var output in tests) { if (inputType == ImageTypes.Matrix) { continue; } var expectResult = output.ExpectResult; var imageObj = DlibTest.LoadImageHelp(inputType, path); Matrix <double> outputObj = null; var outputImageAction = new Func <bool, Matrix <double> >(expect => { switch (output.Type) { case MatrixElementTypes.UInt8: Dlib.ExtractFHogFeatures <byte>(imageObj, out outputObj); break; case MatrixElementTypes.UInt16: Dlib.ExtractFHogFeatures <ushort>(imageObj, out outputObj); break; case MatrixElementTypes.UInt32: Dlib.ExtractFHogFeatures <uint>(imageObj, out outputObj); break; case MatrixElementTypes.Int8: Dlib.ExtractFHogFeatures <sbyte>(imageObj, out outputObj); break; case MatrixElementTypes.Int16: Dlib.ExtractFHogFeatures <short>(imageObj, out outputObj); break; case MatrixElementTypes.Int32: Dlib.ExtractFHogFeatures <int>(imageObj, out outputObj); break; case MatrixElementTypes.Float: Dlib.ExtractFHogFeatures <float>(imageObj, out outputObj); break; case MatrixElementTypes.Double: Dlib.ExtractFHogFeatures <double>(imageObj, out outputObj); break; case MatrixElementTypes.RgbPixel: Dlib.ExtractFHogFeatures <RgbPixel>(imageObj, out outputObj); break; case MatrixElementTypes.RgbAlphaPixel: Dlib.ExtractFHogFeatures <RgbAlphaPixel>(imageObj, out outputObj); break; case MatrixElementTypes.HsiPixel: Dlib.ExtractFHogFeatures <HsiPixel>(imageObj, out outputObj); break; case MatrixElementTypes.LabPixel: Dlib.ExtractFHogFeatures <LabPixel>(imageObj, out outputObj); break; default: throw new ArgumentOutOfRangeException(); } return(outputObj); }); var successAction = new Action <Matrix <double> >(image => { }); var failAction = new Action(() => { Assert.True(false, $"{testName} should throw exception for InputType: {inputType}, OutputType: {output.Type}."); }); var finallyAction = new Action(() => { if (imageObj != null) { this.DisposeAndCheckDisposedState(imageObj); } if (outputObj != null) { this.DisposeAndCheckDisposedState(outputObj); } }); var exceptionAction = new Action(() => { Console.WriteLine($"Failed to execute {testName} to InputType: {inputType}, OutputType: {output.Type}."); }); DoTest(outputImageAction, expectResult, successAction, finallyAction, failAction, exceptionAction); } } }
public void SobelEdgeDetector() { var path = this.GetDataFile($"{LoadTarget}.bmp"); var tests = new[] { new { Type = ImageTypes.BgrPixel, ExpectResult = false }, new { Type = ImageTypes.RgbPixel, ExpectResult = false }, new { Type = ImageTypes.RgbAlphaPixel, ExpectResult = false }, new { Type = ImageTypes.UInt8, ExpectResult = false }, new { Type = ImageTypes.UInt16, ExpectResult = false }, new { Type = ImageTypes.HsiPixel, ExpectResult = false }, new { Type = ImageTypes.Float, ExpectResult = true }, new { Type = ImageTypes.Double, ExpectResult = true } }; var type = this.GetType().Name; foreach (ImageTypes inputType in Enum.GetValues(typeof(ImageTypes))) { if (inputType == ImageTypes.Matrix) { continue; } foreach (var test in tests) { TwoDimensionObjectBase imageObj = null; TwoDimensionObjectBase horzObj = null; TwoDimensionObjectBase vertObj = null; try { var image = DlibTest.LoadImageHelp(inputType, path); imageObj = image; var horz = Array2DTest.CreateArray2DHelp(test.Type); horzObj = horz; var vert = Array2DTest.CreateArray2DHelp(test.Type); vertObj = vert; try { Dlib.SobelEdgeDetector(image, horz, vert); if (!test.ExpectResult) { Assert.True(false, $"SobelEdgeDetector should throw exception for InputType: {inputType}, Type: {test.Type}."); } else { Dlib.SaveBmp(horz, $"{Path.Combine(this.GetOutDir(type, "SobelEdgeDetector"), $"{LoadTarget}_{inputType}_{test.Type}_horz.bmp")}"); Dlib.SaveBmp(vert, $"{Path.Combine(this.GetOutDir(type, "SobelEdgeDetector"), $"{LoadTarget}_{inputType}_{test.Type}_vert.bmp")}"); } } catch (Exception) { if (!test.ExpectResult) { Console.WriteLine("OK"); } else { throw; } } } catch (Exception e) { Console.WriteLine(e.StackTrace); Console.WriteLine($"Failed to execute SobelEdgeDetector to InputType: {inputType}, Type: {test.Type}."); throw; } finally { if (imageObj != null) { this.DisposeAndCheckDisposedState(imageObj); } if (horzObj != null) { this.DisposeAndCheckDisposedState(horzObj); } if (vertObj != null) { this.DisposeAndCheckDisposedState(vertObj); } } } } }
public void SuppressNonMaximumEdges() { var path = this.GetDataFile($"{LoadTarget}.bmp"); var tests = new[] { new { Type = ImageTypes.BgrPixel, ExpectResult = true }, new { Type = ImageTypes.RgbPixel, ExpectResult = true }, new { Type = ImageTypes.RgbAlphaPixel, ExpectResult = true }, new { Type = ImageTypes.UInt8, ExpectResult = true }, new { Type = ImageTypes.UInt16, ExpectResult = true }, new { Type = ImageTypes.HsiPixel, ExpectResult = true }, new { Type = ImageTypes.Float, ExpectResult = true }, new { Type = ImageTypes.Double, ExpectResult = true } }; var type = this.GetType().Name; foreach (var inputType in new[] { ImageTypes.Float, ImageTypes.Double }) { foreach (var test in tests) { Array2DBase inputObj = null; Array2DBase horzObj = null; Array2DBase vertObj = null; Array2DBase outputObj = null; try { var inputImage = DlibTest.LoadImageHelp(inputType, path); inputObj = inputImage; var horz = Array2DTest.CreateArray2DHelp(inputType); horzObj = horz; var vert = Array2DTest.CreateArray2DHelp(inputType); vertObj = vert; var outputImage = Array2DTest.CreateArray2DHelp(test.Type); outputObj = outputImage; Dlib.SobelEdgeDetector(inputImage, horz, vert); try { Dlib.SuppressNonMaximumEdges(horz, vert, outputImage); if (!test.ExpectResult) { Assert.True(false, $"SuppressNonMaximumEdges should throw exception for InputType: {inputType}, Type: {test.Type}."); } else { Dlib.SaveBmp(outputImage, $"{Path.Combine(this.GetOutDir(type, "SuppressNonMaximumEdges"), $"{LoadTarget}_{inputType}_{test.Type}.bmp")}"); } } catch (ArgumentException) { if (!test.ExpectResult) { Console.WriteLine("OK"); } else { throw; } } catch (NotSupportedException) { if (!test.ExpectResult) { Console.WriteLine("OK"); } else { throw; } } } catch (Exception e) { Console.WriteLine(e.StackTrace); Console.WriteLine($"Failed to execute SuppressNonMaximumEdges to InputType: {inputType}, Type: {test.Type}."); throw; } finally { if (inputObj != null) { this.DisposeAndCheckDisposedState(inputObj); } if (horzObj != null) { this.DisposeAndCheckDisposedState(horzObj); } if (vertObj != null) { this.DisposeAndCheckDisposedState(vertObj); } if (outputObj != null) { this.DisposeAndCheckDisposedState(outputObj); } } } } }
public void DetectFace2() { if (this._FrontalFaceDetector == null) { Assert.True(false, "ShapePredictor is not initialized!!"); } var faceDetector = this._FrontalFaceDetector; const string testName = "DetectFace2"; var path = this.GetDataFile("Lenna_mini.bmp"); var tests = new[] { new { Type = ImageTypes.BgrPixel, ExpectResult = true }, new { Type = ImageTypes.RgbPixel, ExpectResult = true }, new { Type = ImageTypes.UInt8, ExpectResult = true }, new { Type = ImageTypes.UInt16, ExpectResult = true }, new { Type = ImageTypes.UInt32, ExpectResult = true }, new { Type = ImageTypes.Int8, ExpectResult = true }, new { Type = ImageTypes.Int16, ExpectResult = true }, new { Type = ImageTypes.Int32, ExpectResult = true }, new { Type = ImageTypes.HsiPixel, ExpectResult = true }, new { Type = ImageTypes.Float, ExpectResult = true }, new { Type = ImageTypes.Double, ExpectResult = true }, new { Type = ImageTypes.RgbAlphaPixel, ExpectResult = false } }; foreach (var input in tests) { var expectResult = input.ExpectResult; var imageObj = DlibTest.LoadImageHelp(input.Type, path); Rectangle[] dets = null; var outputImageAction = new Func <bool, Rectangle[]>(expect => { dets = faceDetector.Operator(imageObj); return(dets); }); var successAction = new Action <Rectangle[]>(image => { // This test does NOT check whether output image and detect face area are correct //Dlib.SaveJpeg(image, $"{Path.Combine(this.GetOutDir(type, testName), $"2008_001322_{input.Type}.jpg")}"); }); var failAction = new Action(() => { Assert.True(false, $"{testName} should throw exception for InputType: {input.Type}."); }); var finallyAction = new Action(() => { this.DisposeAndCheckDisposedState(imageObj); }); var exceptionAction = new Action(() => { Console.WriteLine($"Failed to execute {testName} to InputType: {input.Type}."); }); DoTest(outputImageAction, expectResult, successAction, finallyAction, failAction, exceptionAction); } }
public void SumFilter() { const string testName = "SumFilter"; var path = this.GetDataFile($"{LoadTarget}.bmp"); var tests = new[] { new { Type = ImageTypes.UInt8, ExpectResult = true }, new { Type = ImageTypes.UInt16, ExpectResult = true }, new { Type = ImageTypes.UInt32, ExpectResult = true }, new { Type = ImageTypes.Int8, ExpectResult = true }, new { Type = ImageTypes.Int16, ExpectResult = true }, new { Type = ImageTypes.Int32, ExpectResult = true }, new { Type = ImageTypes.Float, ExpectResult = true }, new { Type = ImageTypes.Double, ExpectResult = true }, new { Type = ImageTypes.RgbAlphaPixel, ExpectResult = false }, new { Type = ImageTypes.BgrPixel, ExpectResult = false }, new { Type = ImageTypes.RgbPixel, ExpectResult = false }, new { Type = ImageTypes.HsiPixel, ExpectResult = false } }; var type = this.GetType().Name; foreach (var input in tests) { foreach (var output in tests) { TwoDimensionObjectBase outObj = null; TwoDimensionObjectBase inObj = null; var rect = new Rectangle(3, 3); var expect = input.ExpectResult && output.ExpectResult; try { var inImage = DlibTest.LoadImageHelp(input.Type, path); inObj = inImage; try { var outImage = Array2DTest.CreateArray2DHelp(output.Type, inImage.Rows, inImage.Columns); outObj = outImage; Dlib.SumFilter(inImage, outImage, rect); if (!expect) { Assert.True(false, $"{testName} should throw exception for InputType: {input.Type}, OutputType: {output.Type}"); } else { Assert.Equal(inImage.Columns, outImage.Columns); Assert.Equal(inImage.Rows, outImage.Rows); Dlib.SaveBmp(outImage, $"{Path.Combine(this.GetOutDir(type, testName), $"{LoadTarget}_{input.Type}_{output.Type}.bmp")}"); } } catch (ArgumentException) { if (!expect) { Console.WriteLine("OK"); } else { throw; } } catch (NotSupportedException) { if (!expect) { Console.WriteLine("OK"); } else { throw; } } } catch (Exception e) { Console.WriteLine(e.StackTrace); Console.WriteLine($"Failed to execute {testName} to InputType: {input.Type}, OutputType: {output.Type}"); throw; } finally { if (outObj != null) { this.DisposeAndCheckDisposedState(outObj); } if (inObj != null) { this.DisposeAndCheckDisposedState(inObj); } } } } }
public void GaussianBlurThrowException() { var path = this.GetDataFile($"{ LoadTarget}.bmp"); var tests = new[] { new { Type = ImageTypes.BgrPixel, ExpectResult = false, Sigma = 0, MaxSize = 1 }, new { Type = ImageTypes.RgbPixel, ExpectResult = false, Sigma = 0, MaxSize = 1 }, new { Type = ImageTypes.RgbAlphaPixel, ExpectResult = false, Sigma = 0, MaxSize = 1 }, new { Type = ImageTypes.UInt8, ExpectResult = false, Sigma = 0, MaxSize = 1 }, new { Type = ImageTypes.UInt16, ExpectResult = false, Sigma = 0, MaxSize = 1 }, new { Type = ImageTypes.UInt32, ExpectResult = false, Sigma = 0, MaxSize = 1 }, new { Type = ImageTypes.Int8, ExpectResult = false, Sigma = 0, MaxSize = 1 }, new { Type = ImageTypes.Int16, ExpectResult = false, Sigma = 0, MaxSize = 1 }, new { Type = ImageTypes.Int32, ExpectResult = false, Sigma = 0, MaxSize = 1 }, new { Type = ImageTypes.HsiPixel, ExpectResult = false, Sigma = 0, MaxSize = 1 }, new { Type = ImageTypes.Float, ExpectResult = false, Sigma = 0, MaxSize = 1 }, new { Type = ImageTypes.Double, ExpectResult = false, Sigma = 0, MaxSize = 1 }, new { Type = ImageTypes.BgrPixel, ExpectResult = false, Sigma = 10, MaxSize = 0 }, new { Type = ImageTypes.RgbPixel, ExpectResult = false, Sigma = 10, MaxSize = 0 }, new { Type = ImageTypes.RgbAlphaPixel, ExpectResult = false, Sigma = 10, MaxSize = 0 }, new { Type = ImageTypes.UInt8, ExpectResult = false, Sigma = 10, MaxSize = 0 }, new { Type = ImageTypes.UInt16, ExpectResult = false, Sigma = 10, MaxSize = 0 }, new { Type = ImageTypes.UInt32, ExpectResult = false, Sigma = 10, MaxSize = 0 }, new { Type = ImageTypes.Int8, ExpectResult = false, Sigma = 10, MaxSize = 0 }, new { Type = ImageTypes.Int16, ExpectResult = false, Sigma = 10, MaxSize = 0 }, new { Type = ImageTypes.Int32, ExpectResult = false, Sigma = 10, MaxSize = 0 }, new { Type = ImageTypes.HsiPixel, ExpectResult = false, Sigma = 10, MaxSize = 0 }, new { Type = ImageTypes.Float, ExpectResult = false, Sigma = 10, MaxSize = 0 }, new { Type = ImageTypes.Double, ExpectResult = false, Sigma = 10, MaxSize = 0 }, new { Type = ImageTypes.BgrPixel, ExpectResult = false, Sigma = 10, MaxSize = 2 }, new { Type = ImageTypes.RgbPixel, ExpectResult = false, Sigma = 10, MaxSize = 2 }, new { Type = ImageTypes.RgbAlphaPixel, ExpectResult = false, Sigma = 10, MaxSize = 2 }, new { Type = ImageTypes.UInt8, ExpectResult = false, Sigma = 10, MaxSize = 2 }, new { Type = ImageTypes.UInt16, ExpectResult = false, Sigma = 10, MaxSize = 2 }, new { Type = ImageTypes.UInt32, ExpectResult = false, Sigma = 10, MaxSize = 2 }, new { Type = ImageTypes.Int8, ExpectResult = false, Sigma = 10, MaxSize = 2 }, new { Type = ImageTypes.Int16, ExpectResult = false, Sigma = 10, MaxSize = 2 }, new { Type = ImageTypes.Int32, ExpectResult = false, Sigma = 10, MaxSize = 2 }, new { Type = ImageTypes.HsiPixel, ExpectResult = false, Sigma = 10, MaxSize = 2 }, new { Type = ImageTypes.Float, ExpectResult = false, Sigma = 10, MaxSize = 2 }, new { Type = ImageTypes.Double, ExpectResult = false, Sigma = 10, MaxSize = 2 }, new { Type = ImageTypes.BgrPixel, ExpectResult = true, Sigma = 10, MaxSize = 1001 }, new { Type = ImageTypes.RgbPixel, ExpectResult = true, Sigma = 10, MaxSize = 1001 }, //new { Type = ImageTypes.RgbAlphaPixel, ExpectResult = false, Sigma = 10, MaxSize = 1001}, new { Type = ImageTypes.UInt8, ExpectResult = true, Sigma = 10, MaxSize = 1001 }, new { Type = ImageTypes.UInt16, ExpectResult = true, Sigma = 10, MaxSize = 1001 }, new { Type = ImageTypes.UInt32, ExpectResult = true, Sigma = 10, MaxSize = 1001 }, new { Type = ImageTypes.Int8, ExpectResult = true, Sigma = 10, MaxSize = 1001 }, new { Type = ImageTypes.Int16, ExpectResult = true, Sigma = 10, MaxSize = 1001 }, new { Type = ImageTypes.Int32, ExpectResult = true, Sigma = 10, MaxSize = 1001 }, new { Type = ImageTypes.HsiPixel, ExpectResult = true, Sigma = 10, MaxSize = 1001 }, new { Type = ImageTypes.Float, ExpectResult = true, Sigma = 10, MaxSize = 1001 }, new { Type = ImageTypes.Double, ExpectResult = true, Sigma = 10, MaxSize = 1001 } }; var type = this.GetType().Name; foreach (var test in tests) { var outImage = Array2DTest.CreateArray2DHelp(test.Type); var inIage = DlibTest.LoadImageHelp(test.Type, path); try { Dlib.GaussianBlur(inIage, outImage, test.Sigma, test.MaxSize); if (!test.ExpectResult) { Assert.True(false, $"GaussianBlur should throw exception for Type: {test.Type}, Sigma: {test.Sigma}, MaxSize: {test.MaxSize}."); } Dlib.SaveJpeg(outImage, $"{Path.Combine(this.GetOutDir(type, "GaussianBlurThrowException"), $"{LoadTarget}_{test.Type}_{test.Sigma}_{test.MaxSize}.jpg")}"); } catch (Exception) { if (!test.ExpectResult) { Console.WriteLine("OK"); } else { throw; } } finally { if (outImage != null) { this.DisposeAndCheckDisposedState(outImage); } if (inIage != null) { this.DisposeAndCheckDisposedState(inIage); } } } }
public void UpdateNoscale() { const string testName = "UpdateNoscale"; var path = this.GetDataFile($"{LoadTarget}.bmp"); var tests = new[] { new { Type = ImageTypes.BgrPixel, ExpectResult = true }, new { Type = ImageTypes.RgbPixel, ExpectResult = true }, new { Type = ImageTypes.RgbAlphaPixel, ExpectResult = false }, new { Type = ImageTypes.UInt8, ExpectResult = true }, new { Type = ImageTypes.UInt16, ExpectResult = false }, new { Type = ImageTypes.UInt32, ExpectResult = false }, new { Type = ImageTypes.Int8, ExpectResult = false }, new { Type = ImageTypes.Int16, ExpectResult = false }, new { Type = ImageTypes.Int32, ExpectResult = false }, new { Type = ImageTypes.HsiPixel, ExpectResult = false }, new { Type = ImageTypes.LabPixel, ExpectResult = true }, new { Type = ImageTypes.Float, ExpectResult = false }, new { Type = ImageTypes.Double, ExpectResult = false } }; foreach (var input in tests) { var expectResult = input.ExpectResult; var inputType = input.Type; var imageObj = DlibTest.LoadImageHelp(input.Type, path); var tracker = new CorrelationTracker(); var rect = new DRectangle(46, 15, 216, 205); var outputImageAction = new Func <bool, Array2DBase>(expect => { tracker.StartTrack(imageObj, rect); return(imageObj); }); var successAction = new Action <Array2DBase>(image => { tracker.UpdateNoscale(image, rect); }); var failAction = new Action(() => { Assert.True(false, $"{testName} should throw exception for InputType: {inputType}."); }); var finallyAction = new Action(() => { this.DisposeAndCheckDisposedState(tracker); if (imageObj != null) { this.DisposeAndCheckDisposedState(imageObj); } }); var exceptionAction = new Action(() => { Console.WriteLine($"Failed to execute {testName} to InputType: {inputType}."); }); DoTest(outputImageAction, expectResult, successAction, finallyAction, failAction, exceptionAction); } }
public void TryTrack() { const string testName = "TryTrack"; var tests = new[] { new { Type = ImageTypes.BgrPixel, ExpectResult = true }, new { Type = ImageTypes.RgbPixel, ExpectResult = true }, new { Type = ImageTypes.UInt8, ExpectResult = true }, new { Type = ImageTypes.LabPixel, ExpectResult = true } }; var type = this.GetType().Name; foreach (var input in tests) { var tracker = new CorrelationTracker(); try { var index = 0; foreach (var file in this.GetDataFiles("video_frames").Where(info => info.Name.EndsWith("jpg"))) { var expectResult = input.ExpectResult; var inputType = input.Type; var imageObj = DlibTest.LoadImageHelp(input.Type, file); if (index == 0) { var rect = DRectangle.CenteredRect(93, 110, 38, 86); tracker.StartTrack(imageObj, rect); } var outputImageAction = new Func <bool, Array2DBase>(expect => { if (index != 0) { tracker.Update(imageObj); } return(imageObj); }); var successAction = new Action <Array2DBase>(image => { if (index != 0) { tracker.Update(image); } var r = tracker.GetPosition(); using (var img = Dlib.LoadImage <RgbPixel>(file.FullName)) { Dlib.DrawRectangle(img, (Rectangle)r, new RgbPixel { Red = 255 }, 3); Dlib.SaveJpeg(img, Path.Combine(this.GetOutDir(type), $"{Path.GetFileNameWithoutExtension(file.FullName)}.jpg")); } }); var failAction = new Action(() => { Assert.True(false, $"{testName} should throw exception for InputType: {inputType}."); }); var finallyAction = new Action(() => { index++; if (imageObj != null) { this.DisposeAndCheckDisposedState(imageObj); } }); var exceptionAction = new Action(() => { Console.WriteLine($"Failed to execute {testName} to InputType: {inputType}."); }); DoTest(outputImageAction, expectResult, successAction, finallyAction, failAction, exceptionAction); } } finally { this.DisposeAndCheckDisposedState(tracker); } } }
public void FindCandidateObjectLocations() { var path = this.GetDataFile("Lenna.jpg"); var tests = new[] { new { Type = ImageTypes.BgrPixel, ExpectResult = true }, new { Type = ImageTypes.RgbPixel, ExpectResult = true }, new { Type = ImageTypes.RgbAlphaPixel, ExpectResult = true }, new { Type = ImageTypes.UInt8, ExpectResult = true }, new { Type = ImageTypes.UInt16, ExpectResult = true }, new { Type = ImageTypes.UInt32, ExpectResult = true }, new { Type = ImageTypes.Int8, ExpectResult = true }, new { Type = ImageTypes.Int16, ExpectResult = true }, new { Type = ImageTypes.Int32, ExpectResult = true }, new { Type = ImageTypes.HsiPixel, ExpectResult = false }, new { Type = ImageTypes.LabPixel, ExpectResult = false }, new { Type = ImageTypes.Float, ExpectResult = false }, new { Type = ImageTypes.Double, ExpectResult = false } }; var type = this.GetType().Name; foreach (var test in tests) { Array2DBase inImg = null; try { inImg = DlibTest.LoadImageHelp(test.Type, path); var rects = Dlib.FindCandidateObjectLocations(inImg)?.ToArray(); if (rects == null || !rects.Any()) { Assert.True(false, $"{nameof(FindCandidateObjectLocations)} should detect any rectangles."); } switch (test.Type) { case ImageTypes.RgbPixel: foreach (var r in rects) { Dlib.DrawRectangle((Array2D <RgbPixel>)inImg, r, new RgbPixel { Blue = 255 }); } break; case ImageTypes.RgbAlphaPixel: foreach (var r in rects) { Dlib.DrawRectangle((Array2D <RgbAlphaPixel>)inImg, r, new RgbAlphaPixel { Blue = 255, Alpha = 255 }); } break; case ImageTypes.UInt8: foreach (var r in rects) { Dlib.DrawRectangle((Array2D <byte>)inImg, r, 0); } break; case ImageTypes.UInt16: foreach (var r in rects) { Dlib.DrawRectangle((Array2D <ushort>)inImg, r, 0); } break; case ImageTypes.UInt32: foreach (var r in rects) { Dlib.DrawRectangle((Array2D <uint>)inImg, r, 255 << 16); } break; case ImageTypes.Int8: foreach (var r in rects) { Dlib.DrawRectangle((Array2D <sbyte>)inImg, r, 0); } break; case ImageTypes.Int16: foreach (var r in rects) { Dlib.DrawRectangle((Array2D <short>)inImg, r, 0); } break; case ImageTypes.Int32: foreach (var r in rects) { Dlib.DrawRectangle((Array2D <int>)inImg, r, 255 << 16); } break; case ImageTypes.HsiPixel: foreach (var r in rects) { Dlib.DrawRectangle((Array2D <HsiPixel>)inImg, r, new HsiPixel { H = 255 }); } break; case ImageTypes.LabPixel: foreach (var r in rects) { Dlib.DrawRectangle((Array2D <LabPixel>)inImg, r, new LabPixel { L = 255 }); } break; case ImageTypes.Float: foreach (var r in rects) { Dlib.DrawRectangle((Array2D <float>)inImg, r, 0f); } break; case ImageTypes.Double: foreach (var r in rects) { Dlib.DrawRectangle((Array2D <double>)inImg, r, 0d); } break; } Dlib.SaveBmp(inImg, $"{Path.Combine(this.GetOutDir(type, "FindCandidateObjectLocations"), $"Lenna_{test.Type}.bmp")}"); } catch (Exception e) { if (!test.ExpectResult) { Console.WriteLine("OK"); } else { Console.WriteLine(e.StackTrace); Console.WriteLine($"Failed to execute FindCandidateObjectLocations to Type: {test.Type}."); throw; } } finally { if (inImg != null) { this.DisposeAndCheckDisposedState(inImg); } } } }
public void Jet2() { var path = this.GetDataFile($"{LoadTarget}.bmp"); var tests = new[] { new { Type = ImageTypes.BgrPixel, ExpectResult = false, Max = 255, Min = 0 }, new { Type = ImageTypes.RgbPixel, ExpectResult = false, Max = 255, Min = 0 }, new { Type = ImageTypes.RgbAlphaPixel, ExpectResult = false, Max = 255, Min = 0 }, new { Type = ImageTypes.UInt8, ExpectResult = true, Max = 255, Min = 0 }, new { Type = ImageTypes.UInt16, ExpectResult = true, Max = 255, Min = 0 }, new { Type = ImageTypes.UInt32, ExpectResult = true, Max = 255, Min = 0 }, new { Type = ImageTypes.Int8, ExpectResult = true, Max = 255, Min = 0 }, new { Type = ImageTypes.Int16, ExpectResult = true, Max = 255, Min = 0 }, new { Type = ImageTypes.Int32, ExpectResult = true, Max = 255, Min = 0 }, new { Type = ImageTypes.HsiPixel, ExpectResult = false, Max = 255, Min = 0 }, new { Type = ImageTypes.Float, ExpectResult = true, Max = 255, Min = 0 }, new { Type = ImageTypes.Double, ExpectResult = true, Max = 255, Min = 0 }, new { Type = ImageTypes.BgrPixel, ExpectResult = false, Max = 75, Min = 50 }, new { Type = ImageTypes.RgbPixel, ExpectResult = false, Max = 75, Min = 50 }, new { Type = ImageTypes.RgbAlphaPixel, ExpectResult = false, Max = 75, Min = 50 }, new { Type = ImageTypes.UInt8, ExpectResult = true, Max = 75, Min = 50 }, new { Type = ImageTypes.UInt16, ExpectResult = true, Max = 75, Min = 50 }, new { Type = ImageTypes.UInt32, ExpectResult = true, Max = 75, Min = 50 }, new { Type = ImageTypes.Int8, ExpectResult = true, Max = 75, Min = 50 }, new { Type = ImageTypes.Int16, ExpectResult = true, Max = 75, Min = 50 }, new { Type = ImageTypes.Int32, ExpectResult = true, Max = 75, Min = 50 }, new { Type = ImageTypes.HsiPixel, ExpectResult = false, Max = 75, Min = 50 }, new { Type = ImageTypes.Float, ExpectResult = true, Max = 75, Min = 50 }, new { Type = ImageTypes.Double, ExpectResult = true, Max = 75, Min = 50 } }; var type = this.GetType().Name; foreach (var test in tests) { Array2DBase imageObj = null; Array2DBase horzObj = null; Array2DBase vertObj = null; Array2DBase outputImageObj = null; MatrixOp matrix = null; DlibObject windowObj = null; try { const ImageTypes inputType = ImageTypes.Float; var image = DlibTest.LoadImageHelp(test.Type, path); imageObj = image; var horz = Array2DTest.CreateArray2DHelp(inputType); horzObj = horz; var vert = Array2DTest.CreateArray2DHelp(inputType); vertObj = vert; var outputImage = Array2DTest.CreateArray2DHelp(test.Type); outputImageObj = outputImage; Dlib.SobelEdgeDetector(image, horz, vert); Dlib.SuppressNonMaximumEdges(horz, vert, outputImage); try { matrix = JetHelp(test.Type, outputImage, test.Max, test.Min); if (test.ExpectResult) { if (this.CanGuiDebug) { var window = new ImageWindow(matrix, $"{test.Type} - Max: {test.Max}, Min : {test.Min}"); windowObj = window; } Dlib.SaveBmp(image, $"{Path.Combine(this.GetOutDir(type, "Jet2"), $"{LoadTarget}_{test.Type}_{test.Max}_{test.Min}.bmp")}"); } else { Assert.True(false, $"Failed to execute Jet2 to Type: {test.Type}"); } } catch (Exception) { if (!test.ExpectResult) { Console.WriteLine("OK"); } else { throw; } } } catch (Exception e) { Console.WriteLine(e.StackTrace); Console.WriteLine($"Failed to execute Jet2 to Type: {test.Type}"); throw; } finally { if (outputImageObj != null) { this.DisposeAndCheckDisposedState(outputImageObj); } if (vertObj != null) { this.DisposeAndCheckDisposedState(vertObj); } if (horzObj != null) { this.DisposeAndCheckDisposedState(horzObj); } if (windowObj != null) { this.DisposeAndCheckDisposedState(windowObj); } if (matrix != null) { this.DisposeAndCheckDisposedState(matrix); } if (imageObj != null) { this.DisposeAndCheckDisposedState(imageObj); } } } }