#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/features2d/features2d.hpp>
#include<opencv2/nonfree/nonfree.hpp>
#include<opencv2/calib3d/calib3d.hpp>
#include<opencv2/stitching/stitcher.hpp>
#include<iostream>
#include<vector>
#include<Windows.h>
#include<sstream>
#include<time.h>
#include<fstream>
#include<math.h>
using namespace cv;
using namespace std;
//定义全局变量
Rect pre_Rect;//定义初始帧的框
int shrink = 4;//图片帧缩小倍数
int frame_num = 1;//记录循环帧数
ofstream recordFile("E:\\软件所资料\\科创项目材料\\图片\\all_pic3(1.2)\\纪录处理时间.txt");//创建记录文件
//运动物体检测函数声明
Mat MoveDetect(Mat temp, Mat frame);
//基于特征点匹配的图像拼接
void stitch(Mat& srcImage1, Mat& srcImage2, Mat& panorama);
//读入视频帧 调用stitch2函数进行拼接
void readVideo(string videoName);
//求矩形的中心点
Point getCenterPoint(Rect rect);
//获取两点间的距离
double getDistance(Point p1, Point p2);
//手动标框
void onMouse(int event, int x, int y, int, void *);
int main(int argc, char* argv[]) {
//pre_Rect = Rect(Point(1050, 520), Point(1160, 580));//自己设定的初始矩形框
pre_Rect = Rect(Point(260, 130), Point(300, 150));//自己设定的初始矩形框
readVideo("E:\\软件所资料\\录制视频\\test.mov");
return 0;
}
//读入视频帧 调用stitch函数进行拼接
void readVideo(string videoName) {
VideoCapture videoCap(videoName);
if (!videoCap.isOpened()) {
exit;
}
Mat preFrame;//先前帧
Mat nowFrame;//当前帧
Mat stichFrame;//拼接帧
Mat resultFrame;//结果帧
//函数回调 鼠标事件
namedWindow("result", 1);
setMouseCallback("result", onMouse, 0);
clock_t start, finish, all_start, all_finish;//计算程序运行时间
double processTime, totalTime = 0;
videoCap >> preFrame;
GaussianBlur(preFrame, preFrame, Size(5, 5), 0, 0);//高斯滤波器去除图像中噪点
resize(preFrame, preFrame, Size(preFrame.cols / shrink, preFrame.rows / shrink));
//imshow("first", preFrame);
//int count = 1;
while (true) {
all_start = clock();//纪录总的开始时间
videoCap >> nowFrame;
GaussianBlur(nowFrame, nowFrame, Size(5, 5), 0, 0);//高斯滤波器去除图像中噪点
if (nowFrame.empty()) {
recordFile << "平均处理时间:" << totalTime / frame_num << "ms" << "\n" << "\n";
recordFile.close();
break;
}
resize(nowFrame, nowFrame, Size(nowFrame.cols / shrink, nowFrame.rows / shrink));
recordFile << frame_num++ << "\n";
Mat temp;
start = clock();
stitch(nowFrame, preFrame, temp);
finish = clock();
recordFile << "拼接用时:" << finish - start << "ms" << "\n";
if (temp.empty()) {
stichFrame = nowFrame;
}
else {
stichFrame = temp;
}
start = clock();
resultFrame = MoveDetect(nowFrame, stichFrame);
finish = clock();
recordFile << "减差和检测用时:" << finish - start << "ms" << "\n";
all_finish = clock();//纪录总的结束时间
processTime = all_finish - all_start;
recordFile << "共运行:" << processTime << "ms" << "\n" << "\n";
totalTime += processTime;
//拼接字符串
String imageName = "E:\\软件所资料\\科创项目材料\\图片\\all_pic3(1.2)\\第";//字符串检测图片名称
//数字转换成字符串
ostringstream strCount;
strCount << frame_num;
string str = strCount.str();
imageName.append(str);
imageName.append("帧图片.jpg");
imwrite(imageName, resultFrame);
imshow("result", resultFrame);
waitKey(1);
preFrame = nowFrame.clone();//当前帧nowFrame赋给上一帧preFrame;
}
}
//基于特征点匹配的图像拼接
void stitch(Mat& srcImage1, Mat& srcImage2, Mat& panorama) {
clock_t start;
clock_t finish;
start </