How to display webcam images captured with Emgu?

前端 未结 7 946
北恋
北恋 2020-12-10 14:18

I\'m currently working on a project that use Facial Recognition. I therefore need a way to display the webcam images to the user so he can adjust his face.

I\'ve bee

7条回答
  •  北荒
    北荒 (楼主)
    2020-12-10 15:13

    Look on the Emgu wiki -> Tutorials -> Examples -> WPF (Windows Presentation Foundation) It contains the following code snippet to convert your IImage to a BitmapSource, which you can directly apply to your control.

    using Emgu.CV; using System.Runtime.InteropServices; ...

        /// 
        /// Delete a GDI object
        /// 
        /// The poniter to the GDI object to be deleted
        /// 
        [DllImport("gdi32")]
        private static extern int DeleteObject(IntPtr o);
    
        /// 
        /// Convert an IImage to a WPF BitmapSource. The result can be used in the Set Property of Image.Source
        /// 
        /// The Emgu CV Image
        /// The equivalent BitmapSource
        public static BitmapSource ToBitmapSource(IImage image)
        {
            using (System.Drawing.Bitmap source = image.Bitmap)
            {
                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;
            }
        }
    

提交回复
热议问题