/// <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()); } }
/// <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 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.AsCvIplImage(); 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 <byte>[,] img) { Bitmap bmp = null; using (var uImg = img.Lock()) { bmp = toBitmap(uImg, PixelFormat.Format24bppRgb); } 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.AsCvIplImage(); var center = new Point(circle.X, circle.Y); CvCoreInvoke.cvCircle(&iplImage, center, circle.Radius, color.ToCvScalar(), thickness, LineTypes.EightConnected, 0); } }
/// <summary> /// Displays the specified image in a window (non blocking mode). /// </summary> /// <param name="image">Image to show.</param> /// <param name="windowTitle">Window title (ID).</param> /// <param name="autoSize">True to adjust form to the image size, false otherwise.</param> public static void Show(this Bgr <byte>[,] image, string windowTitle = "Image", bool autoSize = false) { CvInvoke.cvNamedWindow(windowTitle, autoSize ? WindowSizing.AutoSize : WindowSizing.Normal); using (var uIm = image.Lock()) { var mat = uIm.AsCvMat(); CvInvoke.cvShowImage(windowTitle, ref mat); } CvInvoke.cvWaitKey(5); }
/// <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.AsCvIplImage(); CvCoreInvoke.cvRectangleR(&iplImage, rect, color.ToCvScalar(opacity), thickness, LineTypes.EightConnected, 0); } }
private unsafe static void draw(Bgr <byte>[,] image, byte opacity, Action <IplImage> drawingAction) { using (var uImg = image.Lock()) { var cvImg = uImg.AsCvIplImage(); var cvOverlayImPtr = CvCoreInvoke.cvCloneImage(&cvImg); drawingAction(*cvOverlayImPtr); CvCoreInvoke.cvAddWeighted(cvOverlayImPtr, (float)opacity / Byte.MaxValue, &cvImg, 1 - (float)opacity / Byte.MaxValue, 0, &cvImg); CvCoreInvoke.cvReleaseImage(&cvOverlayImPtr); } }
/// <summary> /// Converts the specified managed array to the corresponding bitmap source. /// </summary> /// <param name="image">Managed array.</param> /// <returns>Bitmap source.</returns> public static BitmapSource ToBitmapSource(this Bgr <byte>[,] image) { BitmapSource bmpSource = null; using (var uImg = image.Lock()) { bmpSource = BitmapSource.Create(uImg.Width, uImg.Height, 96, 96, PixelFormats.Bgr24, null, uImg.ImageData, uImg.Stride * uImg.Height, uImg.Stride); } return(bmpSource); }
/// <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(); }
/// <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()); } }
/// <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.AsCvIplImage(); 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); } } }
unsafe static void Draw(Bgr <byte>[,] image, byte opacity, Action <IplImage> drawingAction) { using (var uImg = image.Lock()) DrawingNativeImage.Draw(uImg, opacity, drawingAction); }