demo/2023816/main.cpp

177 lines
3.9 KiB
C++

#include<opencv2/opencv.hpp>
#include <opencv2/highgui/highgui_c.h>
#include<iostream>
#include "tuneParameter.h"
#include "preprocessing.h"
#include "bufferTracker.h"
#include "PnP.h"
using std::cout;
using std::endl;
////暂停 获取ROI处理图像
//cv::Mat rawImages;
//cv::Rect roiRect;
//cv::Mat ROI;
//cv::Point startPoint;
//cv::Point endPoint;
//bool downFlag = false;
//bool upFlag = false;
//
//void MouseEvent(int event, int x, int y, int flags, void* data)
//{
//
// if (event == cv::EVENT_LBUTTONDOWN)
// {
// downFlag = true;
// startPoint.x = x;
// startPoint.y = y;
// }
//
// if (event == cv::EVENT_LBUTTONUP)
// {
// upFlag = true;
// endPoint.x = x;
// endPoint.y = y;
// }
//
// if (downFlag == true && upFlag == false)
// {
// cv::Point tempPoint;
// tempPoint.x = x;
// tempPoint.y = y;
//
// cv::Mat tempImage = rawImages.clone();
// rectangle(tempImage, startPoint, tempPoint, cv::Scalar(255, 0, 0), 2, 3, 0);
// imshow("ROIing", tempImage);
// }
//
// if (downFlag == true && upFlag == true)
// {
//
// roiRect.width = abs(startPoint.x - endPoint.x);
// roiRect.height = abs(startPoint.y - endPoint.y);
// roiRect.x = min(startPoint.x, endPoint.x);
// roiRect.y = min(startPoint.y, endPoint.y);
// cout << roiRect.width << " " << roiRect.height << " " << roiRect.x << " " << roiRect.y << endl;
// cv::Mat ROI(rawImages, roiRect);
// imshow("ROI", ROI);
// downFlag = false;
// upFlag = false;
// }
// cv::waitKey(0);
//
//}
/// <summary>
/// 主函数
/// </summary>
/// <returns></returns>
int main() {
std::basic_string<char> path = "./vedio/blue.mp4";
cv::VideoCapture cap(path);
bool paused = 0;
//相机参数
cv::Mat cameraMatrix;
cv::Mat distCoeffs;
cv::FileStorage fs("./out_camera_data.xml", cv::FileStorage::READ);
fs["camera_matrix"] >> cameraMatrix;
fs["distortion_coefficients"] >> distCoeffs;
if (cap.isOpened()) {
cv::Rect roiRect;
cv::Mat rawImages, ROI, mask;
PreviousFrameInfo previousFrameInfo;
std::chrono::high_resolution_clock::time_point previousFrameEndTime;
while (true) {
//判断上一帧的相关信息
if (previousFrameInfo.firstFrame) {
previousFrameEndTime = std::chrono::high_resolution_clock::now();
previousFrameInfo.firstFrame = false;
}
else {
auto t = std::chrono::high_resolution_clock::now();
previousFrameInfo.elapsedTime = std::chrono::high_resolution_clock::now() - previousFrameEndTime;
previousFrameEndTime = t;
}
vector<cv::Point2f> targetPoints;
//进行视频资源的读取
if (!paused) {
cap >> rawImages;
//暂停视频
if (rawImages.empty())
break;
//截取部分区域进行处理
roiRect.width = 605;
roiRect.height = 500;
/*roiRect.x = 154;
roiRect.y = 176;*/
roiRect.x = 125;
roiRect.y = 80;
cv::Mat ROI(rawImages, roiRect);
//图像预处理
mask = preprocessing(ROI);
//寻找轮廓、确定击打点
targetPoints = findTarget(ROI, previousFrameInfo, mask);
//预测 等待时机...啊...耐心
//位姿解算
PNP(ROI, targetPoints, cameraMatrix, distCoeffs);
cv::namedWindow("rawImages", 0);
//resizeWindow("rawImages", 741, 620);
imshow("rawImages", ROI);
cv::waitKey(1);
}
// 按q退出循环、按空格暂停视频进行调节
char key = cv::waitKey(1);
if (key == 'q')
break;
else if (key == ' ') {
paused = !paused;
////截取感兴趣的区域
//namedWindow("ROIing");
//resizeWindow("ROIing", 741, 620);
//imshow("ROIing", rawImages);
//setMouseCallback("ROIing", MouseEvent, 0);
//waitKey(0);
////截取部分区域进行处理
//roiRect.width = 605;
//roiRect.height = 500;
///*roiRect.x = 154;
//roiRect.y = 176;*/
//roiRect.x = 125;
//roiRect.y = 80;
//Mat ROI(rawImages, roiRect);
////调参
//tuneParameter(ROI);
}
}
cout << "视频图像处理完成" << endl;
//释放视频资源
cap.release();
}
else
cout << "视频图像读取不成功" << endl;
return 0;
}