Esempio n. 1
0
        internal static MatrixOp JetHelp(ImageTypes type, TwoDimensionObjectBase obj, double max, double min = 0)
        {
            MatrixOp matrixOp;

            switch (type)
            {
            case ImageTypes.RgbPixel:
                matrixOp = Dlib.Jet(obj as Array2D <RgbPixel>, max, min);
                break;

            case ImageTypes.RgbAlphaPixel:
                matrixOp = Dlib.Jet(obj as Array2D <RgbAlphaPixel>, max, min);
                break;

            case ImageTypes.UInt8:
                matrixOp = Dlib.Jet(obj as Array2D <byte>, max, min);
                break;

            case ImageTypes.UInt16:
                matrixOp = Dlib.Jet(obj as Array2D <ushort>, max, min);
                break;

            case ImageTypes.UInt32:
                matrixOp = Dlib.Jet(obj as Array2D <uint>, max, min);
                break;

            case ImageTypes.Int8:
                matrixOp = Dlib.Jet(obj as Array2D <sbyte>, max, min);
                break;

            case ImageTypes.Int16:
                matrixOp = Dlib.Jet(obj as Array2D <short>, max, min);
                break;

            case ImageTypes.Int32:
                matrixOp = Dlib.Jet(obj as Array2D <int>, max, min);
                break;

            case ImageTypes.HsiPixel:
                matrixOp = Dlib.Jet(obj as Array2D <HsiPixel>, max, min);
                break;

            case ImageTypes.LabPixel:
                matrixOp = Dlib.Jet(obj as Array2D <LabPixel>, max, min);
                break;

            case ImageTypes.Float:
                matrixOp = Dlib.Jet(obj as Array2D <float>, max, min);
                break;

            case ImageTypes.Double:
                matrixOp = Dlib.Jet(obj as Array2D <double>, max, min);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            return(matrixOp);
        }
        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 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.LoadImage(input.Type, path);
                        inObj = inImage;

                        try
                        {
                            var outImage = Array2DTest.CreateArray2D(output.Type, inImage.Rows, inImage.Columns);
                            outObj = outImage;

                            Dlib.SumFilter(inImage, outImage, rect);

                            if (!expect)
                            {
                                Assert.Fail($"{testName} should throw exception for InputType: {input.Type}, OutputType: {output.Type}");
                            }
                            else
                            {
                                Assert.AreEqual(inImage.Columns, outImage.Columns);
                                Assert.AreEqual(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);
                        }
                    }
                }
            }
        }
Esempio n. 4
0
        private void ExecuteMaxCostAssignment(TwoDimensionObjectBase obj)
        {
            if (obj is Matrix <sbyte> sbyteMatrix)
            {
                Dlib.MaxCostAssignment(sbyteMatrix);
                return;
            }

            if (obj is Matrix <short> shortMatrix)
            {
                Dlib.MaxCostAssignment(shortMatrix);
                return;
            }

            if (obj is Matrix <int> intMatrix)
            {
                Dlib.MaxCostAssignment(intMatrix);
                return;
            }

            if (obj is Matrix <byte> byteMatrix)
            {
                Dlib.MaxCostAssignment(byteMatrix);
                return;
            }

            if (obj is Matrix <ushort> ushortMatrix)
            {
                Dlib.MaxCostAssignment(ushortMatrix);
                return;
            }

            if (obj is Matrix <uint> uintMatrix)
            {
                Dlib.MaxCostAssignment(uintMatrix);
                return;
            }

            if (obj is Matrix <float> floatMatrix)
            {
                Dlib.MaxCostAssignment(floatMatrix);
                return;
            }

            if (obj is Matrix <double> doubleMatrix)
            {
                Dlib.MaxCostAssignment(doubleMatrix);
                return;
            }

            if (obj is Matrix <RgbPixel> rgbPixelMatrix)
            {
                Dlib.MaxCostAssignment(rgbPixelMatrix);
                return;
            }

            if (obj is Matrix <RgbAlphaPixel> rgbAlphaPixelMatrix)
            {
                Dlib.MaxCostAssignment(rgbAlphaPixelMatrix);
                return;
            }

            if (obj is Matrix <HsiPixel> hsiPicelMatrix)
            {
                Dlib.MaxCostAssignment(hsiPicelMatrix);
                return;
            }

            throw new NotSupportedException();
        }