OpenCVSharp 4.5 光流

用OpenCVSharp 4.5跑一遍 OpenCV官方教程

原OpenCV官方教程链接:OpenCV: Optical Flow

核心函数: cv.calcOpticalFlowPyrLK()cv.calcOpticalFlowFarneback()

using System;
using OpenCvSharp;

namespace ConsoleApp1
{
    class tutorial38 : ITutorial
    {
        public void Run()
        {
            string filename = @"I:\csharp\videos\slow_traffic_small.mp4";

            VideoCapture capture = new VideoCapture(filename);
            if (!capture.IsOpened())
            {
                //error in opening the video input
                Console.WriteLine("Unable to open file!");
                return;
            }

            // Create some random colors
            Scalar[] colors = new Scalar[100];
            RNG rng = new RNG(0x1fffff);
            for (int i = 0; i < 100; i++)
            {
                int r = rng.Uniform(0, 256);
                int g = rng.Uniform(0, 256);
                int b = rng.Uniform(0, 256);
                colors[i] = new Scalar(r, g, b);
            }
            Mat old_frame = new Mat(), old_gray = new Mat();


            // Take first frame and find corners in it
            capture.Read(old_frame);
            Cv2.CvtColor(old_frame, old_gray, ColorConversionCodes.BGR2GRAY);
            Point2f[] p0 = Cv2.GoodFeaturesToTrack(old_gray, 100, 0.3, 7, null, 7, false, 0.04);
            Point2f[] p1 = new Point2f[p0.Length];
            // Create a mask image for drawing purposes
            Mat mask = Mat.Zeros(old_frame.Size(), old_frame.Type());
            while (true)
            {
                Mat frame = new Mat(), frame_gray = new Mat();
                capture.Read(frame);
                if (frame.Empty())
                    break;
                Cv2.CvtColor(frame, frame_gray, ColorConversionCodes.BGR2GRAY);
                // calculate optical flow
                byte[] status;
                float[] err;

                TermCriteria criteria = new TermCriteria((CriteriaTypes.Count) | (CriteriaTypes.Eps), 10, 0.03);
                Cv2.CalcOpticalFlowPyrLK(old_gray, frame_gray, p0, ref p1, out status, out err, new Size(15, 15), 2, criteria);
                Point2f[] good_new = new Point2f[p0.Length];
                for (uint i = 0; i < p0.Length; i++)
                {
                    // Select good points
                    if (status[i] == 1)
                    {
                        good_new[i] = p1[i];
                        // draw the tracks
                        Cv2.Line(mask, (Point)p1[i], (Point)p0[i], colors[i], 2);
                        Cv2.Circle(frame, (Point)p1[i], 5, colors[i], -1);
                    }
                }
                Mat img = new Mat();
                Cv2.Add(frame, mask, img);
                Cv2.ImShow("Frame", img);
                int keyboard = Cv2.WaitKey(30);
                if (keyboard == 'q' || keyboard == 27)
                    break;
                // Now update the previous frame and previous points
                old_gray = frame_gray.Clone();
                p0 = good_new;
            }

        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值