opencv两视频合并

目题

  • opencv两视频合并
    • 代码
    • 编译
    • 运行

opencv两视频合并

实现两视频合并功能。

代码

mergeVideoo.cpp :

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 



#include 

#include 

#include 

using namespace std;

using namespace cv;







int main(int argc, char ** argv)

{



    std::string videoFile = "top_view.mp4";//视频的路径

    std::string videoFile1 = "fusion_view.mp4";//视频的路径

    

    /** 打开第一个视频文件 */

    VideoCapture cap; //视频句柄变量

    cap.open(videoFile);//打开视频

    if(!cap.isOpened())  //判断是否打开了

    {  

        printf("cap.isOpened is error\n");

        return -1;  

    } 

    

    /** 打开第二个视频文件 */

    VideoCapture cap1; //视频句柄变量

    cap1.open(videoFile1);//打开视频

    if(!cap1.isOpened())  //判断是否打开了

    {  

        printf("cap.isOpened is error\n");

        return -1;  

    } 

    

    

    /** 打开第一个视频文件的帧数 */

    int frame_num = cap.get(cv::CAP_PROP_FRAME_COUNT);

	std::cout << "videoFile total frame number is: " << frame_num << std::endl;

	/** 打开第二个视频文件的帧数 */

	int frame_num1 = cap1.get(cv::CAP_PROP_FRAME_COUNT);

	std::cout << "videoFile1 total frame number is: " << frame_num1 << std::endl;

	

	/** 打开第一个视频文件的帧率 */

    int fps = cap.get(cv::CAP_PROP_FPS);

	std::cout << "videoFile fps: " << fps << std::endl;

	/** 打开第二个视频文件的帧率 */

	int fps1 = cap1.get(cv::CAP_PROP_FPS);

	std::cout << "videoFile1 fps1: " << fps1 << std::endl;

	

	

	 /** 打开第一个视频文件的宽度 */

    int image_width = cap.get(cv::CAP_PROP_FRAME_WIDTH);

	std::cout << "videoFile image width is: " << image_width << std::endl;

	/** 打开第二个视频文件的宽度 */

	int image_width1 = cap1.get(cv::CAP_PROP_FRAME_WIDTH);

	std::cout << "videoFile1 image width is: " << image_width1 << std::endl;

	

	/** 打开第一个视频文件的高度 */

    int image_height = cap.get(cv::CAP_PROP_FRAME_HEIGHT);

	std::cout << "videoFile image height: " << image_height << std::endl;

	/** 打开第二个视频文件的高度 */

	int image_height1 = cap1.get(cv::CAP_PROP_FRAME_HEIGHT);

	std::cout << "videoFile1 image height: " << image_height1 << std::endl;

	



    /** 打开第一个视频文件的矩阵对象的格式*/

     int frame_format = cap.get(cv::CAP_PROP_FORMAT);

	std::cout << "videoFile frame format: " << frame_format << std::endl;

	/** 打开第二个视频文件的矩阵对象的格式 */

	int frame_format1 = cap1.get(cv::CAP_PROP_FORMAT);

	std::cout << "videoFile1 image height: " << frame_format1 << std::endl;



	

	/** 合并视频初始化 */

	std::string mergeVideooFile = "mergeVideo.avi";

	int mergeVideooHeight = 600;

	int mergeVideooWidth = 1250;

	float mergeVideooFps = 10.0;

	int mergeVideooFpsFrameFormat = CV_8UC3;

	

	/*

	mergeVideooHeight = image_height + image_height1 + 10;

	mergeVideooWidth = image_width + image_width1 + 10;

	mergeVideooFps = fps;

	if(fps1>fps)

	    mergeVideooFps = fps1;

    mergeVideooFps = 10;

    mergeVideooFpsFrameFormat = frame_format;

	*/

	cv::VideoWriter track_writer;

    Mat img = cv::Mat::zeros(mergeVideooWidth, mergeVideooHeight, mergeVideooFpsFrameFormat);

    track_writer.open (mergeVideooFile, cv::VideoWriter::fourcc('M', 'P', '4', '2'), mergeVideooFps, cv::Size(mergeVideooWidth, mergeVideooHeight));

    if(!track_writer.isOpened())

    {

        assert("track writer open failed!\n");

    }

    

    





    Mat frame; 

    Mat frame1;

   

    while(1)

    {

        cap.read(frame);//从第一个视频获取一帧图片

        cap1.read(frame1);//从第二个视频获取一帧图片

        if(frame.empty()) break; //是否加载成功





        //std::cout << "Width : " << frame1.cols << std::endl;

        //std::cout << "Height: " << frame1.rows << std::endl;

        // std::cout << "Width : " << frame.cols << std::endl;

        //std::cout << "Height: " << frame.rows << std::endl;

        //cout << "======================"  << endl;

        

        

        // 设定ROI区域:截取一部分进行合并

        Mat imageROI= frame(Rect(400,0,400,600));

        Mat imageROI1= frame1(Rect(200,0,850,450));

       

        // 大小转换

        Mat imageROI1dst = Mat::zeros(600,850 , CV_8UC3); //我要转化为850*600大小的

	    resize(imageROI1, imageROI1dst, imageROI1dst.size());

   

      

        // 视频写字

        putText(imageROI1dst, videoFile, Point(5, 55),FONT_HERSHEY_PLAIN,2.0,(0, 255, 255),2);

        putText(imageROI, videoFile1, Point(5, 55),FONT_HERSHEY_PLAIN,2.0,(0, 255, 255),2);

      

         

        //创建目标Mat

        Mat des;

        des.create(mergeVideooHeight,mergeVideooWidth, imageROI1dst.type());

        // 视频帧合并

        Mat r1 = des(Rect(0, 0, 850, 600));

        imageROI1dst.copyTo(r1);

        Mat r2 = des(Rect(850, 0, 400, 600));

        imageROI.copyTo(r2);

        

        // 格式化要保存的视频帧

        cv::resize(des, img , cv::Size(mergeVideooWidth, mergeVideooHeight));

        // 保存视频

        track_writer.write(img);

        

        

        

        // 可视化

        imshow("mergeVideoo",des);

        if (cv::waitKey(30) == 'q')//可以用开控制播放或者获取图片的速度,毫秒级别

		{

			break;

		}

    }

    

    

    destroyWindow("mergeVideoo");//关闭窗口bb;

    destroyAllWindows();//关闭所有的窗口

    cap.release();//释放视频句柄

    

    track_writer.release();

	return 0;

}

编译

$ g++ mergeVideo.cpp -o mergeVideo$(pkg-config --cflags --libs opencv)

运行

$ ./mergeVideo

你可能感兴趣的:(c++,opencv)