/// <summary> /// Convert a Bitmap to a Mat /// </summary> /// <param name="mat">The mat to copy Bitmap into</param> /// <param name="bitmap">The bitmap to copy into mat</param> public static void ToMat(this Android.Graphics.Bitmap bitmap, Mat mat) { Android.Graphics.Bitmap.Config config = bitmap.GetConfig(); if (config.Equals(Android.Graphics.Bitmap.Config.Argb8888)) { using (BitmapArgb8888Image bi = new BitmapArgb8888Image(bitmap)) { CvInvoke.CvtColor(bi, mat, ColorConversion.Rgba2Bgra); } } else if (config.Equals(Android.Graphics.Bitmap.Config.Rgb565)) { Size size = new Size(bitmap.Width, bitmap.Height); int[] values = new int[size.Width * size.Height]; bitmap.GetPixels(values, 0, size.Width, 0, 0, size.Width, size.Height); GCHandle handle = GCHandle.Alloc(values, GCHandleType.Pinned); using (Mat bgra = new Mat(size, DepthType.Cv8U, 4, handle.AddrOfPinnedObject(), size.Width * 4)) { bgra.CopyTo(mat); } handle.Free(); } else { throw new NotImplementedException(String.Format("Coping from Bitmap of {0} is not implemented", config)); } }
public Bitmap ToBitmap(Bitmap.Config config) { System.Drawing.Size size = Size; if (config == Bitmap.Config.Argb8888) { Bitmap result = Bitmap.CreateBitmap(size.Width, size.Height, Bitmap.Config.Argb8888); using (BitmapArgb8888Image bi = new BitmapArgb8888Image(result)) { bi.ConvertFrom(this); //CvInvoke.cvSet(bi, new MCvScalar(0, 0, 255, 255), IntPtr.Zero); } return(result); } else if (config == Bitmap.Config.Rgb565) { Bitmap result = Bitmap.CreateBitmap(size.Width, size.Height, Bitmap.Config.Rgb565); using (BitmapRgb565Image bi = new BitmapRgb565Image(result)) bi.ConvertFrom(this); return(result); } else { throw new NotImplementedException("Only Bitmap config of Argb888 or Rgb565 is supported."); } }
/// <summary> /// Convert Image object to Bitmap /// </summary> /// <param name="config">The Bitmap Config</param> /// <returns>The Bitmap</returns> public static Android.Graphics.Bitmap ToBitmap <TColor, TDepth>(this Image <TColor, TDepth> image, Android.Graphics.Bitmap.Config config) where TColor : struct, IColor where TDepth : new() { System.Drawing.Size size = image.Size; if (config == Android.Graphics.Bitmap.Config.Argb8888) { Android.Graphics.Bitmap result = Android.Graphics.Bitmap.CreateBitmap(size.Width, size.Height, Android.Graphics.Bitmap.Config.Argb8888); using (BitmapArgb8888Image bi = new BitmapArgb8888Image(result)) { bi.ConvertFrom(image); //CvInvoke.cvSet(bi, new MCvScalar(0, 0, 255, 255), IntPtr.Zero); } return(result); } else if (config == Android.Graphics.Bitmap.Config.Rgb565) { Android.Graphics.Bitmap result = Android.Graphics.Bitmap.CreateBitmap(size.Width, size.Height, Android.Graphics.Bitmap.Config.Rgb565); using (BitmapRgb565Image bi = new BitmapRgb565Image(result)) bi.ConvertFrom(image); return(result); } else { throw new NotImplementedException("Only Bitmap config of Argb888 or Rgb565 is supported."); } }
public Bitmap ToBitmap(Bitmap.Config config) { System.Drawing.Size size = Size; if (config == Bitmap.Config.Argb8888) { Bitmap result = Bitmap.CreateBitmap(size.Width, size.Height, Bitmap.Config.Argb8888); using (BitmapArgb8888Image bi = new BitmapArgb8888Image(result)) using (Image <Rgba, Byte> tmp = ToImage <Rgba, Byte>()) { tmp.Copy(bi, null); } return(result); } else if (config == Bitmap.Config.Rgb565) { Bitmap result = Bitmap.CreateBitmap(size.Width, size.Height, Bitmap.Config.Rgb565); using (BitmapRgb565Image bi = new BitmapRgb565Image(result)) using (Image <Bgr, Byte> tmp = ToImage <Bgr, Byte>()) bi.ConvertFrom(tmp); return(result); } else { throw new NotImplementedException("Only Bitmap config of Argb888 or Rgb565 is supported."); } }
/// <summary> /// Convert the Mat to Bitmap /// </summary> /// <param name="mat">The Mat to convert to Bitmap</param> /// <param name="bitmap">The bitmap, must be of the same size and has bitmap config type of either Argb888 or Rgb565</param> /// <returns>The Bitmap</returns> public static void ToBitmap(this Mat mat, Android.Graphics.Bitmap bitmap) { System.Drawing.Size size = mat.Size; if (!(size.Width == bitmap.Width && size.Height == bitmap.Height)) { throw new Exception("Bitmap size doesn't match the Mat size"); } Android.Graphics.Bitmap.Config config = bitmap.GetConfig(); if (config == Android.Graphics.Bitmap.Config.Argb8888) { int channels = mat.NumberOfChannels; using (BitmapArgb8888Image bi = new BitmapArgb8888Image(bitmap)) { if (channels == 1) { CvInvoke.CvtColor(mat, bi.Mat, ColorConversion.Gray2Rgba); } else if (channels == 3) { CvInvoke.CvtColor(mat, bi, ColorConversion.Bgr2Rgba); } else if (channels == 4) { CvInvoke.CvtColor(mat, bi, ColorConversion.Bgra2Rgba); } else { using (Image <Rgba, Byte> tmp = mat.ToImage <Rgba, Byte>()) { tmp.Copy(bi, null); } } } } else if (config == Android.Graphics.Bitmap.Config.Rgb565) { using (BitmapRgb565Image bi = new BitmapRgb565Image(bitmap)) using (Image <Bgr, Byte> tmp = mat.ToImage <Bgr, Byte>()) bi.ConvertFrom(tmp); } else { throw new NotImplementedException("Only Bitmap config of Argb888 or Rgb565 is supported."); } }
//#region Conversion with Bitmap /// <summary> /// The Get property provide a more efficient way to convert Image<Gray, Byte>, Image<Bgr, Byte> and Image<Bgra, Byte> into Bitmap /// such that the image data is <b>shared</b> with Bitmap. /// If you change the pixel value on the Bitmap, you change the pixel values on the Image object as well! /// For other types of image this property has the same effect as ToBitmap() /// <b>Take extra caution not to use the Bitmap after the Image object is disposed</b> /// The Set property convert the bitmap to this Image type. /// </summary> public static Image <TColor, TDepth> ToImage <TColor, TDepth>(this Bitmap bitmap) where TColor : struct, IColor where TDepth : new() { #region reallocate memory if necessary Size size = new Size(bitmap.Width, bitmap.Height); Image <TColor, TDepth> image = new Image <TColor, TDepth>(size); /* * if (image.Ptr == IntPtr.Zero) * { * image.AllocateData(size.Height, size.Width, image.NumberOfChannels); * } * else if (!Size.Equals(size)) * { * image.DisposeObject(); * image.AllocateData(size.Height, size.Width, image.NumberOfChannels); * }*/ #endregion Android.Graphics.Bitmap.Config config = bitmap.GetConfig(); if (config.Equals(Android.Graphics.Bitmap.Config.Argb8888)) { using (BitmapArgb8888Image bi = new BitmapArgb8888Image(bitmap)) { image.ConvertFrom(bi); } } else if (config.Equals(Android.Graphics.Bitmap.Config.Rgb565)) { int[] values = new int[size.Width * size.Height]; bitmap.GetPixels(values, 0, size.Width, 0, 0, size.Width, size.Height); GCHandle handle = GCHandle.Alloc(values, GCHandleType.Pinned); using (Image <Bgra, Byte> bgra = new Image <Bgra, byte>(size.Width, size.Height, size.Width * 4, handle.AddrOfPinnedObject())) { image.ConvertFrom(bgra); } handle.Free(); } else { throw new NotImplementedException(String.Format("Coping from Bitmap of {0} is not implemented", config)); } return(image); }
/// <summary> /// Convert the Mat to Bitmap /// </summary> /// <param name="config">The bitmap config type. If null, Argb8888 will be used</param> /// <returns>The Bitmap</returns> public Bitmap ToBitmap(Bitmap.Config config = null) { System.Drawing.Size size = Size; if (config == null || config == Bitmap.Config.Argb8888) { Bitmap result = Bitmap.CreateBitmap(size.Width, size.Height, Bitmap.Config.Argb8888); int channels = NumberOfChannels; using (BitmapArgb8888Image bi = new BitmapArgb8888Image(result)) { if (channels == 1) { CvInvoke.CvtColor(this, bi.Mat, ColorConversion.Gray2Rgba); } else if (channels == 3) { CvInvoke.CvtColor(this, bi, ColorConversion.Bgr2Rgba); } else if (channels == 4) { CvInvoke.CvtColor(this, bi, ColorConversion.Bgra2Rgba); } else { using (Image <Rgba, Byte> tmp = ToImage <Rgba, Byte>()) { tmp.Copy(bi, null); } } } return(result); } else if (config == Bitmap.Config.Rgb565) { Bitmap result = Bitmap.CreateBitmap(size.Width, size.Height, Bitmap.Config.Rgb565); using (BitmapRgb565Image bi = new BitmapRgb565Image(result)) using (Image <Bgr, Byte> tmp = ToImage <Bgr, Byte>()) bi.ConvertFrom(tmp); return(result); } else { throw new NotImplementedException("Only Bitmap config of Argb888 or Rgb565 is supported."); } }