Draw arrow from Open cv dense optical Flow data C#

178 Views Asked by At

I want to draw arrows on my image from a dense Optical Flow data. I use OpenCVSharp.

public Mat GetDense(Mat nextFrameGray, Mat nextFrameColor, Mat lastgrayFrame)
        {
            Mat flow = new(lastgrayFrame.Size(), MatType.CV_32FC2);
            Cv2.CalcOpticalFlowFarneback(lastgrayFrame, nextFrameGray, flow, 0.5, 3, 15, 3, 5, 1.2, 0);

            return flow;
        }    
            Mat denseFrame = GetDense(grayFrame, colorFrame, lastgrayFrame);

            // visualization
            Mat[] flow_parts = new Mat[2];
            Cv2.Split(denseFrame, out flow_parts);
            //Magnétude
            Mat magnitude = new Mat(), angle = new Mat(), magn_norm = new Mat();
            Cv2.CartToPolar(flow_parts[0], flow_parts[1], magnitude, angle, true);
            Cv2.Normalize(magnitude, magn_norm, 0.0, 1.0, NormTypes.MinMax);

I get the Mat dense frame which contains the Optical and I can't get this result...

Result I would like to have

I try that and this does'nt work...

 for (int x = 0; x < colorFrame.Width; x+= 20)
            {
                for (int y = 0; y < colorFrame.Height; y += 20)
                {
                    //Cv2.ArrowedLine(colorFrame, new Point(x, y), new Point(x+2,y+2), Scalar.Green, 2);
                    //Cv2.ArrowedLine(colorFrame, flow_parts[0].At<Point>(x,y), flow_parts[1].At<Point>(x, y), Scalar.Green, 2);
                    Debug.WriteLine(flow_parts[0].At<Point>(x, y).ToString() + " | " + flow_parts[1].At<Point>(x, y).ToString());
                }
            }

Is there any other way to do this ? If not how to make this code work ?

Thanks

0

There are 0 best solutions below