Example #1
0
        public void FlipImageLeftRight()
        {
            const string testName = "FlipImageLeftRight";
            var          path     = this.GetDataFile($"{LoadTarget}.bmp");

            var tests = new[]
            {
                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 input in tests)
            {
                {
                    var expectResult = input.ExpectResult;
                    var imageObj     = DlibTest.LoadImage(input.Type, path);

                    var outputImageAction = new Func <bool, Array2DBase>(expect =>
                    {
                        Dlib.FlipImageLeftRight(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.Fail($"{testName} should throw excption 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 StartTrack()
        {
            const string testName = "StartTrack";
            var          path     = this.GetDataFile($"{LoadTarget}.bmp");

            var tests = new[]
            {
                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.HsiPixel, ExpectResult = true },
                new { Type = ImageTypes.Float, ExpectResult = true },
                new { Type = ImageTypes.Double, ExpectResult = true }
            };

            foreach (var input in tests)
            {
                var expectResult = input.ExpectResult;
                var inputType    = input.Type;
                var imageObj     = DlibTest.LoadImage(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 =>
                {
                });

                var failAction = new Action(() =>
                {
                    Assert.Fail($"{testName} should throw excption for InputType: {inputType}.");
                });

                var finallyAction = new Action(() =>
                {
                    this.DisposeAndCheckDisposedState(rect);
                    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 DetectFace2()
        {
            if (this._FrontalFaceDetector == null)
            {
                Assert.Fail("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.LoadImage(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.Fail($"{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);
            }
        }
Example #4
0
        public void AssignImage()
        {
            var path = this.GetDataFile($"{LoadTarget}.bmp");

            var tests = new[]
            {
                new { Type = ImageTypes.RgbAlphaPixel, ExpectResult = true },
                new { Type = ImageTypes.RgbPixel, 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 input in tests)
            {
                foreach (var output in tests)
                {
                    TwoDimentionObjectBase outObj = null;
                    TwoDimentionObjectBase inObj  = null;

                    var expect = input.ExpectResult && output.ExpectResult;

                    try
                    {
                        var inIage = DlibTest.LoadImage(input.Type, path);
                        inObj = inIage;

                        try
                        {
                            var outImage = Array2DTest.CreateArray2D(output.Type);
                            outObj = outImage;

                            Dlib.AssignImage(inIage, outImage);

                            if (!expect)
                            {
                                Assert.Fail($"AssignImage should throw excption for InputType: {input.Type}, OutputType: {output.Type}");
                            }
                            else
                            {
                                Assert.AreEqual(inIage.Columns, outImage.Columns);
                                Assert.AreEqual(inIage.Rows, outImage.Rows);

                                Dlib.SaveBmp(outImage, $"{Path.Combine(this.GetOutDir(type, "AssignImage"), $"{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 AssignImage to InputType: {input.Type}, OutputType: {output.Type}");
                        throw;
                    }
                    finally
                    {
                        if (outObj != null)
                        {
                            this.DisposeAndCheckDisposedState(outObj);
                        }
                        if (inObj != null)
                        {
                            this.DisposeAndCheckDisposedState(inObj);
                        }
                    }
                }
            }
        }
Example #5
0
        public void DetectFace2()
        {
            if (this._ShapePredictor == null)
            {
                Assert.Fail("ShapePredictor is not initialized!!");
            }

            const string testName = "DetectFace2";
            var          path     = this.GetDataFile("Lenna_mini.bmp");
            var          tests    = new[]
            {
                new { Type = ImageTypes.RgbPixel, ExpectResult = true },
                new { Type = ImageTypes.UInt8, ExpectResult = true },
                new { Type = ImageTypes.UInt16, 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 }
            };

            using (var faceDetector = FrontalFaceDetector.GetFrontalFaceDetector())
                foreach (var input in tests)
                {
                    var         expectResult = input.ExpectResult;
                    var         imageObj     = DlibTest.LoadImage(input.Type, path);
                    Rectangle[] dets         = null;

                    var outputImageAction = new Func <bool, Array2DBase>(expect =>
                    {
                        dets = faceDetector.Detect(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.Fail($"{testName} should throw excption 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 TryTrack()
        {
            const string testName = "TryTrack";

            var tests = new[]
            {
                new { Type = ImageTypes.Int32, 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.LoadImage(input.Type, file);

                        if (index == 0)
                        {
                            using (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);
                            }
                            using (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.Fail($"{testName} should throw excption 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);
                }
            }
        }
Example #7
0
        public void Jet2()
        {
            var path = this.GetDataFile($"{LoadTarget}.bmp");

            var tests = new[]
            {
                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.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.LoadImage(test.Type, path);
                    imageObj = image;
                    var horz = Array2DTest.CreateArray2D(inputType);
                    horzObj = horz;
                    var vert = Array2DTest.CreateArray2D(inputType);
                    vertObj = vert;
                    var outputImage = Array2DTest.CreateArray2D(test.Type);
                    outputImageObj = outputImage;

                    Dlib.SobelEdgeDetector(image, horz, vert);
                    Dlib.SuppressNonMaximumEdges(horz, vert, outputImage);

                    try
                    {
                        matrix = Jet(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.Fail($"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);
                    }
                }
            }
        }
        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.CreateArray2D(test.Type);
                var inIage   = DlibTest.LoadImage(test.Type, path);

                try
                {
                    Dlib.GaussianBlur(inIage, outImage, test.Sigma, test.MaxSize);

                    if (!test.ExpectResult)
                    {
                        Assert.Fail($"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 SuppressNonMaximumEdges()
        {
            var path = this.GetDataFile($"{LoadTarget}.bmp");

            var tests = new[]
            {
                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.LoadImage(inputType, path);
                        inputObj = inputImage;
                        var horz = Array2DTest.CreateArray2D(inputType);
                        horzObj = horz;
                        var vert = Array2DTest.CreateArray2D(inputType);
                        vertObj = vert;
                        var outputImage = Array2DTest.CreateArray2D(test.Type);
                        outputObj = outputImage;

                        Dlib.SobelEdgeDetector(inputImage, horz, vert);

                        try
                        {
                            Dlib.SuppressNonMaximumEdges(horz, vert, outputImage);

                            if (!test.ExpectResult)
                            {
                                Assert.Fail($"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);
                        }
                    }
                }
            }
        }
Example #10
0
        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.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.LoadImage(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;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }

                        return(outputObj);
                    });

                    var successAction = new Action <Matrix <double> >(image =>
                    {
                    });

                    var failAction = new Action(() =>
                    {
                        Assert.Fail($"{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 FindCandidateObjectLocations()
        {
            var path = this.GetDataFile("Lenna.jpg");

            var tests = new[]
            {
                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.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.LoadImage(test.Type, path);
                    var rects = Dlib.FindCandidateObjectLocations(inImg)?.ToArray();
                    if (rects == null || !rects.Any())
                    {
                        Assert.Fail($"{nameof(FindCandidateObjectLocations)} should detect any rectangles.");
                    }

                    switch (test.Type)
                    {
                    case ImageTypes.RgbPixel:
                        foreach (var r in rects)
                        {
                            Dlib.DrawRectangle(inImg, r, new RgbPixel {
                                Blue = 255
                            });
                        }
                        break;

                    case ImageTypes.RgbAlphaPixel:
                        foreach (var r in rects)
                        {
                            Dlib.DrawRectangle(inImg, r, new RgbAlphaPixel {
                                Blue = 255, Alpha = 255
                            });
                        }
                        break;

                    case ImageTypes.UInt8:
                        foreach (var r in rects)
                        {
                            Dlib.DrawRectangle(inImg, r, (byte)0);
                        }
                        break;

                    case ImageTypes.UInt16:
                        foreach (var r in rects)
                        {
                            Dlib.DrawRectangle(inImg, r, (ushort)0);
                        }
                        break;

                    case ImageTypes.Int32:
                        foreach (var r in rects)
                        {
                            Dlib.DrawRectangle(inImg, r, 255 << 16);
                        }
                        break;

                    case ImageTypes.HsiPixel:
                        foreach (var r in rects)
                        {
                            Dlib.DrawRectangle(inImg, r, new HsiPixel {
                                H = 255
                            });
                        }
                        break;

                    case ImageTypes.Float:
                        foreach (var r in rects)
                        {
                            Dlib.DrawRectangle(inImg, r, 0f);
                        }
                        break;

                    case ImageTypes.Double:
                        foreach (var r in rects)
                        {
                            Dlib.DrawRectangle(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 EqualizeHistogram2()
        {
            const string testName = nameof(EqualizeHistogram2);
            var          path     = this.GetDataFile($"{LoadTarget}.bmp");

            var inTests = 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 outTests = 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 = true },
                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 inTests)
            {
                foreach (var output in outTests)
                {
                    var expectResult = input.ExpectResult && output.ExpectResult;
                    var imageObj     = DlibTest.LoadImage(input.Type, path);

                    var outputImageAction = new Func <bool, Array2DBase>(expect =>
                    {
                        switch (output.Type)
                        {
                        case ImageTypes.BgrPixel:
                            {
                                Dlib.EqualizeHistogram <BgrPixel>(imageObj, out var ret);
                                return(ret);
                            }

                        case ImageTypes.RgbPixel:
                            {
                                Dlib.EqualizeHistogram <RgbPixel>(imageObj, out var ret);
                                return(ret);
                            }

                        case ImageTypes.RgbAlphaPixel:
                            {
                                Dlib.EqualizeHistogram <RgbAlphaPixel>(imageObj, out var ret);
                                return(ret);
                            }

                        case ImageTypes.UInt8:
                            {
                                Dlib.EqualizeHistogram <byte>(imageObj, out var ret);
                                return(ret);
                            }

                        case ImageTypes.UInt16:
                            {
                                Dlib.EqualizeHistogram <ushort>(imageObj, out var ret);
                                return(ret);
                            }

                        case ImageTypes.UInt32:
                            {
                                Dlib.EqualizeHistogram <uint>(imageObj, out var ret);
                                return(ret);
                            }

                        case ImageTypes.Int8:
                            {
                                Dlib.EqualizeHistogram <sbyte>(imageObj, out var ret);
                                return(ret);
                            }

                        case ImageTypes.Int16:
                            {
                                Dlib.EqualizeHistogram <short>(imageObj, out var ret);
                                return(ret);
                            }

                        case ImageTypes.Int32:
                            {
                                Dlib.EqualizeHistogram <int>(imageObj, out var ret);
                                return(ret);
                            }

                        case ImageTypes.HsiPixel:
                            {
                                Dlib.EqualizeHistogram <HsiPixel>(imageObj, out var ret);
                                return(ret);
                            }

                        case ImageTypes.Float:
                            {
                                Dlib.EqualizeHistogram <float>(imageObj, out var ret);
                                return(ret);
                            }

                        case ImageTypes.Double:
                            {
                                Dlib.EqualizeHistogram <double>(imageObj, out var ret);
                                return(ret);
                            }

                        default:
                            throw new ArgumentOutOfRangeException();
                        }
                    });

                    var successAction = new Action <Array2DBase>(image =>
                    {
                        Dlib.SaveBmp(image, $"{Path.Combine(this.GetOutDir(type, testName), $"{LoadTarget}_{input.Type}_{output.Type}.bmp")}");
                    });

                    var failAction = new Action(() =>
                    {
                        Assert.Fail($"{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 FindCandidateObjectLocations()
        {
            var path = this.GetDataFile("Lenna.jpg");

            var tests = new[]
            {
                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 = 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;
                IEnumerable <Rectangle> rects = null;

                try
                {
                    inImg = DlibTest.LoadImage(test.Type, path);
                    rects = Dlib.FindCandidateObjectLocations(inImg);
                    if (rects == null || !rects.Any())
                    {
                        Assert.Fail($"FindCandidateObjectLocations should detect any rectangles.");
                    }

#if DEBUG
                    using (var bmp = Image.FromFile(path.FullName))
                        using (var g = Graphics.FromImage(bmp))
                            using (var p = new Pen(Color.Blue, 1f))
                            {
                                foreach (var r in rects)
                                {
                                    g.DrawRectangle(p, r.Left, r.Top, r.Width, r.Height);
                                }

                                bmp.Save($"{Path.Combine(this.GetOutDir(type, "FindCandidateObjectLocations"), $"Lenna_{test.Type}.bmp")}");
                            }
#endif
                }
                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 (rects != null)
                    {
                        this.DisposeAndCheckDisposedState(rects);
                    }
                    if (inImg != null)
                    {
                        this.DisposeAndCheckDisposedState(inImg);
                    }
                }
            }
        }
        public void ExtractImageChip()
        {
            const string testName = nameof(ExtractImageChip);
            var          path     = this.GetDataFile($"{LoadTarget}.bmp");

            var tests = new[]
            {
                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 = 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 }
            };

            var type = this.GetType().Name;

            using (var dims = new ChipDims(227, 227))
                using (var chip = new ChipDetails(new Rectangle(0, 0, 100, 100), dims))
                    foreach (var input in tests)
                    {
                        var expectResult = input.ExpectResult;
                        var imageObj     = DlibTest.LoadImage(input.Type, path);

                        var outputImageAction = new Func <bool, Array2DBase>(expect =>
                        {
                            switch (input.Type)
                            {
                            case ImageTypes.RgbPixel:
                                return(Dlib.ExtractImageChip <RgbPixel>(imageObj, chip));

                            case ImageTypes.RgbAlphaPixel:
                                return(Dlib.ExtractImageChip <RgbAlphaPixel>(imageObj, chip));

                            case ImageTypes.UInt8:
                                return(Dlib.ExtractImageChip <byte>(imageObj, chip));

                            case ImageTypes.UInt16:
                                return(Dlib.ExtractImageChip <ushort>(imageObj, chip));

                            case ImageTypes.UInt32:
                                return(Dlib.ExtractImageChip <uint>(imageObj, chip));

                            case ImageTypes.Int8:
                                return(Dlib.ExtractImageChip <sbyte>(imageObj, chip));

                            case ImageTypes.Int16:
                                return(Dlib.ExtractImageChip <short>(imageObj, chip));

                            case ImageTypes.Int32:
                                return(Dlib.ExtractImageChip <int>(imageObj, chip));

                            case ImageTypes.HsiPixel:
                                return(Dlib.ExtractImageChip <HsiPixel>(imageObj, chip));

                            case ImageTypes.Float:
                                return(Dlib.ExtractImageChip <float>(imageObj, chip));

                            case ImageTypes.Double:
                                return(Dlib.ExtractImageChip <double>(imageObj, chip));

                            default:
                                throw new ArgumentOutOfRangeException();
                            }
                        });

                        var successAction = new Action <Array2DBase>(image =>
                        {
                            Dlib.SaveBmp(image, $"{Path.Combine(this.GetOutDir(type, testName), $"{LoadTarget}_{input.Type}.bmp")}");
                        });

                        var failAction = new Action(() =>
                        {
                            Assert.Fail($"{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);
                    }
        }
Example #15
0
        public void RotateImage2()
        {
            const string testName = "RotateImage2";
            var          path     = this.GetDataFile($"{LoadTarget}.bmp");

            var tests = new[]
            {
                new { Type = ImageTypes.RgbPixel, ExpectResult = true },
                new { Type = ImageTypes.UInt8, ExpectResult = true },
                new { Type = ImageTypes.UInt16, ExpectResult = true },
                new { Type = ImageTypes.Float, ExpectResult = true },
                new { Type = ImageTypes.Double, ExpectResult = true }
            };

            var type = this.GetType().Name;

            foreach (InterpolationTypes itype in Enum.GetValues(typeof(InterpolationTypes)))
            {
                foreach (var angle in new[] { 30, 60, 90 })
                {
                    foreach (var input in tests)
                    {
                        var expectResult = input.ExpectResult;
                        var imageObj     = DlibTest.LoadImage(input.Type, path);
                        var outputObj    = Array2DTest.CreateArray2D(input.Type);

                        var outputImageAction = new Func <bool, Array2DBase>(expect =>
                        {
                            Dlib.RotateImage(imageObj, outputObj, angle, itype);
                            return(outputObj);
                        });

                        var successAction = new Action <Array2DBase>(image =>
                        {
                            Dlib.SaveJpeg(outputObj, $"{Path.Combine(this.GetOutDir(type, testName), $"{LoadTarget}_{input.Type}_{input.Type}_{angle}_{itype}.jpg")}");
                        });

                        var failAction = new Action(() =>
                        {
                            Console.WriteLine($"Failed to execute {testName} to InputType: {input.Type}, OutputType: {input.Type}, Angle: {angle}, InterpolationType: {itype}.");
                        });

                        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: {input.Type}, OutputType: {input.Type}, Angle: {angle}, InterpolationType: {itype}.");
                        });

                        DoTest(outputImageAction, expectResult, successAction, finallyAction, failAction, exceptionAction);
                    }
                }
            }
        }
        public void SobelEdgeDetector()
        {
            var path = this.GetDataFile($"{LoadTarget}.bmp");

            var tests = new[]
            {
                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.LoadImage(inputType, path);
                        imageObj = image;
                        var horz = Array2DTest.CreateArray2D(test.Type);
                        horzObj = horz;
                        var vert = Array2DTest.CreateArray2D(test.Type);
                        vertObj = vert;

                        try
                        {
                            Dlib.SobelEdgeDetector(image, horz, vert);

                            if (!test.ExpectResult)
                            {
                                Assert.Fail($"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);
                        }
                    }
                }
            }
        }
Example #17
0
        public void TransformImagePointTransformProjective()
        {
            const string testName = "TransformImagePointTransformProjective";
            var          path     = this.GetDataFile($"{LoadTarget}.bmp");

            var tests = new[]
            {
                new { Type = ImageTypes.HsiPixel, ExpectResult = false },
                new { Type = ImageTypes.RgbAlphaPixel, ExpectResult = false },
                new { Type = ImageTypes.RgbPixel, ExpectResult = true },
                new { Type = ImageTypes.UInt8, ExpectResult = true },
                new { Type = ImageTypes.UInt16, ExpectResult = true },
                new { Type = ImageTypes.Float, ExpectResult = true },
                new { Type = ImageTypes.Double, ExpectResult = true }
            };

            var angleCases = new[]
            {
                //45d, -45d, 90d, 180d
                0d
            };

            var xCases = new[]
            {
                //10, -10, 50,5, -50,5
                0d
            };

            var yCases = new[]
            {
                //10, -10, 50,5, -50,5
                0d
            };

            var type = this.GetType().Name;

            foreach (var angle in angleCases)
            {
                foreach (var x in xCases)
                {
                    foreach (var y in yCases)
                    {
                        foreach (var input in tests)
                        {
                            foreach (var output in tests)
                            {
                                var expectResult = input.ExpectResult && output.ExpectResult;
                                var imageObj     = DlibTest.LoadImage(input.Type, path);
                                var outputObj    = Array2DTest.CreateArray2D(output.Type, imageObj.Rows, imageObj.Columns);
                                var matrix       = new Matrix <double>(3, 3);
                                var transform    = new PointTransformProjective(matrix);

                                var outputImageAction = new Func <bool, Array2DBase>(expect =>
                                {
                                    Dlib.TransformImage(imageObj, outputObj, transform);
                                    return(outputObj);
                                });

                                var successAction = new Action <Array2DBase>(image =>
                                {
                                    Dlib.SaveJpeg(image, $"{Path.Combine(this.GetOutDir(type, testName), $"{LoadTarget}_{input.Type}_{output.Type}_{angle}_{x}_{y}.jpg")}");
                                });

                                var failAction = new Action(() =>
                                {
                                    Assert.Fail($"{testName} should throw excption for InputType: {input.Type}, OutputType: {output.Type}, Angle,: {angle}, X: {x}, Y: {y}.");
                                });

                                var finallyAction = new Action(() =>
                                {
                                    this.DisposeAndCheckDisposedState(matrix);
                                    this.DisposeAndCheckDisposedState(transform);
                                    this.DisposeAndCheckDisposedState(imageObj);
                                    if (outputObj != null)
                                    {
                                        this.DisposeAndCheckDisposedState(outputObj);
                                    }
                                });

                                var exceptionAction = new Action(() =>
                                {
                                    Console.WriteLine($"Failed to execute {testName} to InputType: {input.Type}, OutputType: {output.Type}, Angle,: {angle}, X: {x}, Y: {y}.");
                                });

                                DoTest(outputImageAction, expectResult, successAction, finallyAction, failAction, exceptionAction);
                            }
                        }
                    }
                }
            }
        }
        public void TryTrack()
        {
            const string testName = "TryTrack";

            var tests = new[]
            {
                new { Type = ImageTypes.UInt8, 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.LoadImage(input.Type, file);

                        if (index == 0)
                        {
                            using (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);
                            }
                            using (var r = tracker.GetPosition())
                            {
#if DEBUG
                                using (var tmp = Image.FromFile(file.FullName))
                                    using (var bmp = new Bitmap(tmp))
                                        using (var g = Graphics.FromImage(bmp))
                                            using (var p = new Pen(Color.Red, 3f))
                                            {
                                                g.DrawRectangle(p, (float)r.Left, (float)r.Top, (float)r.Width, (float)r.Height);
                                                bmp.Save(Path.Combine(this.GetOutDir(type), $"{Path.GetFileNameWithoutExtension(file.FullName)}.jpg"));
                                            }
#endif
                            }
                        });

                        var failAction = new Action(() =>
                        {
                            Assert.Fail($"{testName} should throw excption 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);
                }
            }
        }