-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
307 lines (222 loc) · 7.18 KB
/
main.cpp
File metadata and controls
307 lines (222 loc) · 7.18 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include <iostream>
#include <stdint.h>
#include <stdio.h>
#include <bitset>
#include <smmintrin.h> // intrinsics
#include <emmintrin.h>
#include<fstream>
#include<stdio.h>
#include <opencv.hpp>
#include "StereoBMHelper.h"
#include "FastFilters.h"
#include "StereoSGM.h"
#include<time.h>
#include <vector>
#include <list>
#include <algorithm>
#include <numeric>
#include <iostream>
#include "multilayer_stixel_world.h"
#ifdef _DEBUG
#pragma comment(lib, "opencv_world400d")
#else
#pragma comment(lib, "opencv_world400")
#endif
const int dispRange = 128;
void jet(float x, int& r, int& g, int& b)
{
if (x < 0) x = -0.05;
if (x > 1) x = 1.05;
x = x / 1.15 + 0.1; // use slightly asymmetric range to avoid darkest shades of blue.
r = __max(0, __min(255, (int)(round(255 * (1.5 - 4 * fabs(x - .75))))));
g = __max(0, __min(255, (int)(round(255 * (1.5 - 4 * fabs(x - .5))))));
b = __max(0, __min(255, (int)(round(255 * (1.5 - 4 * fabs(x - .25))))));
}
cv::Mat Float2ColorJet(cv::Mat &fimg, float dmin, float dmax)
{
int width = fimg.cols, height = fimg.rows;
cv::Mat img(height, width, CV_8UC3);
float scale = 1.0 / (dmax - dmin);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
float f = fimg.at<float>(y, x);
int r = 0;
int g = 0;
int b = 0;
/*if (f != INFINITY)*/ {
float val = scale * (f - dmin);
jet(val, r, g, b);
}
img.at<cv::Vec3b>(y, x) = cv::Vec3b(b, g, r);
}
}
return img;
}
template<typename T>
void processCensus5x5SGM(T* leftImg, T* rightImg, float32* output, float32* dispImgRight,
int width, int height, uint16 paths, const int dispCount)
{
const int maxDisp = dispCount - 1;
//std::cout << std::endl << paths << ", " << dispCount << std::endl;
// get memory and init sgm params
uint32* leftImgCensus = (uint32*)_mm_malloc(width*height*sizeof(uint32), 16);
uint32* rightImgCensus = (uint32*)_mm_malloc(width*height*sizeof(uint32), 16);
StereoSGMParams_t params;
params.lrCheck = true;
params.MedianFilter = true;
params.Paths = paths;
params.NoPasses = 2;
uint16* dsi = (uint16*)_mm_malloc(width*height*(maxDisp + 1)*sizeof(uint16), 32);
StereoSGM<T> m_sgm16(width, height, maxDisp, params);
census5x5_16bit_SSE(leftImg, leftImgCensus, width, height);
census5x5_16bit_SSE(rightImg, rightImgCensus, width, height);
costMeasureCensus5x5_xyd_SSE(leftImgCensus, rightImgCensus, height, width, dispCount, params.InvalidDispCost, dsi);
m_sgm16.process(dsi, leftImg, output, dispImgRight);
_mm_free(dsi);
}
void onMouse(int event, int x, int y, int flags, void *param)
{
cv::Mat *im = reinterpret_cast<cv::Mat *>(param);
switch (event)
{
case cv::EVENT_LBUTTONDBLCLK:
std::cout << "at (" << std::setw(3) << x << "," << std::setw(3) << y << ") value is: "
<< static_cast<int>(im->at<uchar>(cv::Point(x, y))) << std::endl;
break;
}
}
int formatJPG(cv::Mat& imgL, cv::Mat& imgR, cv::Mat &imgDisp)
{
int cols_ = imgL.cols;
int rows_ = imgL.rows;
uint16* leftImg = (uint16*)_mm_malloc(rows_*cols_*sizeof(uint16), 16);
uint16* rightImg = (uint16*)_mm_malloc(rows_*cols_*sizeof(uint16), 16);
for (int i = 0; i < rows_; i++)
{
for (int j = 0; j < cols_; j++)
{
leftImg[i * cols_ + j] = *(imgL.data + i*imgL.step + j * imgL.elemSize());
rightImg[i * cols_ + j] = *(imgR.data + i*imgR.step + j * imgR.elemSize());
}
}
//左右图像的视差图分配存储空间(width*height*sizeof(float32))
float32* dispImg = (float32*)_mm_malloc(rows_*cols_*sizeof(float32), 16);
float32* dispImgRight = (float32*)_mm_malloc(rows_*cols_*sizeof(float32), 16);
const int numPaths = 8;
processCensus5x5SGM(leftImg, rightImg, dispImg, dispImgRight, cols_, rows_, numPaths, dispRange);
cv::Mat tmpDisp(cv::Size(cols_, rows_), CV_32FC1, dispImg);
tmpDisp.copyTo(imgDisp);
_mm_free(leftImg);
_mm_free(rightImg);
return 0;
}
static cv::Scalar computeColor(float val)
{
const float hscale = 6.f;
float h = 0.6f * (1.f - val), s = 1.f, v = 1.f;
float r, g, b;
static const int sector_data[][3] =
{ { 1, 3, 0 }, { 1, 0, 2 }, { 3, 0, 1 }, { 0, 2, 1 }, { 0, 1, 3 }, { 2, 1, 0 } };
float tab[4];
int sector;
h *= hscale;
if (h < 0)
do h += 6; while (h < 0);
else if (h >= 6)
do h -= 6; while (h >= 6);
sector = cvFloor(h);
h -= sector;
if ((unsigned)sector >= 6u)
{
sector = 0;
h = 0.f;
}
tab[0] = v;
tab[1] = v*(1.f - s);
tab[2] = v*(1.f - s*h);
tab[3] = v*(1.f - s*(1.f - h));
b = tab[sector_data[sector][0]];
g = tab[sector_data[sector][1]];
r = tab[sector_data[sector][2]];
//return 255 * cv::Scalar(b, g, r);
return cv::Scalar(255 * b, 255 * g, 255 * r);
}
static cv::Scalar dispToColor(float disp, float maxdisp)
{
if (disp < 0)
return cv::Scalar(128, 128, 128);
return computeColor(std::min(disp, maxdisp) / maxdisp);
}
static void drawStixel(cv::Mat& img, const Stixel& stixel, cv::Scalar color)
{
const int radius = std::max(stixel.width / 2, 1);
const cv::Point tl(stixel.u - radius, stixel.vT);
const cv::Point br(stixel.u + radius, stixel.vB);
cv::rectangle(img, cv::Rect(tl, br), color, -1);
cv::rectangle(img, cv::Rect(tl, br), cv::Scalar(255, 255, 255), 1);
}
int main()
{
// input camera parameters
const cv::FileStorage cvfs("camera.xml", cv::FileStorage::READ);
CV_Assert(cvfs.isOpened());
// input parameters
MultiLayerStixelWrold::Parameters param;
param.camera.fu = cvfs["FocalLengthX"];
param.camera.fv = cvfs["FocalLengthY"];
param.camera.u0 = cvfs["CenterX"];
param.camera.v0 = cvfs["CenterY"];
param.camera.baseline = cvfs["BaseLine"];
param.camera.height = cvfs["Height"];
param.camera.tilt = cvfs["Tilt"];
param.dmax = dispRange;
MultiLayerStixelWrold stixelWorld(param);
std::string dir = "E:/Image Set/";
cv::VideoWriter DemoWrite;
//std::string outputName = dir + "0005.avi";
for (int frameno = 0;; frameno++)
{
char base_name[256];
sprintf(base_name, "%06d.png", frameno);
std::string bufl = dir + "1/testing/0020/" + base_name;
std::string bufr = dir + "2/testing/0020/" + base_name;
std::cout << " " << frameno << std::endl;
cv::Mat leftBGR = cv::imread(bufl, cv::IMREAD_COLOR);
cv::Mat right = cv::imread(bufr, cv::IMREAD_GRAYSCALE);
if (leftBGR.empty() || right.empty())
{
std::cout << "Left image no exist!" << std::endl;
frameno = 0;
continue;
//break;
}
cv::Mat left;
if (leftBGR.channels() == 3)
{
cv::cvtColor(leftBGR, left, cv::COLOR_BGR2GRAY);
}
else
{
left = leftBGR.clone();
}
CV_Assert(left.size() == right.size() && left.type() == right.type());
cv::Rect roiRect(0, 0, left.cols - left.cols % 16, left.rows);
cv::Mat leftROI(left, roiRect);
cv::Mat rightROI(right, roiRect);
cv::Mat showImage(leftBGR, roiRect);
// calculate disparity SGM-Based
cv::Mat imgDisp;
formatJPG(leftROI, rightROI, imgDisp);
const std::vector<Stixel> stixels = stixelWorld.compute(imgDisp);
// draw stixels
cv::Mat draw;
cv::cvtColor(leftROI, draw, cv::COLOR_GRAY2BGRA);
cv::Mat stixelImg = cv::Mat::zeros(leftROI.size(), draw.type());
for (const auto& stixel : stixels)
drawStixel(stixelImg, stixel, dispToColor(stixel.disp, 64));
draw = draw + 0.5 * stixelImg;
cv::imshow("disparity", Float2ColorJet(imgDisp, 0, dispRange));
cv::imshow("stixels", draw);
cv::waitKey(10);
}
}