C#使用OpenCvSharp4库读取本地视频

C#使用OpenCvSharp4库读取本地视频

OpenCvSharp4库

OpenCvSharp4库是一个基于.Net封装的OpenCV库,Github源代码地址为:https://github.com/shimat/opencvsharp,里面有关于Windows下安装OpenCvSharp4库的描述,如下图所示:
C#使用OpenCvSharp4库读取本地视频_第1张图片

视频资源地址

视频资源可以到https://github.com/murtazahassan/Learn-OpenCV-in-3-hours/blob/master/Resources/test_video.mp4下载

新建一个C# .Net控制台程序OpenCVExample,并安装OpenCvSharp4和OpenCvSharp4.runtime.win两个NuGet包,或者直接安装OpenCvSharp4.Windows

C#使用OpenCvSharp4库读取本地视频_第2张图片

C#示例代码

C#示例代码如下,如下代码展示了怎么使用VideoCapture和Cv2.ImShow读取本地视频:

using OpenCvSharp;
using System;

namespace OpenCVExample
{
    public class Program
    {
        /// 
        ///  读取本地视频并逐帧显示
        /// 
        public static void ReadLocalVideo()
        {
            VideoCapture cap = new VideoCapture("Resources\\test_video.mp4");   // 读取本地视频文件
            if (!cap.IsOpened())
            {
                Console.WriteLine("VidepCapture open failed");
                return;
            }
            while (true)
            {    // 如果cap初始化成功
                Mat frame = new Mat();
                if (cap.Read(frame))    // 抓取和解码,返回下一帧
                {
                    Cv2.ImShow("video Result", frame);
                    if (Cv2.WaitKey(1) == (int)('q'))   // 用户输入q键退出
                    {
                        break;
                    }
                }
                else
                {
                    // 当视频播放完毕,也退出
                    Console.WriteLine("there is no frame to read");
                    break;
                }

            }
        }

        static void Main(string[] args)
        {
            ReadLocalVideo();
           
            //Console.ReadKey();
        }
    }
}

运行结果

程序运行结果如下图所示:
C#使用OpenCvSharp4库读取本地视频_第3张图片

参考资料

  • https://github.com/shimat/opencvsharp
  • LEARN OPENCV C++ in 4 HOURS | Including 3x Projects | Computer Vision
  • Learn-OpenCV-cpp-in-4-Hours
  • LEARN OPENCV in 3 HOURS with Python | Including 3xProjects | Computer Vision
  • Learn-OpenCV-in-3-hours

你可能感兴趣的:(C#,WinForm和WPF,OpenCV,编程语言学习,c#,音视频,开发语言)