Beispiel #1
0
 /// <summary>
 /// Convert the umat into Bitmap, the pixel values are copied over to the Bitmap
 /// </summary>
 public static Bitmap ToBitmap(this UMat umat)
 {
     using (Mat tmp = umat.GetMat(CvEnum.AccessType.Read))
     {
         return(tmp.ToBitmap());
     }
 }
Beispiel #2
0
 /// <summary>
 /// Convert the gpuMat into Bitmap, the pixel values are copied over to the Bitmap
 /// </summary>
 public static Bitmap ToBitmap(this GpuMat gpuMat)
 {
     using (Mat tmp = new Mat())
     {
         gpuMat.Download(tmp);
         return(tmp.ToBitmap());
     }
 }
Beispiel #3
0
        /// <summary>
        /// Convert the Mat to Bitmap
        /// </summary>
        /// <param name="mat">The Mat to convert to Bitmap</param>
        /// <param name="config">The bitmap config type. If null, Argb8888 will be used</param>
        /// <returns>The Bitmap</returns>
        public static Android.Graphics.Bitmap ToBitmap(this Mat mat, Android.Graphics.Bitmap.Config config = null)
        {
            System.Drawing.Size size = mat.Size;

            if (config == null)
            {
                config = Android.Graphics.Bitmap.Config.Argb8888;
            }

            Android.Graphics.Bitmap result = Android.Graphics.Bitmap.CreateBitmap(size.Width, size.Height, config);
            mat.ToBitmap(result);
            return(result);
        }
Beispiel #4
0
        /// <summary>
        /// Write the Mat into the file
        /// </summary>
        /// <param name="mat">The Mat to write</param>
        /// <param name="fileName">The name of the file to be written into</param>
        /// <returns>True if the file has been written into Mat</returns>
        public bool WriteFile(Mat mat, String fileName)
        {
            try
            {
                //Try to save the image using .NET's Bitmap class
                String extension = System.IO.Path.GetExtension(fileName);
                if (!String.IsNullOrEmpty(extension))
                {
                    using (Bitmap bmp = mat.ToBitmap())
                    {
                        switch (extension.ToLower())
                        {
                        case ".jpg":
                        case ".jpeg":
                            bmp.Save(fileName, ImageFormat.Jpeg);
                            break;

                        case ".bmp":
                            bmp.Save(fileName, ImageFormat.Bmp);
                            break;

                        case ".png":
                            bmp.Save(fileName, ImageFormat.Png);
                            break;

                        case ".tiff":
                        case ".tif":
                            bmp.Save(fileName, ImageFormat.Tiff);
                            break;

                        case ".gif":
                            bmp.Save(fileName, ImageFormat.Gif);
                            break;

                        default:
                            throw new NotImplementedException(String.Format("Saving to {0} format is not supported", extension));
                        }
                    }
                }
                return(true);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                //throw;
                return(false);
            }
        }
        /// <summary>
        /// Convert an IImage to a WPF BitmapSource. The result can be used in the Set Property of Image.Source
        /// </summary>
        /// <param name="image">The Emgu CV Image</param>
        /// <returns>The equivalent BitmapSource</returns>
        public static BitmapSource ToBitmapSource(this IInputArray image)
        {
            using (InputArray ia = image.GetInputArray())
                using (Mat m = ia.GetMat())
                    using (System.Drawing.Bitmap source = m.ToBitmap())
                    {
                        IntPtr ptr = source.GetHbitmap(); //obtain the Hbitmap

                        BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                            ptr,
                            IntPtr.Zero,
                            Int32Rect.Empty,
                            System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

                        DeleteObject(ptr); //release the HBitmap
                        return(bs);
                    }
        }
Beispiel #6
0
        /// <summary>
        /// Write the Mat into file
        /// </summary>
        /// <param name="mat">The mat to be written</param>
        /// <param name="fileName">The name of the file</param>
        /// <returns>True if successfully written Mat into file</returns>
        public bool WriteFile(Mat mat, String fileName)
        {
            try
            {
                FileInfo fileInfo = new FileInfo(fileName);
                using (Android.Graphics.Bitmap bmp = mat.ToBitmap())
                    using (FileStream fs = fileInfo.Open(FileMode.Append, FileAccess.Write))
                    {
                        String extension = fileInfo.Extension.ToLower();
                        Debug.Assert(extension.Substring(0, 1).Equals("."));
                        switch (extension)
                        {
                        case ".jpg":
                        case ".jpeg":
                            bmp.Compress(Android.Graphics.Bitmap.CompressFormat.Jpeg, 90, fs);
                            break;

                        case ".png":
                            bmp.Compress(Android.Graphics.Bitmap.CompressFormat.Png, 90, fs);
                            break;

                        case ".webp":
                            bmp.Compress(Android.Graphics.Bitmap.CompressFormat.Webp, 90, fs);
                            break;

                        default:
                            throw new NotImplementedException(String.Format("Saving to {0} format is not supported",
                                                                            extension));
                        }
                    }

                return(true);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                //throw;
                return(false);
            }
        }