现我们成立了梦龙工作室,工作室成员皆为985在读理工科学生,排名前20%,5%等,获得过国赛一等奖,大数据杯二等奖,美赛M奖,以一作发表ei会议,SCI二区等。旨在帮助大学生参与竞赛,如mathorcup 大数据杯,国赛,美赛,电工杯等等,入群可提问比赛或数据处理有关问题,群内开放讨论,欢迎有兴趣的朋友加群!群号:684715652
利用opencv库录制九宫格视频(C++实现)
在项目开始之前,我的环境已配置完成,具体环境如何配置可参考网络教程。下面我们开始项目的实现
库的导入
#include<iostream>
#include<opencv2/opencv.hpp>
#include<string.h>
using namespace std;
using namespace cv;
这就不多说了
开启摄像头
Mat frame;
Mat newframe;
string outputVideoPath = "F:\\C++language\\robocon.avi";
VideoCapture capture(0);
int frame_width = capture.get(CAP_PROP_FRAME_WIDTH);
int frame_height = capture.get(CAP_PROP_FRAME_HEIGHT);
VideoWriter writer;
开始摄像头,并获取摄像头的像素高度与宽度
定义所需变量
int num = 3;//原图片长宽皆被划分为三份,共划分成九份
int stepwidth;//划分后单个图片的宽度
int stepheight;//划分后的那个图片的高度
int space = 5;//九宫格中每张图片的间隔
捕获图片并生成视频
capture >> frame;
stepwidth = frame.cols / num;
stepheight = frame.rows / num;
resize(frame, frame, Size(stepwidth * num, stepheight * num), 1, 1, INTER_LINEAR);
newframe = Mat(Size(frame.cols + (num - 1) * space, frame.rows + (num - 1) * space), CV_8UC3, Scalar(255, 255, 255));//新画布的生成
writer.open(outputVideoPath, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'), 10, Size(frame.cols + (num - 1) * space, frame.rows + (num - 1) * space));
if (!capture.isOpened())
{
cout << "The camera cannot be opened" << endl;
}
if (!writer.isOpened())
{
cout << "The video cannot be saved" << endl;
}
根据九宫格各张图片以及间隔的大小生成新的画布,用于存放新的九宫格图片
实现图片的抓取、转换与保存
int count = 1;
while (count <= 60)
{
capture >> frame;
stepwidth = frame.cols / num;
stepheight = frame.rows / num;
resize(frame, frame, Size(stepwidth * num, stepheight * num), 1, 1, INTER_LINEAR);
Mat newframe = Mat(Size(frame.cols + (num - 1) * space, frame.rows + (num - 1) * space), CV_8UC3, Scalar(255, 255, 255));
int i = 0;
int j = 0;
for (i = 0; i < num; i++)
{
for (j=0; j < num; j++)
{
int x = stepwidth * j;
int y = stepheight * i;
frame(Rect(x, y, stepwidth, stepheight)).copyTo(newframe(Rect(x + space * j, y + space * i, stepwidth, stepheight)));
}
}
imshow("output", newframe);
waitKey(100);
writer << newframe;
count += 1;
}
}
视频以10帧的形式呈现,共60帧图片。