Beispiel #1
0
        /// <summary>
        /// Displays the specified image in a window and waits the user to create a rectangle by drawing.
        /// <para>Press and hold shift + mouse (track-pad) to translate and zoom an image.</para>
        /// </summary>
        /// <param name="image">Image to display.</param>
        /// <param name="windowTitle">Window title (ID).</param>
        /// <param name="scaleForm">True to adjust form to the image size, false otherwise.</param>
        /// <returns>Drawn rectangle.</returns>
        public static RectangleF GetRectangle(this Bgr <byte>[,] image, string windowTitle = "Draw rectangle (close when finished)", bool scaleForm = false)
        {
            var rect = FormCollection.CreateAndShowDialog(() =>
            {
                var f       = new DrawingRectangleForm(windowTitle);
                f.ScaleForm = scaleForm;
                f.SetImage(image);

                return(f);
            },
                                                          f => f.Rectangle);

            return(rect);
        }
Beispiel #2
0
        /// <summary>
        /// Displays the specified image in a window and waits the user to create a mask by drawing.
        /// <para>Press and hold shift + mouse (track-pad) to translate and zoom an image.</para>
        /// </summary>
        /// <param name="image">Image to display.</param>
        /// <param name="windowTitle">Window title (ID).</param>
        /// <param name="scaleForm">True to adjust form to the image size, false otherwise.</param>
        /// <returns>Drawn mask.</returns>
        public static Gray <byte>[,] GetMask(this Bgr <byte>[,] image, string windowTitle = "Draw image mask (close when finished)", bool scaleForm = false)
        {
            var mask = FormCollection.CreateAndShowDialog(() =>
            {
                var f       = new DrawingPenForm(windowTitle);
                f.ScaleForm = scaleForm;
                f.SetImage(image);

                return(f);
            },
                                                          f => f.Mask);

            return(mask);
        }
Beispiel #3
0
        /// <summary>
        /// Draws contour.
        /// </summary>
        /// <param name="image">Input image.</param>
        /// <param name="contour">Contour points.</param>
        /// <param name="color">Contour color.</param>
        /// <param name="thickness">Contours thickness.</param>
        /// <param name="opacity">Sets alpha channel where 0 is transparent and 255 is full opaque.</param>
        public unsafe static void Draw(this Bgr <byte>[,] image, Point[] contour, Bgr <byte> color, int thickness, byte opacity = Byte.MaxValue)
        {
            var contourHandle = GCHandle.Alloc(contour, GCHandleType.Pinned);

            using (var img = image.Lock())
            {
                var iplImage = img.AsCvIplImage();

                //TODO - noncritical: implement with cvContour
                CvCoreInvoke.cvPolyLine(&iplImage, new IntPtr[] { contourHandle.AddrOfPinnedObject() }, new int[] { contour.Length }, 1,
                                        true, color.ToCvScalar(), thickness, LineTypes.EightConnected, 0);
            }

            contourHandle.Free();
        }
Beispiel #4
0
        /// <summary>
        /// Draws circles.
        /// </summary>
        /// <param name="image">Input image.</param>
        /// <param name="circles">Circles</param>
        /// <param name="color">Circle color.</param>
        /// <param name="thickness">Contours thickness.</param>
        public unsafe static void Draw(this Bgr <byte>[,] image, IEnumerable <Circle> circles, Bgr <byte> color, int thickness)
        {
            using (var img = image.Lock())
            {
                var iplImage = img.AsCvIplImage();

                foreach (var circle in circles)
                {
                    var center = new Point(circle.X, circle.Y);

                    CvCoreInvoke.cvCircle(&iplImage, center, circle.Radius, color.ToCvScalar(),
                                          thickness, LineTypes.EightConnected, 0);
                }
            }
        }
