美文网首页
利用opencv合成视频

利用opencv合成视频

作者: 此间不留白 | 来源:发表于2021-03-13 15:46 被阅读0次

    《learning openCV》ex8-1解决方案

    有以下任务:

    1. 读取一个视频的所有帧
    2. 将所有帧转换为灰度图像
    3. 利用Canny算法对所有帧实现边缘检测
    4. 将转换后的视频在一个窗口显示,其中,窗口长度为原视频宽度的3倍,高度不变,并在每个视频上方标出基本的视频信息。

    针对以上任务,利用c++实现代码如下所示:

    
    #include<opencv2/opencv.hpp>
    #include<opencv2/highgui.hpp>
    #include<opencv2/highgui/highgui_c.h>
    #include<iostream>
    #include"putText.h"
    using namespace cv;
    using namespace std;
    
    /*
    1. create a program that
        (1) reads frames from a video
        (2) turns the result to grayscale
        (3) performs Canny edge detection on the image.
      Display all three stages of processing in three windows,with each window appropriately named for its function
    
      a. Display all three stages of processing in one image.(Hint:create another image of the same height but three times
        the windth as the video frame.Copy the images to this,either by using pointers or(more cleverly) by creating
        three new image headers that point to the beginning of and to one-third and two-thirds of the way into imageData.
        Then use Mat::CopyTo().)
      b. Write appropriate text labels describing the processing in each of the three slots.
            
    
    
    */
    // turn result to grayscale;
    
    
    
    int main()
    {
        //1. 读取视频帧,显示在一个窗口上
        VideoCapture cap = VideoCapture("stellaris.mp4");
        if (!cap.isOpened())
        {
            cerr << "视频读取错误!" << endl;
            return 0;
        }
        int frame_nums = static_cast<int>(cap.get(CAP_PROP_FRAME_COUNT)); //视频帧总数
        int currentFrame = static_cast<int>(cap.get(CAP_PROP_POS_FRAMES)); //记录当前视频帧的位置
        double fps = static_cast<double>(cap.get(CAP_PROP_FPS)); //获取视频帧率
        
        int frame_width = static_cast<int>(cap.get(CAP_PROP_FRAME_WIDTH)); //记录视频帧的宽度
        int frame_height = static_cast<int>(cap.get(CAP_PROP_FRAME_HEIGHT)); //记录视频帧的高度
    
        VideoWriter writer;
        writer.open("out.mp4", writer.fourcc('m', 'p', '4', 'v'), fps, Size(frame_width, static_cast<int>(frame_height * 0.6)), -1);
        
        double t = 0;
        Mat frame;   //记录当前帧
        Mat frame_gray; //灰度变化
        Mat frame_canny; //边缘检测
        while (cap.read(frame))
        {
            /*
            t = (double)getTickCount();
            cap.grab();
            cap.retrieve(frame);  //读取帧
            */
    
    
            if (frame.empty())
            {
                break;
            }
            
            
            
            //imshow("raw video", frame); //显示原始视频
            //waitKey(3);
            cvtColor(frame, frame_gray, CV_RGB2GRAY);
            //imshow("gray video", frame_gray); //显示灰度变化后的视频
            //waitKey(3);
            Canny(frame_gray, frame_canny, 80, 255);
            //imshow("canny video", frame_canny);  //显示边缘检测后的视频
            //waitKey(3);
    
            //合成3个视频在一个子窗口显示
            Mat MatShow = Mat(frame.rows, 3*frame.cols, CV_8UC3, Scalar(0, 0, 0));
            
            //重新变为3个通道
            Mat frame_gray_show;
            Mat frame_canny_show;
            cvtColor(frame_gray, frame_gray_show, CV_GRAY2BGR);
            cvtColor(frame_canny, frame_canny_show, CV_GRAY2BGR);
    
    
            Mat MatSub = MatShow.colRange(0,frame.cols);
            frame.copyTo(MatSub);
            //cout << MatShow.size() << endl;
            MatSub = MatShow.colRange(frame.cols, 2*frame.cols);
            frame_gray_show.copyTo(MatSub);
            MatSub= MatShow.colRange(2*frame.cols, 3*frame.cols);
            frame_canny_show.copyTo(MatSub);
            
            putTextZH(MatShow, "高维入侵", Point(frame.cols/2, 50), Scalar(255, 255, 0), 38, "华文楷体");
            putTextZH(MatShow, "虚空恶魔", Point(static_cast<int>(1.5*frame.cols), 50), Scalar(255, 255, 0), 38, "华文楷体");
            putTextZH(MatShow, "肃正协议", Point(static_cast<int>(2.5 * frame.cols),50), Scalar(255, 255,0), 38, "华文楷体");
            Mat Mat_resize;
            resize(MatShow,Mat_resize,Size(frame_width, static_cast<int>(frame_height*0.6)));
            
            writer.write(Mat_resize);
            //imshow("hello", Mat_resize);
            cv::waitKey(3);
    
    
        }
        cv::waitKey(0);
        writer.release();
        
        cap.release();
        
        return 0;
    }
    
    

    最后实现效果如下图所示:


    合成视频效果观看

    相关文章

      网友评论

          本文标题:利用opencv合成视频

          本文链接:https://www.haomeiwen.com/subject/sduxcltx.html