示例#1
0
 /// <summary>
 /// Get a copy of the data values as an array
 /// </summary>
 /// <param name="jagged">If true, a jagged array will returned. Otherwise it will return a regular array.</param>
 /// <returns>a copy of the data values as an array</returns>
 public Array GetData(bool jagged = true)
 {
     using (InputArray iaM = this.GetInputArray())
         using (Mat m = iaM.GetMat())
         {
             return(m.GetData(jagged));
         }
 }
示例#2
0
 /// <summary>
 /// Converts to NSImage.
 /// </summary>
 /// <returns>The NSImage.</returns>
 public static NSImage ToNSImage(this IInputArray inputArray)
 {
     using (InputArray array = inputArray.GetInputArray())
         using (Mat m = array.GetMat())
         {
             return(m.ToNSImage());
         }
 }
示例#3
0
 /// <summary>
 /// Converts to UIImage.
 /// </summary>
 /// <returns>The UIImage</returns>
 public static UIImage ToUIImage(this IInputArray inputArray)
 {
     using (InputArray ia = inputArray.GetInputArray())
         using (Mat mat = ia.GetMat())
         {
             return(mat.ToUIImage());
         }
 }
示例#4
0
        /*
         * public static Mat ToMat(this WriteableBitmap writeableBitmap)
         * {
         *  Mat m = new Mat();
         *  writeableBitmap.ToArray(m);
         *  return m;
         * }*/

        public static WriteableBitmap ToWritableBitmap(this IInputArray array)
        {
            using (InputArray ia = array.GetInputArray())
            {
                Size            size   = ia.GetSize();
                WriteableBitmap bmp    = new WriteableBitmap(size.Width, size.Height);
                byte[]          buffer = new byte[bmp.PixelWidth * bmp.PixelHeight * 4];
                GCHandle        handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);

                using (Mat resultImage = new Mat(
                           new Size(bmp.PixelWidth, bmp.PixelHeight),
                           DepthType.Cv8U,
                           4,
                           handle.AddrOfPinnedObject(),
                           bmp.PixelWidth * 4))
                {
                    int channels = ia.GetChannels();
                    switch (channels)
                    {
                    case 1:
                        CvInvoke.CvtColor(array, resultImage, ColorConversion.Gray2Bgra);
                        break;

                    case 3:
                        CvInvoke.CvtColor(array, resultImage, ColorConversion.Bgr2Bgra);
                        break;

                    case 4:
                        using (Mat m = ia.GetMat())
                            m.CopyTo(resultImage);
                        break;

                    default:
                        throw new NotImplementedException(String.Format(
                                                              "Conversion from {0} channel IInputArray to WritableBitmap is not supported",
                                                              channels));
                    }
                }
                handle.Free();

                using (Stream resultStream = bmp.PixelBuffer.AsStream())
                {
                    resultStream.Write(buffer, 0, buffer.Length);
                }

                return(bmp);
            }
        }
示例#5
0
        /// <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);
                    }
        }
示例#6
0
        /// <summary>
        /// Convert an input array to texture 2D
        /// </summary>
        /// <param name="image">The input image, if 3 channel, we assume it is Bgr, if 4 channels, we assume it is Bgra</param>
        /// <param name="flipType"></param>
        /// <param name="buffer">Optional buffer for the texture conversion, should be big enough to hold the image data. e.g. width*height*pixel_size</param>
        /// <returns>The texture 2D</returns>
        public static Texture2D InputArrayToTexture2D(IInputArray image, Emgu.CV.CvEnum.FlipType flipType = FlipType.Vertical, byte[] buffer = null)
        {
            using (InputArray iaImage = image.GetInputArray())
                using (Mat m = iaImage.GetMat())
                {
                    Size size = m.Size;

                    if (m.NumberOfChannels == 3 && m.Depth == DepthType.Cv8U)
                    {
                        Texture2D texture = new Texture2D(size.Width, size.Height, TextureFormat.RGB24, false);

                        byte[] data;
                        int    bufferLength = size.Width * size.Height * 3;
                        if (buffer != null)
                        {
                            if (buffer.Length < bufferLength)
                            {
                                throw new ArgumentException(String.Format("Buffer size ({0}) is not big enough for the RBG24 texture, width * height * 3 = {1} is required.", buffer.Length, bufferLength));
                            }
                            data = buffer;
                        }
                        else
                        {
                            data = new byte[bufferLength];
                        }
                        GCHandle dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned);
                        using (
                            Image <Rgb, byte> rgb = new Image <Rgb, byte>(size.Width, size.Height, size.Width * 3,
                                                                          dataHandle.AddrOfPinnedObject()))
                        {
                            rgb.ConvertFrom(m);
                            if (flipType != FlipType.None)
                            {
                                CvInvoke.Flip(rgb, rgb, flipType);
                            }
                        }
                        dataHandle.Free();
                        texture.LoadRawTextureData(data);
                        texture.Apply();
                        return(texture);
                    }
                    else //if (m.NumberOfChannels == 4 && m.Depth == DepthType.Cv8U)
                    {
                        Texture2D texture = new Texture2D(size.Width, size.Height, TextureFormat.RGBA32, false);
                        byte[]    data;
                        int       bufferLength = size.Width * size.Height * 4;
                        if (buffer != null)
                        {
                            if (buffer.Length < bufferLength)
                            {
                                throw new ArgumentException(String.Format("Buffer size ({0}) is not big enough for the RGBA32 texture, width * height * 4 = {1} is required.", buffer.Length, bufferLength));
                            }
                            data = buffer;
                        }
                        else
                        {
                            data = new byte[bufferLength];
                        }
                        GCHandle dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned);
                        using (
                            Image <Rgba, byte> rgba = new Image <Rgba, byte>(size.Width, size.Height, size.Width * 4,
                                                                             dataHandle.AddrOfPinnedObject()))
                        {
                            rgba.ConvertFrom(m);
                            if (flipType != FlipType.None)
                            {
                                CvInvoke.Flip(rgba, rgba, flipType);
                            }
                        }
                        dataHandle.Free();
                        texture.LoadRawTextureData(data);

                        texture.Apply();
                        return(texture);
                    }
                }
        }