Beispiel #5
0
        public static void Convert(Hsv <byte> hsv, ref Bgr <byte> bgr)
        {
            if (hsv.S == 0)
            {
                bgr.R = hsv.V;
                bgr.G = hsv.V;
                bgr.B = hsv.V;
                return;
            }

            int hue = hsv.H * 2;      //move to [0-360 range] (only needed for byte!)

            int hQuadrant = hue / 60; // Hue quadrant 0 - 5 (60deg)
            int hOffset   = hue % 60; // Hue position in quadrant
            int vs        = hsv.V * hsv.S;

            byte p = (byte)(hsv.V - (vs / 255));
            byte q = (byte)(hsv.V - (vs / 255 * hOffset) / 60);
            byte t = (byte)(hsv.V - (vs / 255 * (60 - hOffset)) / 60);

            switch (hQuadrant)
            {
            case 0:
                bgr.R = hsv.V; bgr.G = t; bgr.B = p;
                break;

            case 1:
                bgr.R = q; bgr.G = hsv.V; bgr.B = p;
                break;

            case 2:
                bgr.R = p; bgr.G = hsv.V; bgr.B = t;
                break;

            case 3:
                bgr.R = p; bgr.G = q; bgr.B = hsv.V;
                break;

            case 4:
                bgr.R = t; bgr.G = p; bgr.B = hsv.V;
                break;

            default:
                bgr.R = hsv.V; bgr.G = p; bgr.B = q;
                break;
            }
        }
Beispiel #6
0
        /// <summary>
        /// Displays the image and enables the user selects an area.
        /// <para>Press and hold any key to temporary block the calling thread (useful when used with video sequence stream).</para>
        /// </summary>
        /// <param name="image">Image to display.</param>
        /// <param name="windowTitle">Window title (ID).</param>
        /// <param name="onDrawn">Action executed when a rectangle is drawn (when mouse is released).</param>
        /// <param name="scaleForm">True to adjust form to the image size, false otherwise.</param>
        /// <param name="startBlocked">
        /// Used only when a form is first initialized. True to start as a dialog, false to show the form in non-blocking way.
        /// <para>When form is the blocking state (dialog) user is required to press and release a key to enable non-blocking mode.</para>
        /// </param>
        public static void GetRectangle(this Bgr <byte>[,] image, string windowTitle = "Draw rectangle", Action <RectangleF> onDrawn = null, bool scaleForm = false, bool startBlocked = false)
        {
            ManualResetEvent resetEvent = null;

            FormCollection.CreateOrUpdate(
                () => new DrawingRectangleForm(windowTitle, !startBlocked),
                form =>
            {
                form.ScaleForm = scaleForm;
                form.SetImage(image);
                resetEvent   = form.ResetEvent;
                form.OnDrawn = onDrawn;
            },
                windowTitle);

            resetEvent.WaitOne();
        }
Beispiel #7
0
        public static void Convert(Bgr <byte> bgr, ref Hsv <byte> hsv)
        {
            byte rgbMin, rgbMax;

            rgbMin = bgr.R < bgr.G ? (bgr.R < bgr.B ? bgr.R : bgr.B) : (bgr.G < bgr.B ? bgr.G : bgr.B);
            rgbMax = bgr.R > bgr.G ? (bgr.R > bgr.B ? bgr.R : bgr.B) : (bgr.G > bgr.B ? bgr.G : bgr.B);

            hsv.V = rgbMax;
            if (hsv.V == 0)
            {
                hsv.H = 0;
                hsv.S = 0;
                return;
            }

            hsv.S = (byte)(255 * (rgbMax - rgbMin) / rgbMax);
            if (hsv.S == 0)
            {
                hsv.H = 0;
                return;
            }

            int hue = 0;

            if (rgbMax == bgr.R)
            {
                hue = 0 + 60 * (bgr.G - bgr.B) / (rgbMax - rgbMin);
                if (hue < 0)
                {
                    hue += 360;
                }
            }
            else if (rgbMax == bgr.G)
            {
                hue = 120 + 60 * (bgr.B - bgr.R) / (rgbMax - rgbMin);
            }
            else //rgbMax == bgr.B
            {
                hue = 240 + 60 * (bgr.R - bgr.G) / (rgbMax - rgbMin);
            }

            hsv.H = (byte)(hue / 2); //scale [0-360] . [0-180] (only needed for byte!)

            //Debug.Assert(hue >= 0 && hue <= 360);
        }
