EmguCV:使用光流在Motion中绘制物体轮廓?

Jea*_*sse 14 c# computer-vision emgucv

我想在C#(使用EmguCV 3.0)中进行运动检测,以移除运动中或前景中的对象以绘制叠加层.

这是我用Kinect完成的一个示例测试(因为它是一个深度相机) Démo与Kinect

我如何开始使用EmguCV 3.0?

  • 我尝试了许多不起作用的背景删除代码
  • 看起来OpticalFlow是一个很好的开始,但在EmguCV 3.0中没有任何例子
  • 如果我找到最大的斑点,我怎么能找到它的轮廓?

有人可以帮助我开始吗?

编辑:17/06/2015

在EmguCV3.0.0 RC中,我没有在包和文档中看到OpticalFlow:http://www.emgu.com/wiki/files/3.0.0-rc1/document/html/b72c032d-59ae-c36f-5e00-12f8d621dfb8 热媒

只有:DenseOpticalFlow,OpticalFlowDualTVL1 ???

这是一个AbsDiff代码:

var grayFrame = frame.Convert<Gray, Byte>();
var motionFrame = grayFrame.AbsDiff(backFrame)
                           .ThresholdBinary(new Gray(20), new Gray(255))
                           .Erode(2) 
                           .Dilate(2);
Run Code Online (Sandbox Code Playgroud)

结果: 演示差异

我不知道怎么把动作变成白色?

这是Blob代码:

Image<Bgr, Byte> smoothedFrame = new Image<Bgr, byte>(frame.Size);
CvInvoke.GaussianBlur(frame, smoothedFrame, new Size(3, 3), 1); //filter out noises

Mat forgroundMask = new Mat();
fgDetector.Apply(smoothedFrame, forgroundMask);

CvBlobs blobs = new CvBlobs();
blobDetector.Detect(forgroundMask.ToImage<Gray, byte>(), blobs);
blobs.FilterByArea(400, int.MaxValue);
blobTracker.Update(blobs, 1.0, 0, 1);

foreach (var pair in blobs) {
  CvBlob b = pair.Value;
  CvInvoke.Rectangle(frame, b.BoundingBox, new MCvScalar(255.0, 255.0, 255.0), 2);
}
Run Code Online (Sandbox Code Playgroud)

结果: Blob演示

为什么这么多误报?

这是一个MOG2代码:

forgroundDetector.Apply(frame, forgroundMask);
motionHistory.Update(forgroundMask);
var motionMask = GetMotionMask();
Image<Bgr, Byte> motionImage = new Image<Bgr, byte>(motionMask.Size);
CvInvoke.InsertChannel(motionMask, motionImage, 0);

Rectangle[] rects;
using (VectorOfRect boundingRect = new VectorOfRect()) {
  motionHistory.GetMotionComponents(segMask, boundingRect);
  rects = boundingRect.ToArray();
}

foreach (Rectangle comp in rects) { ...
Run Code Online (Sandbox Code Playgroud)

结果: MOG2演示

如果我选择最大的区域,我怎样才能获得物体的轮廓?

小智 7

首先,我可以举一些光流代码示例.

oldImage并且newImage是保持前一帧和当前帧的变量.在我的代码中,它是类型的Image<Gray, Byte>.

// prep containers for x and y vectors
Image<Gray, float> velx = new Image<Gray, float>(newImage.Size);
Image<Gray, float> vely = new Image<Gray, float>(newImage.Size);

// use the Horn and Schunck dense optical flow algorithm.
OpticalFlow.HS(oldImage, newImage, true, velx, vely, 0.1d, new MCvTermCriteria(100));

// color each pixel
Image<Hsv, Byte> coloredMotion = new Image<Hsv, Byte>(newImage.Size);
for (int i = 0; i < coloredMotion.Width; i++)
{
    for (int j = 0; j < coloredMotion.Height; j++)
    {
        // Pull the relevant intensities from the velx and vely matrices
        double velxHere = velx[j, i].Intensity;
        double velyHere = vely[j, i].Intensity;

        // Determine the color (i.e, the angle)
        double degrees = Math.Atan(velyHere / velxHere) / Math.PI * 90 + 45;
        if (velxHere < 0)
        {
            degrees += 90;
        }
        coloredMotion.Data[j, i, 0] = (Byte) degrees;
        coloredMotion.Data[j, i, 1] = 255;

        // Determine the intensity (i.e, the distance)
        double intensity = Math.Sqrt(velxHere * velxHere + velyHere * velyHere) * 10;
        coloredMotion.Data[j, i, 2] = (intensity > 255) ? 255 : intensity;
    }
}
// coloredMotion is now an image that shows intensity of motion by lightness
// and direction by color.
Run Code Online (Sandbox Code Playgroud)

关于如何删除前景的更大问题:

如果我有办法获得静态背景图像,这是最好的开始方式.然后,通过AbsDiff方法检测前景,并使用ErodeDilateGaussian平滑图像,然后使用斑点检测.

对于简单的前景检测,我发现光流处理过多(最大8fps),而AbsDiff方法同样准确,但对帧速率没有影响.

关于轮廓,如果您只是想查找大小,位置和其他时刻,那么上面的AbsDiff教程中的斑点检测似乎就足够了Image.FindContours(...).

如果没有,我将开始查看本教程中使用的CvBlobDetector类.有一个内置的DrawBlob功能,可能会派上用场.