OpenCVSharp 4.5 生成视频

用 OpenCVSharp 4.5 跑一遍 OpenCV官方教程

原官方教程链接:OpenCV: Creating a video with OpenCV

using System;
using OpenCvSharp;

namespace ConsoleApp1
{
    class tutorial35 : ITutorial
    {
        public void Run()
        {
            string inputVideofile = @"I:\csharp\videos\Megamind.avi";
            string outputVideofile = @"I:\csharp\videos\Megamind_output.avi";
            int channel = 2;  // Red Channel

            VideoCapture inputVideo = new VideoCapture(inputVideofile);
            if (!inputVideo.IsOpened())
            {
                Console.WriteLine("Could not open video file {0}", inputVideofile);
                return;
            }
            //string::size_type pAt = source.find_last_of('.');                  // Find extension point
            //const string NAME = source.substr(0, pAt) + argv[2][0] + ".avi";   // Form the new name with container

            int ex = (int)inputVideo.Get(VideoCaptureProperties.FourCC);// Get Codec Type- Int form
                                                                        // Transform from int to char via Bitwise operators

            char[] EXT = { (char)(ex & 0XFF), (char)((ex & 0XFF00) >> 8), (char)((ex & 0XFF0000) >> 16), (char)((ex & 0XFF000000) >> 24), (char)0 };

            Size S = new Size((int)inputVideo.Get(VideoCaptureProperties.FrameWidth),    // Acquire input size
                          (int)inputVideo.Get(VideoCaptureProperties.FrameHeight));

            VideoWriter outputVideo = new VideoWriter();                                        // Open the output
            outputVideo.Open(outputVideofile, ex, inputVideo.Get(VideoCaptureProperties.Fps), S, true);

            if (!outputVideo.IsOpened())
            {
                Console.WriteLine("Could not open the output video for write: {0}", outputVideofile);
                return;
            }
            Console.WriteLine("Input frame resolution: Width={0},Height={1}, of nr# {2}, Input Codec Type: {3}", S.Width, S.Height, inputVideo.Get(VideoCaptureProperties.FrameCount), EXT);

            Mat src = new Mat(), res = new Mat();
            Mat[] spl = new Mat[3];
            for (; ; ) //Show the image captured in the window and repeat
            {
                inputVideo.Read(src);              // read
                if (src.Empty()) break;         // check if at end
                Cv2.Split(src, out spl);                // process - extract only the correct channel
                for (int i = 0; i < 3; ++i)
                    if (i != channel)
                        spl[i] = Mat.Zeros(S, spl[0].Type());
                Cv2.Merge(spl, res);
                //outputVideo.write(res); //save or
                outputVideo.Write(res);
            }
            Console.WriteLine("Finished writing");
            return;
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值