Beispiel #8
0
        /// <summary>
        /// Sets the specified image.
        /// </summary>
        /// <param name="image">Image to display.</param>
        public void SetImage(Bgr <byte>[,] image)
        {
            if (bmp == null || bmp.Width != image.Width() || bmp.Height != image.Height())
            {
                bmp = new Bitmap(image.Width(), image.Height(), PixelFormat.Format24bppRgb);
            }

            using (BitmapData bmpData = bmp.Lock())
                using (var uIm = image.Lock())
                {
                    Copy.UnsafeCopy2D(uIm.ImageData, bmpData.Data, uIm.Stride, bmpData.ScanWidth, uIm.Height);
                }

            PictureBox.Image = bmp;

            if (ScaleForm)
            {
                ClientSize = new Size(image.Width(), image.Height());
            }
        }
Beispiel #9
0
        /// <summary>
        /// Displays an color picker dialog.
        /// <para>REQUIREMENT: calling thread must be STAThread.</para>
        /// </summary>
        /// <param name="defaultColor">Default color.</param>
        /// <returns>Picked or default color in case the selection is cancelled.</returns>
        public static Bgr <byte> PickColor(Bgr <byte> defaultColor = default(Bgr <byte>))
        {
            Bgr <byte> color = defaultColor;

            using (ColorDialog dialog = new ColorDialog())
            {
                dialog.Color = new Eto.Drawing.Color {
                    Bb = defaultColor.B, Gb = defaultColor.G, Rb = defaultColor.R
                };
                var result = dialog.ShowDialog(null);
                if (result == DialogResult.Ok)
                {
                    color = new Bgr <byte> {
                        B = (byte)dialog.Color.Bb, G = (byte)dialog.Color.Gb, R = (byte)dialog.Color.Rb
                    }
                }
                ;
            }

            return(color);
        }
Beispiel #10
0
        /// <summary>
        /// Draws Box2D.
        /// </summary>
        /// <param name="image">Input image.</param>
        /// <param name="box">Box 2D.</param>
        /// <param name="color">Object's color.</param>
        /// <param name="thickness">Border thickness (-1 to fill the object).</param>
        /// <param name="opacity">Sets alpha channel where 0 is transparent and 255 is full opaque.</param>
        public unsafe static void Draw(this Bgr <byte>[,] image, Box2D box, Bgr <byte> color, int thickness, byte opacity = Byte.MaxValue)
        {
            if (thickness < 1)
            {
                throw new NotSupportedException("Only positive values are valid!");
            }

            var vertices = box.GetVertices();

            draw(image, opacity, cvImg =>
            {
                for (int i = 0; i < vertices.Length; i++)
                {
                    int idx2 = (i + 1) % vertices.Length;

                    CvCoreInvoke.cvLine(&cvImg, vertices[i].Round(), vertices[idx2].Round(),
                                        color.ToCvScalar(), thickness,
                                        LineTypes.EightConnected, 0);
                }
            });
        }
Beispiel #11
0
 public static void Convert(Rgb <byte> rgb, ref Bgr <byte> bgr)
 {
     rgb.R = bgr.R;
     rgb.G = bgr.G;
     rgb.B = bgr.B;
 }
Beispiel #12
0
 /// <summary>
 /// Encodes the specified image into the specified image type byte array.
 /// </summary>
 /// <param name="image">Image to encode.</param>
 /// <param name="extension">Image type extension (.bmp, .png, .jpg)</param>
 /// <returns>Image type byte array.</returns>
 public static byte[] Encode(this Bgr <ushort>[,] image, string extension)
 {
     return(encode(image, extension, null));
 }
Beispiel #13
0
 /// <summary>
 /// Encodes the specified image into the PNG byte array.
 /// </summary>
 /// <param name="image">Image to encode.</param>
 /// <param name="pngCompression">PNG compression level [0..9] where 9 is the highest compression.</param>
 /// <returns>PNG byte array.</returns>
 public static byte[] EncodeAsPng(this Bgr <ushort>[,] image, int pngCompression = 3)
 {
     return(encodeAsPng(image, pngCompression));
 }
