本帖最后由 Coos 于 2012-12-16 19:54 编辑
先上图看效果:
首先是wp有自带的mjpeg解码功能
能输出WriteableBitmap格式,方便接下来进行图像分析
速度还行,最大也只是100多MS的延迟
接收图像方法
public void recVideo()
{
string sourceURL = "图片流地址";
while (true)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(new Uri(sourceURL));
req.BeginGetResponse(new AsyncCallback(resResult), req);
//Thread.Sleep(10);
}
}
异步调用方法
private void resResult(IAsyncResult ir)
{
try
{
HttpWebRequest req = ir.AsyncState as HttpWebRequest;
WebResponse resp = req.EndGetResponse(ir);
Stream stream = resp.GetResponseStream();
Dispatcher.BeginInvoke(() =>
{
WriteableBitmap myBitmap = PictureDecoder.DecodeJpeg(stream); 这里就是自带的解码方法,一句话就行了,如果只要实现视频输出,下面图像识别就可以删掉了
image1.Source = myBitmap;
#region 图像识别
var w = myBitmap.PixelWidth;
var h = myBitmap.PixelHeight;
if (_pixelDataGray == null || _pixelDataGray.Length != h * w)
{
//定义了几个数组
_pixelDataGray = new byte[w / _downsampleFactor * h / _downsampleFactor];
_pixelDataDownsampled =
new byte[w / _downsampleFactor * h / _downsampleFactor];
_pixelDataGrayInt = new int[w / _downsampleFactor * h / _downsampleFactor];
_wb = new WriteableBitmap(w / _downsampleFactor, w / _downsampleFactor);
}
//另外写了几个类。Utils 和 FaceDetection,其中FaceDetection的检测算法是跟opencv学的
Utils.DownSample(myBitmap.Pixels, w, h, ref _pixelDataGrayInt, _downsampleFactor);
Utils.ARGBToGreyScale(_pixelDataGrayInt, ref _pixelDataGray);
//_pixelDataGray = e.FrameBuffer;
Utils.HistogramEqualization(ref _pixelDataGray);
Utils.GrayToARGB(_pixelDataGray, ref _pixelDataGrayInt);
List<FaceDetection.Rectangle> faces =
_detector.getFaces(
_pixelDataGrayInt,
w / _downsampleFactor,
h / _downsampleFactor,
2f, 1.25f, 0.1f, 1, false, true);
_pixelDataGrayInt.CopyTo(_wb.Pixels, 0);
_wb.Invalidate();
//image控件上覆盖了个 canvas控件,用来画圆圈
cnvsFaceRegions.Children.Clear();
foreach (var r in faces)
{
Ellipse el = new Ellipse();
TranslateTransform loc = new TranslateTransform();
loc.X = r.X * _downsampleFactor / (double)w * cnvsFaceRegions.Width;
loc.Y = r.Y * _downsampleFactor / (double)w * cnvsFaceRegions.Height;
el.RenderTransform = loc;
el.Width = r.Width * _downsampleFactor *2;
el.Height = r.Height * _downsampleFactor *2;
el.Stroke = new SolidColorBrush(Colors.Red);
cnvsFaceRegions.Children.Add(el);
}
#endregion
});
}
catch (Exception ex)
{
Dispatcher.BeginInvoke(() => sysInfo.Text = ex.Message);
}
}
相关类:
ryancar.zip
(7.76 KB, 下载次数: 59)
|