开发者

EmguCV - Motion detection not returning angles

I am runnng the motion detection algorithm against a video (file) and following the code sample motion detection, and trying to find the angle of each component and the overall motion. I do get a motion value back, with blobs etc., but the motion direction of each component always is always 0 degrees or 360 degrees and make no sense. What could I be doing wrong? Please help, thanks.

This is the constructor

_motionHistory = new MotionHistory(
                                              10.0, //in second, the duration of motion history you wants to keep
                                              0.05, //in second, parameter for cvCalcMotionGradient
                                              0.5); //in second, parameter for cvCalcMotionGradient

The following is the code for looping through the motion components:

foreach (MCvConnectedComp comp in motionComponents)
                    {
                        //reject the components that have small area;
                        if (comp.area < 1) continue;

                        // find the angle and motion pixel count of the specific area
                            double angle, motionPixelCount;
                            _motionHistory.MotionInfo(comp.rect, out angle, out motionPixelCount);

                            string motion_direction = GetMotionDescriptor(comp.rect);
Console.writeline (motion_direction);


                    }

                    // find and draw the overall motion angle
                    double overallAngle, overallMotionPixelCount;
                    _motionHistory.MotionInfo(motionMask.ROI, out overallAngle, out overallMotionPixelCount);

And this where I get my motion descriptor angle

private string GetMotionDescriptor(Rectangle motionRegion)
        {
            float circleRadius = (motionRegion.Width + motionRegion.Height) >> 2;
            Point center = new Point(motionRegion.X + motionRegion.Width >> 1, motionRegion.Y + motionRegion.Height >> 1);

            int xDirection = (int)(Math.Cos(angle * (Math.PI / 180.0)) * circleRadius);
           开发者_如何转开发 int yDirection = (int)(Math.Sin(angle * (Math.PI / 180.0)) * circleRadius);
            //double movementAngle = Math.Atan(xDirection / yDirection) * 180 / Math.PI;
            Point pointOnCircle = new Point(center.X + xDirection, center.Y - yDirection);
            double slope = (double)(pointOnCircle.Y - center.Y)/(double)(pointOnCircle.X - center.X);
            double ang = Math.Atan(slope) * 180/Math.PI;
            return (ang).ToString() + " degrees";
        }


aha! I figured out the reason and posting here if anyone is running into the same problem. The

_motionHistory = new MotionHistory(mhi, maxDelta, minDelta); 

Should be adjusted to the frame rate and the motion. The trick lies in the 3 params (1) motion history to keep, (2) max time delta, (3) min time delta.

They need to be adjusted in some way to reflect the motion you wish to capture. Hope that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