如何显示使用Emgu捕获的网络摄像头图像?

Zog*_*riP 10 c# wpf webcam image

我目前正在开发一个使用面部识别的项目.因此,我需要一种方法向用户显示网络摄像头图像,以便他可以调整他的脸部.

我一直在尝试使用尽可能少的CPU来从网络摄像头获取图像:

但它们都不是很好......无论是太慢还是太耗费CPU资源.

然后我尝试了Emgu库,我觉得很棒.起初,我在Windows窗体项目中尝试了它,并在图片框中更新图像.但是,当我试图将它集成到我的WPF项目中时,我遇到了如何将我的图像传递给我的Image控件的问题.

现在,我有以下源代码:

<Window x:Class="HA.FacialRecognition.Enroll.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Width="800" Height="600"
        Loaded="Window_Loaded" Closing="Window_Closing">
    <Grid>
        <Image x:Name="webcam" Width="640" Height="480" >
            <Image.Clip>
                <EllipseGeometry  RadiusX="240" RadiusY="240">
                    <EllipseGeometry.Center>
                        <Point X="320" Y="240" />
                    </EllipseGeometry.Center>
                </EllipseGeometry>
            </Image.Clip>
        </Image>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

而背后的代码:

private Capture capture;
private System.Timers.Timer timer;

public Window1()
{
    InitializeComponent();
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    capture = new Capture();
    capture.FlipHorizontal = true;

    timer = new System.Timers.Timer();
    timer.Interval = 15;
    timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
    timer.Start();
}

void timer_Elapsed(object sender, ElapsedEventArgs e)
{
    using (Image<Bgr, byte> frame = capture.QueryFrame())
    {
        if (frame != null)
        {
            var bmp = frame.Bitmap;
            // How do I pass this bitmap to my Image control called "webcam"?
        }
    }
}

private void Window_Closing(object sender, CancelEventArgs e)
{
    if (capture != null)
    {
        capture.Dispose();
    }
}
Run Code Online (Sandbox Code Playgroud)

我的猜测是使用BitmapSource/WriteableBitmap,但我没有让它们工作......

谢谢!

Ank*_*kit 3

Image 类有一个您可能正在寻找的 UriSource 属性