예제 #1
0
        public void GetRowColumn()
        {
            const int width  = 150;
            const int height = 100;

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

            foreach (var test in tests)
            {
                var array2D = CreateArray2D(test.Type, height, width);
                switch (array2D.ImageType)
                {
                case ImageTypes.UInt8:
                {
                    var array = (Array2D <byte>)array2D;
                    Dlib.AssignAllPpixels(array, 255);
                    using (var row = array[0])
                        Assert.AreEqual(row[0], 255, "Array<byte> failed");
                }
                break;

                case ImageTypes.UInt16:
                {
                    var array = (Array2D <ushort>)array2D;
                    Dlib.AssignAllPpixels(array, 255);
                    using (var row = array[0])
                        Assert.AreEqual(row[0], 255, "Array<ushort> failed");
                }
                break;

                case ImageTypes.UInt32:
                {
                    var array = (Array2D <uint>)array2D;
                    Dlib.AssignAllPpixels(array, 255);
                    using (var row = array[0])
                        Assert.AreEqual(row[0], 255u, "Array<uint> failed");
                }
                break;

                case ImageTypes.Int8:
                {
                    var array = (Array2D <sbyte>)array2D;
                    Dlib.AssignAllPpixels(array, 127);
                    using (var row = array[0])
                        Assert.AreEqual(row[0], 127, "Array<sbyte> failed");
                }
                break;

                case ImageTypes.Int16:
                {
                    var array = (Array2D <short>)array2D;
                    Dlib.AssignAllPpixels(array, 255);
                    using (var row = array[0])
                        Assert.AreEqual(row[0], 255, "Array<short> failed");
                }
                break;

                case ImageTypes.Int32:
                {
                    var array = (Array2D <int>)array2D;
                    Dlib.AssignAllPpixels(array, 255);
                    using (var row = array[0])
                        Assert.AreEqual(row[0], 255, "Array<int> failed");
                }
                break;

                case ImageTypes.Float:
                {
                    var array = (Array2D <float>)array2D;
                    Dlib.AssignAllPpixels(array, 255);
                    using (var row = array[0])
                        Assert.AreEqual(row[0], 255, "Array<float> failed");
                }
                break;

                case ImageTypes.Double:
                {
                    var array = (Array2D <double>)array2D;
                    Dlib.AssignAllPpixels(array, 255);
                    using (var row = array[0])
                        Assert.AreEqual(row[0], 255, "Array<double> failed");
                }
                break;

                case ImageTypes.RgbPixel:
                {
                    var array = (Array2D <RgbPixel>)array2D;
                    var pixel = new RgbPixel
                    {
                        Red   = 255,
                        Blue  = 255,
                        Green = 255
                    };

                    Dlib.AssignAllPpixels(array, pixel);
                    using (var row = array[0])
                    {
                        var t = row[0];
                        Assert.AreEqual(t.Red, 255, "Array<RgbPixel> failed");
                        Assert.AreEqual(t.Blue, 255, "Array<RgbPixel> failed");
                        Assert.AreEqual(t.Green, 255, "Array<RgbPixel> failed");
                    }
                }
                break;

                case ImageTypes.RgbAlphaPixel:
                {
                    var array = (Array2D <RgbAlphaPixel>)array2D;
                    var pixel = new RgbAlphaPixel
                    {
                        Red   = 255,
                        Blue  = 255,
                        Green = 255,
                        Alpha = 255
                    };

                    Dlib.AssignAllPpixels(array, pixel);
                    using (var row = array[0])
                    {
                        var t = row[0];
                        Assert.AreEqual(t.Red, 255, "Array<RgbAlphaPixel> failed");
                        Assert.AreEqual(t.Blue, 255, "Array<RgbAlphaPixel> failed");
                        Assert.AreEqual(t.Green, 255, "Array<RgbAlphaPixel> failed");
                        Assert.AreEqual(t.Alpha, 255, "Array<RgbAlphaPixel> failed");
                    }
                }
                break;

                case ImageTypes.HsiPixel:
                {
                    var array = (Array2D <HsiPixel>)array2D;
                    var pixel = new HsiPixel
                    {
                        H = 255,
                        S = 255,
                        I = 255
                    };

                    Dlib.AssignAllPpixels(array, pixel);
                    using (var row = array[0])
                    {
                        var t = row[0];
                        Assert.AreEqual(t.H, 255, "Array<HsiPixel> failed");
                        Assert.AreEqual(t.S, 255, "Array<HsiPixel> failed");
                        Assert.AreEqual(t.I, 255, "Array<HsiPixel> failed");
                    }
                }
                break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(array2D.ImageType), array2D.ImageType, null);
                }
                this.DisposeAndCheckDisposedState(array2D);
            }
        }
