-
Notifications
You must be signed in to change notification settings - Fork 0
/
undistortVideo.h
95 lines (81 loc) · 2.84 KB
/
undistortVideo.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* Video Undistort Software using OpenCV
*
* This program reads predefined camera parameters from file, and undistort a video file using it
*
* Published for WalkWithMe Youtube Channel
* Written by Ahn, Jeeho
* 2021.6.19
*/
#pragma once
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include "opencv2/calib3d.hpp"
#include "opencv2/imgproc.hpp"
#include <opencv2/videoio/videoio.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int undistortVideo(string original_file, string result_file, cv::Mat* cameraMatrix, cv::Mat* distCoeffs, int num_of_threads = 8)
{
//read video file
VideoCapture vid = VideoCapture(original_file);
//video info
auto frame_width = vid.get(CAP_PROP_FRAME_WIDTH);
auto frame_height = vid.get(CAP_PROP_FRAME_HEIGHT);
auto bit_rate = vid.get(CAP_PROP_BITRATE);
auto total_frames = vid.get(CAP_PROP_FRAME_COUNT);
auto frame_rate = vid.get(CAP_PROP_FPS);
//video to be written out
//set to be mp4 format
VideoWriter out_video(result_file, cv::VideoWriter::fourcc('m','p','4','v'), frame_rate, Size(vid.get(cv::CAP_PROP_FRAME_WIDTH), vid.get(cv::CAP_PROP_FRAME_HEIGHT)));
//frame count
int count = 1;
//undistort each frame in loop
while(count < total_frames)
{
std::vector<shared_ptr<cv::Mat>> temp_frames(0);
//stack frames in a list for multithreading
for (int a = 0; a < num_of_threads; a++)
{
cv::Mat frame;
vid >> frame;
if (frame.empty())
break;
temp_frames.push_back(std::make_shared<cv::Mat>(frame));
}
//imshow("d",frame);
//waitKey(0);
//output list
std::vector<shared_ptr<cv::Mat>> temp_undist_imgs(temp_frames.size());
//OpenMP for multithreading
#pragma omp parallel for
for (int l = 0; l < temp_frames.size(); l++)
{
// notes on fixing the crop: https://docs.opencv.org/4.x/dc/dbb/tutorial_py_calibration.html
cv::Size s = (*temp_frames[l]).size();
s.width += 100;
cv::Mat newCameraMatrix = cv::getOptimalNewCameraMatrix(*cameraMatrix, *distCoeffs, s, 1, s);
cv::Mat undist_img_;
//cv::undistort(*temp_frames[l], undist_img_, *cameraMatrix, *distCoeffs); // crop it
cv::undistort(*temp_frames[l], undist_img_, *cameraMatrix, *distCoeffs, newCameraMatrix); // don't crop it
temp_undist_imgs[l] = make_shared<cv::Mat>(undist_img_);
//imshow("w", *temp_frames[l]);
imshow("w", undist_img_);
imwrite("E:\\ultimate\\test.png", undist_img_);
waitKey(0);
}
//write all undistorted frames to a video file
for (int u = 0; u < temp_undist_imgs.size(); u++)
{
out_video.write(*temp_undist_imgs[u]);
std::cout << "\r" << count << " frames out of " << total_frames << " frames" << std::flush;
count++;
}
}
//end = clock();
//printf("\nTime Elapsed : %.2f seconds \n", ((float)(end - start) / CLOCKS_PER_SEC));
//waitKey(0); // key press to close window
out_video.release();
return 0;
}