forked from AbangLZU/LaneDetect
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
143 lines (114 loc) · 4.42 KB
/
Copy pathmain.cpp
File metadata and controls
143 lines (114 loc) · 4.42 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include "laneDetection.h"
#include "calibration.h"
using namespace std;
cv::VideoCapture laneVideo;
cv::Mat videoFrame; // Video Frame.
cv::Mat videoFrameUndistorted; // Video Frame (after calibration).
cv::Mat videoFramePerspective; // Video Frame (after perspective transform).
cv::Mat _videoFrameUndistorted;
cv::Size videoSize; // Input Variable Size.
cv::Mat cameraMatrix, dist; //Calibration Matrix.
cv::Mat perspectiveMatrix; //Homography Matrix.
cv::Point2f perspectiveSrc[] = {cv::Point2f(565,470), cv::Point2f(721,470), cv::Point2f(277,698), cv::Point2f(1142,698)};
cv::Point2f perspectiveDst[] = {cv::Point2f(300,0), cv::Point2f(980,0), cv::Point2f(300,720), cv::Point2f(980,720)};
int main(int argc, char **argv)
{
//Get the Perspective Matrix.
perspectiveMatrix = getPerspectiveTransform(perspectiveSrc,perspectiveDst);
//Load the video.
if(argc < 2)
{
cerr << "There is no input video." << endl;
return -1;
}
laneVideo.open(argv[1]); //Load the video.
if(!laneVideo.isOpened())
{
cerr << "Could not open the video." << endl;
return -1;
}
videoSize = cv::Size((int)laneVideo.get(cv::CAP_PROP_FRAME_WIDTH),
(int)laneVideo.get(cv::CAP_PROP_FRAME_HEIGHT));
//--------------Camera Calibration Start-----------------
cv::FileStorage fsRead;
fsRead.open("Intrinsic.xml", cv::FileStorage::READ);
cv::Mat src = cv::imread("../camera_cal/calibration2.jpg");
cv::Mat dst;
if (fsRead.isOpened() == false)
{
CameraCalibrator myCameraCalibrator;
myCameraCalibrator.doCalibration(cameraMatrix, dist);
cv::FileStorage fs;
fs.open("Intrinsic.xml", cv::FileStorage::WRITE);
fs << "CameraMatrix" << cameraMatrix;
fs << "Dist" << dist;
fs.release();
fsRead.release();
cout << "There is no existing intrinsic parameters XML file." << endl;
cout << "Start calibraton......" << endl;
}
else
{
fsRead["CameraMatrix"] >> cameraMatrix;
fsRead["Dist"] >> dist;
fsRead.release();
}
undistort(src, dst, cameraMatrix, dist);
//--------------Camera Calibration Finish-----------------
cv::Mat warpEdge;
cv::Mat imageRedChannel;
cv::Mat redBinary;
cv::Mat mergeImage;
cv::Mat histImage;
cv::Mat maskImage;
cv::Mat warpMask;
cv::Mat debugWindowROI;
cv::Mat resizePic;
//===========Start Real Time Processing===========
float laneDistant = 0;
stringstream ss;
cv::namedWindow("Real Time Execution", CV_WINDOW_NORMAL);
laneVideo.set(cv::CAP_PROP_POS_FRAMES, 0);
laneVideo >> videoFrame;
undistort(videoFrame, videoFrameUndistorted, cameraMatrix, dist);
_videoFrameUndistorted = videoFrameUndistorted.clone();
cv::Mat showVideos(videoFrame.size().height, videoFrame.size().width, CV_8UC3, cv::Scalar(0,0,0));
laneDetection LaneAlgoVideo(_videoFrameUndistorted, perspectiveMatrix);
undistort(videoFrame, videoFrameUndistorted, cameraMatrix, dist);
_videoFrameUndistorted = videoFrameUndistorted.clone();
while(!videoFrame.empty())
{
clock_t start_time = clock();
//Start Homography
warpPerspective(_videoFrameUndistorted, videoFramePerspective, perspectiveMatrix, videoSize);
//Applying lane detection algorithm
LaneAlgoVideo.laneDetctAlgo();
//Detect the distance to lane center.
laneDistant = LaneAlgoVideo.getLaneCenterDist();
// if(laneDistant > 0)
// {
// std::cout << abs(laneDistant) << "m " << " To the Right" << std::endl;
// }
// else
// {
// std::cout << abs(laneDistant) << "m " << " To the Left"<< std::endl;
// }
mergeImage = LaneAlgoVideo.getMergeImage();
imshow("Real Time Execution", mergeImage);
clock_t end_time = clock();
std::cout<<"time is "<< end_time - start_time<<std::endl;
laneVideo >> videoFrame;
if(videoFrame.empty()) break;
//Calibration
undistort(videoFrame, videoFrameUndistorted, cameraMatrix, dist);
_videoFrameUndistorted = videoFrameUndistorted.clone();
LaneAlgoVideo.setInputImage(_videoFrameUndistorted);
if(cv::waitKey(10) == 27) break;
}
//===========Finish Real Time Processing===========
cv::waitKey(0);
return 0;
}