Beispiel #14
0
 /// <summary>
 /// Converts the source color to the destination color.
 /// </summary>
 /// <param name="image">Source image.</param>
 /// <param name="area">Working area.</param>
 /// <returns>image with converted color.</returns>
 public static Gray <byte>[,] ToGray(this Bgr <byte>[,] image, Rectangle area)
 {
     return(image.Convert <Bgr <byte>, Gray <byte> >(Bgr <byte> .Convert, area));
 }
Beispiel #15
0
 /// <summary>
 /// Converts 8-bit Bgra to 8-bit Bgr color.
 /// </summary>
 /// <param name="bgra">Source color.</param>
 /// <param name="bgr">Destination color.</param>
 public static void Convert(Bgra <byte> bgra, ref Bgr <byte> bgr)
 {
     bgr.B = bgra.B;
     bgr.G = bgra.G;
     bgr.R = bgra.R;
 }
Beispiel #16
0
 /// <summary>
 /// Gets System.Drawing.Color from Bgr8 color.
 /// </summary>
 /// <param name="color">Color.</param>
 /// <param name="opacity">Opacity. If color has 4 channels opacity is discarded.</param>
 /// <returns>System.Drawing.Color</returns>
 public static System.Drawing.Color ToColor(this Bgr <byte> color, byte opacity = Byte.MaxValue)
 {
     return(Color.FromArgb(opacity, color.R, color.G, color.B));
 }
Beispiel #17
0
        /// <summary>
        /// Draws circles.
        /// </summary>
        /// <param name="image">Input image.</param>
        /// <param name="circles">Circles</param>
        /// <param name="color">Circle color.</param>
        /// <param name="thickness">Contours thickness.</param>
        /// <param name="opacity">Sets alpha channel where 0 is transparent and 255 is full opaque.</param>
        public unsafe static void Draw(this Bgr <byte>[,] image, IEnumerable <Circle> circles, Bgr <byte> color, int thickness, byte opacity = Byte.MaxValue)
        {
            draw(image, opacity, cvImg =>
            {
                foreach (var circle in circles)
                {
                    var center = new Point(circle.X, circle.Y);

                    CvCoreInvoke.cvCircle(&cvImg, center, circle.Radius, color.ToCvScalar(),
                                          thickness, LineTypes.EightConnected, 0);
                }
            });
        }
Beispiel #18
0
 /// <summary>
 /// Draws text on the provided image.
 /// </summary>
 /// <param name="image">Input image.</param>
 /// <param name="text">User text.</param>
 /// <param name="font">Font.</param>
 /// <param name="botomLeftPoint">Bottom-left point.</param>
 /// <param name="color">Text color.</param>
 /// <param name="opacity">Sets alpha channel where 0 is transparent and 255 is full opaque.</param>
 public unsafe static void Draw(this Bgr <byte>[,] image, string text, Font font, Point botomLeftPoint, Bgr <byte> color, byte opacity = Byte.MaxValue)
 {
     draw(image, opacity, cvImg =>
     {
         CvCoreInvoke.cvPutText(&cvImg, text, botomLeftPoint, ref font, color.ToCvScalar());
     });
 }
Beispiel #19
0
 /// <summary>
 /// Converts the source color to the destination color.
 /// </summary>
 /// <param name="image">Source image.</param>
 /// <param name="area">Working area.</param>
 /// <returns>image with converted color.</returns>
 public static Hsv <byte>[,] ToHsv(this Bgr <byte>[,] image, Rectangle area)
 {
     return(image.Convert <Bgr <byte>, Hsv <byte> >(Bgr <byte> .Convert, area));
 }
Beispiel #20
0
 /// <summary>
 /// Converts the source color to the destination color.
 /// </summary>
 /// <param name="image">Source image.</param>
 /// <returns>image with converted color.</returns>
 public static Hsv <byte>[,] ToHsv(this Bgr <byte>[,] image)
 {
     return(image.Convert <Bgr <byte>, Hsv <byte> >(Bgr <byte> .Convert));
 }
Beispiel #21
0
 public static CvScalar ToCvScalar(this Bgr <byte> color, byte opacity = Byte.MaxValue)
 {
     return(new CvScalar {
         V0 = color.B, V1 = color.G, V2 = color.R, V3 = opacity
     });
 }
