#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/videoio.hpp>
int main() {
// 打开主视频文件
cv::VideoCapture mainVideo("main_video.mp4");
if (!mainVideo.isOpened()) {
std::cerr << "无法打开主视频文件" << std::endl;
return -1;
}
// 打开画中画视频文件
cv::VideoCapture pipVideo("pip_video.mp4");
if (!pipVideo.isOpened()) {
std::cerr << "无法打开画中画视频文件" << std::endl;
return -1;
}
cv::Mat frameMain, framePIP;
while (true) {
// 读取主视频帧
if (!mainVideo.read(frameMain)) {
std::cerr << "无法读取主视频帧" << std::endl;
break;
}
// 读取画中画视频帧
if (!pipVideo.read(framePIP)) {
std::cerr << "无法读取画中画视频帧" << std::endl;
break;
}
// 调整画中画视频帧大小以匹配主视频窗口大小
cv::resize(framePIP, framePIP, cv::Size(frameMain.cols / 3, frameMain.rows / 3));
// 将画中画视频帧叠加到主视频帧上
cv::Rect roi(cv::Point(10, 10), framePIP.size());
framePIP.copyTo(frameMain(roi));
// 显示合成后的帧
cv::imshow("Picture in Picture", frameMain);
// 检查按键
char key = cv::waitKey(25);
if (key == 27) { // ESC键退出
break;
}
}
// 释放资源
mainVideo.release();
pipVideo.release();
cv::destroyAllWindows();
return 0;
}
使用opencv实现画中画Demo
于 2024-04-09 10:23:56 首次发布