飞道的博客

C++ OpenCV【视频合并:多个图像拼接在一张图像】

423人阅读  评论(0)

提示:本文中视频拼接指的是将多张图像按空间合并在一张图像上,而不是将多张图像按时间顺序拼接成一个多帧片段。

文章目录

前言

一、OpenCV知识点

1.OpenCV裁剪矩形区域赋值 

2.OpenCV将Mat粘贴到指定位置 

二、程序样例

1.程序源码

2.运行结果


前言

C++版拼接视频不如Python版简洁。

Python版:Python OpenCV【视频合并:多个图像拼接在一张图像】_IT.Husky的博客-CSDN博客

为了实现C++版拼接视频,首先复习一下OpenCV相关知识。


一、OpenCV知识点

1.OpenCV裁剪矩形区域赋值 


  
  1. Mat img; //读取图像
  2. cv::Rect rect(x, y, WIDTH, HEIGHT); //定义坐标(x,y)为矩形的左上角起点,宽高为WIDTH和HEIGHT的矩形
  3. Mat tmp = img(rect); //将img上矩形框住的部分赋给tmp

2.OpenCV将Mat粘贴到指定位置 


  
  1. cv::Mat src; //读取图像
  2. cv::Mat image; //定义一个存放图像的Mat
  3. cv::Rect rect(0, 0, src.cols, src.rows); //定义坐标(0,0)为矩形的左上角起点,宽高为src.cols, src.rows的矩形
  4. src. copyTo( image(rect)); //把src的内容复制到image指定的Rect框中

二、程序样例

1.程序源码

代码如下(带注释):


  
  1. #include <iostream>
  2. #include <opencv.hpp>
  3. using namespace std;
  4. int main()
  5. {
  6. //读视频
  7. cv::VideoCapture videoLeftUp("kll.mp4");
  8. cv::VideoCapture videoRightUp("kll.mp4");
  9. cv::VideoCapture videoLeftDown("kll.mp4");
  10. cv::VideoCapture videoRightDown("kll.mp4");
  11. int fps = videoLeftUp. get( 5);
  12. cv::Size size = cv:: Size(videoLeftUp. get( 3), videoLeftUp. get( 4));
  13. //写视频
  14. cv::VideoWriter writer("kllHeBing.mp4", CV_FOURCC('m', 'p', '4', 'v'), fps, size, true);
  15. cv::Mat frameLeftUp, frameRightUp, frameLeftDown, frameRightDown;
  16. //读视频帧
  17. while (videoLeftUp. read(frameLeftUp) && videoRightUp. read(frameRightUp) &&
  18. videoLeftDown. read(frameLeftDown) && videoRightDown. read(frameRightDown))
  19. {
  20. //调整帧尺寸
  21. cv::Mat frameLeftUpNew, frameRightUpNew, frameLeftDownNew, frameRightDownNew;
  22. resize(frameLeftUp, frameLeftUpNew, cv:: Size(frameLeftUp.cols / 2, frameLeftUp.rows / 2));
  23. resize(frameRightUp, frameRightUpNew, cv:: Size(frameRightUp.cols / 2, frameRightUp.rows / 2));
  24. resize(frameLeftDown, frameLeftDownNew, cv:: Size(frameLeftDown.cols / 2, frameLeftDown.rows / 2));
  25. resize(frameRightDown, frameRightDownNew, cv:: Size(frameRightDown.cols / 2, frameRightDown.rows / 2));
  26. //新建Mat 用于图像合并
  27. cv::Mat frameOut = cv::Mat:: zeros(frameLeftUp.rows, frameLeftUp.cols, frameLeftUp. type());
  28. //定义图像放的位置
  29. cv::Rect rectLeftUp = cvRect( 0, 0, frameLeftUpNew.cols, frameLeftUpNew.rows);
  30. cv::Rect rectRightUp = cvRect(frameRightUpNew.cols, 0, frameRightUpNew.cols, frameRightUpNew.rows);
  31. cv::Rect rectLeftDown = cvRect( 0, frameLeftDownNew.rows, frameLeftDownNew.cols, frameLeftDown.rows);
  32. cv::Rect rectRightDown = cvRect(frameRightDownNew.cols, frameRightDownNew.rows, frameRightDownNew.cols, frameRightDownNew.rows);
  33. //将调整尺寸的帧图像放置新建Mat的指定位置
  34. frameLeftUpNew. copyTo( frameOut(rectLeftUp));
  35. frameRightUpNew. copyTo( frameOut(rectRightUp));
  36. frameLeftDownNew. copyTo( frameOut(rectLeftDown));
  37. frameRightDownNew. copyTo( frameOut(rectRightDown));
  38. /*cv::imshow("frameOut.jpg", frameOut);
  39. cv::waitKey(0);*/
  40. //写入帧图像
  41. writer. write(frameOut);
  42. }
  43. //释放内存
  44. writer. release();
  45. videoLeftUp. release();
  46. videoRightUp. release();
  47. videoLeftDown. release();
  48. videoRightDown. release();
  49. system( "pause");
  50. return 0;
  51. }

2.运行结果

源视频图像首帧:

拼接视频图像首帧:

留作业:请尝试以下合并结果↓


转载:https://blog.csdn.net/Gary_ghw/article/details/128144070
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场