예제 #2
0
        private static void Main()
        {
            using (var img = new Array2D <byte>(400, 400))
                using (var ht = new DlibDotNet.HoughTransform(300))
                    using (var win = new ImageWindow())
                        using (var win2 = new ImageWindow())
                        {
                            var angle1 = 0d;
                            var angle2 = 0d;

                            while (true)
                            {
                                angle1 += Math.PI / 130;
                                angle2 += Math.PI / 400;

                                using (var rect = img.Rect)
                                    using (var cent = rect.Center)
                                        using (var point1 = new Point(90, 0))
                                            using (var tmp1 = cent + point1)
                                                using (var arc = Point.Rotate(cent, tmp1, angle1 * 180 / Math.PI))
                                                    using (var point2 = new Point(500, 0))
                                                        using (var tmp2 = arc + point2)
                                                            using (var tmp3 = arc - point2)
                                                                using (var l = Point.Rotate(arc, tmp2, angle2 * 180 / Math.PI))
                                                                    using (var r = Point.Rotate(arc, tmp3, angle2 * 180 / Math.PI))
                                                                    {
                                                                        Dlib.AssignAllPpixels(img, 0);
                                                                        Dlib.DrawLine(img, l, r, 255);

                                                                        using (var offset = new Point(50, 50))
                                                                            using (var himg = new Array2D <int>())
                                                                                using (var hrect = Dlib.GetRect(ht))
                                                                                    using (var box = Rectangle.Translate(hrect, offset))
                                                                                    {
                                                                                        // Now let's compute the hough transform for a subwindow in the image.  In
                                                                                        // particular, we run it on the 300x300 subwindow with an upper left corner at the
                                                                                        // pixel point(50,50).  The output is stored in himg.
                                                                                        ht.Operator(img, box, himg);

                                                                                        // Now that we have the transformed image, the Hough image pixel with the largest
                                                                                        // value should indicate where the line is.  So we find the coordinates of the
                                                                                        // largest pixel:
                                                                                        using (var mat = Dlib.Mat(himg))
                                                                                            using (var p = Dlib.MaxPoint(mat))
                                                                                            {
                                                                                                // And then ask the ht object for the line segment in the original image that
                                                                                                // corresponds to this point in Hough transform space.
                                                                                                using (var line = ht.GetLine(p))
                                                                                                {
                                                                                                    // Finally, let's display all these things on the screen.  We copy the original
                                                                                                    // input image into a color image and then draw the detected line on top in red.
                                                                                                    using (var temp = new Array2D <RgbPixel>())
                                                                                                    {
                                                                                                        Dlib.AssignImage(img, temp);

                                                                                                        using (var p1 = line.First + offset)
                                                                                                            using (var p2 = line.Second + offset)
                                                                                                            {
                                                                                                                Dlib.DrawLine(temp, p1, p2, new RgbPixel
                                                                                                                {
                                                                                                                    Red = 255
                                                                                                                });
                                                                                                                win.ClearOverlay();
                                                                                                                win.SetImage(temp);

                                                                                                                // Also show the subwindow we ran the Hough transform on as a green box.  You will
                                                                                                                // see that the detected line is exactly contained within this box and also
                                                                                                                // overlaps the original line.
                                                                                                                win.AddOverlay(box, new RgbPixel
                                                                                                                {
                                                                                                                    Green = 255
                                                                                                                });

                                                                                                                using (var jet = Dlib.Jet(himg))
                                                                                                                    win2.SetImage(jet);
                                                                                                            }
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                    }
                                                                    }
                            }
                        }
        }