-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathavm_app_demo.cpp
152 lines (135 loc) · 5.38 KB
/
avm_app_demo.cpp
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
142
143
144
145
146
147
148
149
150
151
152
/***
* function: 360 surrond view combine c++ demo
* author: joker.mao
* date: 2023/07/15
* copyright: ADAS_EYES all right reserved
*/
#include "common.h"
//#define DEBUG
#define AWB_LUN_BANLANCE_ENALE 1
int main(int argc, char** argv)
{
std::cout << argv[0] << " app start running..." << std::endl;
cv::Mat car_img;
cv::Mat origin_dir_img[4];
cv::Mat undist_dir_img[4];
cv::Mat merge_weights_img[4];
cv::Mat out_put_img;
float *w_ptr[4];
CameraPrms prms[4];
//1.read image and read weights
car_img = cv::imread("images/car.png");
cv::resize(car_img, car_img, cv::Size(xr - xl, yb - yt));
out_put_img = cv::Mat(cv::Size(total_w, total_h), CV_8UC3, cv::Scalar(0, 0, 0));
cv::Mat weights = cv::imread("yaml/weights.png", -1);
if (weights.channels() != 4) {
std::cerr << "imread weights failed " << weights.channels() << "\r\n";
return -1;
}
for (int i = 0; i < 4; ++i) {
merge_weights_img[i] = cv::Mat(weights.size(), CV_32FC1, cv::Scalar(0, 0, 0));
w_ptr[i] = (float *)merge_weights_img[i].data;
}
//read weights of corner
int pixel_index = 0;
for (int h = 0; h < weights.rows; ++h) {
uchar* uc_pixel = weights.data + h * weights.step;
for (int w = 0; w < weights.cols; ++w) {
w_ptr[0][pixel_index] = uc_pixel[0] / 255.0f;
w_ptr[1][pixel_index] = uc_pixel[1] / 255.0f;
w_ptr[2][pixel_index] = uc_pixel[2] / 255.0f;
w_ptr[3][pixel_index] = uc_pixel[3] / 255.0f;
uc_pixel += 4;
++pixel_index;
}
}
#ifdef DEBUG
for (int i = 0; i < 4; ++i) {
//0 左下 1 右上 2 左上 3 左下
display_mat(merge_weights_img[i], "w");
}
#endif
//1. read calibration prms
for (int i = 0; i < 4; ++i) {
auto& prm = prms[i];
prm.name = camera_names[i];
auto ok = read_prms("yaml/" + prm.name + ".yaml", prm);
if (!ok) {
return -1;
}
}
//2.lum equalization and awb for four channel image
std::vector<cv::Mat*> srcs;
for (int i = 0; i < 4; ++i) {
auto& prm = prms[i];
origin_dir_img[i] = cv::imread("images/" + prm.name + ".png");
srcs.push_back(&origin_dir_img[i]);
}
#if AWB_LUN_BANLANCE_ENALE
awb_and_lum_banlance(srcs);
#endif
//3. undistort image
for (int i = 0; i < 4; ++i) {
auto& prm = prms[i];
cv::Mat& src = origin_dir_img[i];
undist_by_remap(src, src, prm);
cv::warpPerspective(src, src, prm.project_matrix, project_shapes[prm.name]);
if (camera_flip_mir[i] == "r+") {
cv::rotate(src, src, cv::ROTATE_90_CLOCKWISE);
} else if (camera_flip_mir[i] == "r-") {
cv::rotate(src, src, cv::ROTATE_90_COUNTERCLOCKWISE);
} else if (camera_flip_mir[i] == "m") {
cv::rotate(src, src, cv::ROTATE_180);
}
//display_mat(src, "project");
//cv::imwrite(prms.name + "_undist.png", src);
undist_dir_img[i] = src.clone();
}
//4.start combine
std::cout << argv[0] << " app start combine" << std::endl;
car_img.copyTo(out_put_img(cv::Rect(xl, yt, car_img.cols, car_img.rows)));
//4.1 out_put_img center copy
for (int i = 0; i < 4; ++i) {
cv::Rect roi;
bool is_cali_roi = false;
if (std::string(camera_names[i]) == "front") {
roi = cv::Rect(xl, 0, xr - xl, yt);
//std::cout << "\nfront" << roi;
undist_dir_img[i](roi).copyTo(out_put_img(roi));
} else if (std::string(camera_names[i]) == "left") {
roi = cv::Rect(0, yt, xl, yb - yt);
//std::cout << "\nleft" << roi << out_put_img.size();
undist_dir_img[i](roi).copyTo(out_put_img(roi));
} else if (std::string(camera_names[i]) == "right") {
roi = cv::Rect(0, yt, xl, yb - yt);
//std::cout << "\nright" << roi << out_put_img.size();
undist_dir_img[i](roi).copyTo(out_put_img(cv::Rect(xr, yt, total_w - xr, yb - yt)));
} else if (std::string(camera_names[i]) == "back") {
roi = cv::Rect(xl, 0, xr - xl, yt);
//std::cout << "\nright" << roi << out_put_img.size();
undist_dir_img[i](roi).copyTo(out_put_img(cv::Rect(xl, yb, xr - xl, yt)));
}
}
//4.2the four corner merge
//w: 0 左下 1 右上 2 左上 3 左下
//image: "front", "left", "back", "right"
cv::Rect roi;
//左上
roi = cv::Rect(0, 0, xl, yt);
merge_image(undist_dir_img[0](roi), undist_dir_img[1](roi), merge_weights_img[2], out_put_img(roi));
//右上
roi = cv::Rect(xr, 0, xl, yt);
merge_image(undist_dir_img[0](roi), undist_dir_img[3](cv::Rect(0, 0, xl, yt)), merge_weights_img[1], out_put_img(cv::Rect(xr, 0, xl, yt)));
//左下
roi = cv::Rect(0, yb, xl, yt);
merge_image(undist_dir_img[2](cv::Rect(0, 0, xl, yt)), undist_dir_img[1](roi), merge_weights_img[0], out_put_img(roi));
//右下
roi = cv::Rect(xr, 0, xl, yt);
merge_image(undist_dir_img[2](roi), undist_dir_img[3](cv::Rect(0, yb, xl, yt)), merge_weights_img[3], out_put_img(cv::Rect(xr, yb, xl, yt)));
cv::imwrite("ADAS_EYES_360_VIEW.png", out_put_img);
#ifdef DEBUG
cv::resize(out_put_img, out_put_img, cv::Size(out_put_img.size()/2)),
display_mat(out_put_img, "out_put_img");
#endif
std::cout << argv[0] << " app finished" << std::endl;
}