我目前正在做一个使用面部识别的项目。因此,我需要一种向用户显示网络摄像头图像的方法,以便他可以调整自己的脸。
我一直在尝试使用尽可能少的 CPU 从网络摄像头获取图像:
但是它们都不是很好......要么太慢要么太消耗 CPU 资源。
然后我尝试了Emgu 库,感觉很棒。起初,我在一个 Windows 窗体项目中进行了尝试,并正在更新Picture Box中的图像。但是,当我尝试将它集成到我的 WPF 项目中时,我陷入了如何将图像传递给图像控件的问题。
现在,我有以下源代码:
<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>
以及背后的代码:
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();
}
}
我的猜测是使用 BitmapSource/WriteableBitmap 但我没有让它们工作......
谢谢!