Beispiel #22
0
 /// <summary>
 /// Converts the source channel depth to the destination channel depth.
 /// </summary>
 /// <typeparam name="TDepth">Destination channel depth.</typeparam>
 /// <param name="image">Image.</param>
 /// <returns>Image with converted element depth.</returns>
 public static Bgr <TDepth>[,] Cast <TDepth>(this Bgr <double>[,] image)
 where TDepth : struct
 {
     return(image.ConvertChannelDepth <Bgr <double>, Bgr <TDepth> >());
 }
Beispiel #23
0
 /// <summary>
 /// Draws text on the provided image.
 /// </summary>
 /// <param name="image">Input image.</param>
 /// <param name="text">User text.</param>
 /// <param name="font">Font.</param>
 /// <param name="botomLeftPoint">Bottom-left point.</param>
 /// <param name="color">Text color.</param>
 /// <param name="opacity">Sets alpha channel where 0 is transparent and 255 is full opaque.</param>
 public unsafe static void Draw(this Bgr <byte>[,] image, string text, Font font, Point botomLeftPoint, Bgr <byte> color, byte opacity = Byte.MaxValue)
 {
     using (var img = image.Lock())
     {
         var iplImage = img.AsCvIplImage();
         CvCoreInvoke.cvPutText(&iplImage, text, botomLeftPoint, ref font, color.ToCvScalar());
     }
 }
Beispiel #24
0
 /// <summary>
 /// Saves the specified image.
 /// </summary>
 /// <param name="image">Image to save.</param>
 /// <param name="fileName">Image filename.</param>
 public static void Save(this Bgr <double>[,] image, string fileName)
 {
     image.Save <Bgr <double> >(fileName);
 }
Beispiel #25
0
 /// <summary>
 /// Converts 8-bit gray intensity to the 8-bit Bgr color.
 /// </summary>
 /// <param name="gray">Source color.</param>
 /// <param name="bgr">Destination color.</param>
 public static void Convert(Gray <T> gray, ref Bgr <T> bgr)
 {
     bgr.B = gray.Intensity;
     bgr.G = gray.Intensity;
     bgr.R = gray.Intensity;
 }
Beispiel #26
0
 /// <summary>
 /// Encodes the specified image into the Jpeg byte array.
 /// </summary>
 /// <param name="image">Image to encode.</param>
 /// <param name="jpegQuality">Jpeg quality [0..100] where 100 is the highest quality.</param>
 /// <returns>Jpeg byte array.</returns>
 public static byte[] EncodeAsJpeg(this Bgr <ushort>[,] image, int jpegQuality = 95)
 {
     return(encodeAsJpeg(image, jpegQuality));
 }
Beispiel #27
0
 public static CvScalar ToCvScalar(this Bgr <byte> color)
 {
     return(new CvScalar {
         V0 = color.B, V1 = color.G, V2 = color.R, V3 = Byte.MaxValue
     });
 }
Beispiel #28
0
 /// <summary>
 /// Converts the source color to the destination color.
 /// </summary>
 /// <param name="image">Source image.</param>
 /// <returns>image with converted color.</returns>
 public static Gray <byte>[,] ToGray(this Bgr <byte>[,] image)
 {
     return(image.Convert <Bgr <byte>, Gray <byte> >(Bgr <byte> .Convert));
 }
Beispiel #29
0
 /// <summary>
 /// Saves the specified image.
 /// </summary>
 /// <param name="image">Image to save.</param>
 /// <param name="fileName">Image filename.</param>
 public static void Save(this Bgr <ushort>[,] image, string fileName)
 {
     image.Save <Bgr <ushort> >(fileName);
 }
 /// <summary>
 /// Converts the source color to the destination color.
 /// </summary>
 /// <param name="image">Source image.</param>
 /// <param name="area">Working area.</param>
 /// <returns>image with converted color.</returns>
 public static Bgra <byte>[,] ToBgra(this Bgr <byte>[,] image, Rectangle area)
 {
     return(image.Convert <Bgr <byte>, Bgra <byte> >(Bgr <byte> .Convert, area));
 }