/// <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.AsOpenCvImage(); CvCoreInvoke.cvPutText(&iplImage, text, botomLeftPoint, ref font, color.ToCvScalar()); } }
/// <summary> /// Draws ellipse. /// </summary> /// <param name="image">Input image.</param> /// <param name="ellipse">Ellipse.</param> /// <param name="color">Object's color.</param> /// <param name="thickness">Border thickness.</param> public unsafe static void Draw(this Bgr <byte>[,] image, Ellipse ellipse, Bgr <byte> color, int thickness) { using (var img = image.Lock()) { var iplImage = img.AsOpenCvImage(); CvCoreInvoke.cvEllipse(&iplImage, ellipse.Center.Round(), Size.Round(ellipse.Size), ellipse.Angle, 0, 2 * System.Math.PI, color.ToCvScalar(), thickness, LineTypes.EightConnected, 0); } }
/// <summary> /// Converts an image to an bitmap. /// </summary> /// <param name="img">Input image.</param> /// <returns>Bitmap</returns> public static Bitmap ToBitmap(this Bgr <short>[,] img) { Bitmap bmp = null; using (var uImg = img.Lock()) { bmp = toBitmap(uImg, PixelFormat.Format48bppRgb); } return(bmp); }
/// <summary> /// Draws circle. /// </summary> /// <param name="image">Input image.</param> /// <param name="circle">Circle</param> /// <param name="color">Circle color.</param> /// <param name="thickness">Contours thickness.</param> public unsafe static void Draw(this Bgr <byte>[,] image, Circle circle, Bgr <byte> color, int thickness) { using (var img = image.Lock()) { var iplImage = img.AsOpenCvImage(); var center = new Point(circle.X, circle.Y); CvCoreInvoke.cvCircle(&iplImage, center, circle.Radius, color.ToCvScalar(), thickness, LineTypes.EightConnected, 0); } }
/// <summary> /// Draws rectangle. /// </summary> /// <param name="image">Input image.</param> /// <param name="rect">Rectangle.</param> /// <param name="color">Object's color.</param> /// <param name="thickness">Border thickness. If less than zero structure will be filled.</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, Rectangle rect, Bgr <byte> color, int thickness, byte opacity = Byte.MaxValue) { if (float.IsNaN(rect.X) || float.IsNaN(rect.Y)) { return; } using (var img = image.Lock()) { var iplImage = img.AsOpenCvImage(); CvCoreInvoke.cvRectangleR(&iplImage, rect, color.ToCvScalar(opacity), thickness, LineTypes.EightConnected, 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.AsOpenCvImage(); //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(); }
/// <summary> /// The blending filter is able to blend two images using a homography matrix. /// A linear alpha gradient is used to smooth out differences between the two /// images, effectively blending them in two images. The gradient is computed /// considering the distance between the centers of the two images. /// </summary> /// <param name="im">Image.</param> /// <param name="overlayIm">The overlay image (also called the anchor).</param> /// <param name="homography">Homography matrix used to map a image passed to /// the filter to the overlay image specified at filter creation.</param> /// <param name="fillColor">The filling color used to fill blank spaces. The filling color will only be visible after the image is converted /// to 24bpp. The alpha channel will be used internally by the filter.</param> /// <param name="gradient">A value indicating whether to blend using a linear /// gradient or just superimpose the two images with equal weights.</param> /// <param name="alphaOnly">A value indicating whether only the alpha channel /// should be blended. This can be used together with a transparency /// mask to selectively blend only portions of the image.</param> /// <returns>Blended image.</returns> public static Bgra <byte>[,] Blend(this Bgr <byte>[,] im, Bgr <byte>[,] overlayIm, MatrixH homography, Bgra <byte> fillColor, bool gradient = true, bool alphaOnly = false) { Bgra <byte>[,] resultImage = null; using (var uOverlayIm = overlayIm.Lock()) { Blend blend = new Blend(homography, uOverlayIm.AsBitmap()); blend.AlphaOnly = alphaOnly; blend.Gradient = gradient; blend.FillColor = fillColor.ToColor(); resultImage = im.ApplyBaseTransformationFilter <Bgr <byte>, Bgra <byte> >(blend); } return(resultImage); }
/// <summary> /// Computes gradient orientations from the color image. Orientation from the channel which has the maximum gradient magnitude is taken as the orientation for a location. /// </summary> /// <param name="frame">Image.</param> /// <param name="magnitudeSqrImage">Squared magnitude image.</param> /// <param name="minValidMagnitude">Minimal valid magnitude.</param> /// <returns>Orientation image (angles are in degrees).</returns> public static unsafe Gray<int>[,] Compute(Bgr<byte>[,] frame, out Gray<int>[,] magnitudeSqrImage, int minValidMagnitude) { var minSqrMagnitude = minValidMagnitude * minValidMagnitude; var orientationImage = new Gray<int>[frame.Height(), frame.Width()]; var _magnitudeSqrImage = orientationImage.CopyBlank(); using (var uFrame = frame.Lock()) { ParallelLauncher.Launch(thread => { computeColor(thread, (byte*)uFrame.ImageData, uFrame.Stride, orientationImage, _magnitudeSqrImage, minSqrMagnitude); }, frame.Width() - 2 * kernelRadius, frame.Height() - 2 * kernelRadius); } magnitudeSqrImage = _magnitudeSqrImage; return orientationImage; }
/// <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.</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(); using (var img = image.Lock()) { var iplImage = img.AsOpenCvImage(); for (int i = 0; i < vertices.Length; i++) { int idx2 = (i + 1) % vertices.Length; CvCoreInvoke.cvLine(&iplImage, vertices[i].Round(), vertices[idx2].Round(), color.ToCvScalar(opacity), thickness, LineTypes.EightConnected, 0); } } }
/// <summary> /// The blending filter is able to blend two images using a homography matrix. /// A linear alpha gradient is used to smooth out differences between the two /// images, effectively blending them in two images. The gradient is computed /// considering the distance between the centers of the two images. /// </summary> /// <param name="im">Image.</param> /// <param name="overlayIm">The overlay image (also called the anchor).</param> /// <param name="homography">Homography matrix used to map a image passed to /// the filter to the overlay image specified at filter creation.</param> /// <param name="fillColor">The filling color used to fill blank spaces. The filling color will only be visible after the image is converted /// to 24bpp. The alpha channel will be used internally by the filter.</param> /// <param name="gradient">A value indicating whether to blend using a linear /// gradient or just superimpose the two images with equal weights.</param> /// <param name="alphaOnly">A value indicating whether only the alpha channel /// should be blended. This can be used together with a transparency /// mask to selectively blend only portions of the image.</param> /// <returns>Blended image.</returns> public static Bgra<byte>[,] Blend(this Bgr<byte>[,] im, Bgr<byte>[,] overlayIm, MatrixH homography, Bgra<byte> fillColor, bool gradient = true, bool alphaOnly = false) { Bgra<byte>[,] resultImage = null; using (var uOverlayIm = overlayIm.Lock()) { Blend blend = new Blend(homography, uOverlayIm.AsBitmap()); blend.AlphaOnly = alphaOnly; blend.Gradient = gradient; blend.FillColor = fillColor.ToColor(); resultImage = im.ApplyBaseTransformationFilter<Bgr<byte>, Bgra<byte>>(blend); } return resultImage; }