Repository navigation
Expand file tree
/
Copy pathdense-stereo-demo.cc
More file actions
325 lines (296 loc) · 11.8 KB
/
Copy pathdense-stereo-demo.cc
File metadata and controls
325 lines (296 loc) · 11.8 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Sample dense stereo pipeline, implemented using the C mrcal API. Needs at
// least mrcal 2.4. This is a C++ source file because the remapping and
// disparity search come from OpenCV, which is a C++ library. The mrcal calls
// are C functions
//
// On a Debian machine you can build like this:
// g++ \
// -I/usr/include/opencv4 \
// -fpermissive \
// dense-stereo-demo.cc \
// -lopencv_core \
// -lopencv_calib3d \
// -lopencv_imgproc \
// -lmrcal \
// -o dense-stereo-demo
//
// The -fpermissive is required for the C++ compiler to accept C99 code.
//
// Note: this sample code does not bother to deallocate any memory.
#include <stdio.h>
#include <stdlib.h>
#include <opencv2/calib3d.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
extern "C"
{
#include <mrcal/mrcal.h>
}
static
cv::Mat remap(const float* map,
const mrcal_image_uint8_t* image,
const int remapped_width,
const int remapped_height)
{
cv::Mat cv_image(image->height,
image->width,
CV_8UC1,
image->data,
image->stride);
cv::Mat cv_remapped(remapped_height,
remapped_width,
CV_8UC1);
cv::Mat cv_map(remapped_height,
remapped_width,
CV_32FC2,
(void*)map);
cv::remap( cv_image,
cv_remapped,
cv_map, cv::Mat(),
cv::INTER_LINEAR );
return cv_remapped;
}
int main(int argc, char* argv[])
{
// https://mrcal.secretsauce.net/external/2022-11-05--dtla-overpass--samyang--alpha7/stereo/0.cameramodel
// https://mrcal.secretsauce.net/external/2022-11-05--dtla-overpass--samyang--alpha7/stereo/1.cameramodel
const char* model_filenames[] =
{
"/tmp/0.cameramodel",
"/tmp/1.cameramodel"
};
// https://mrcal.secretsauce.net/external/2022-11-05--dtla-overpass--samyang--alpha7/stereo/0.jpg
// https://mrcal.secretsauce.net/external/2022-11-05--dtla-overpass--samyang--alpha7/stereo/1.jpg
const char* image_filenames[] =
{
"/tmp/0.jpg",
"/tmp/1.jpg",
};
// Hard-coded rectified field-of-view and center-of-view parameters
mrcal_point2_t azel_fov_deg = {170., 60.};
mrcal_point2_t azel0_deg = {};
const int disparity_min = 0;
const int disparity_max = 256;
// hard-coded scale used by OpenCV SGBM
const int disparity_scale = 16;
// Use the same resolution in the rectified image as in the input image
double pixels_per_deg_az = -1.;
double pixels_per_deg_el = -1.;
// mrcal >= 2.5 should use mrcal_cameramodel_VOID_t. The type used here will
// continue to work, but it is suggested to use the new name
mrcal_cameramodel_t* models[2];
mrcal_image_uint8_t images[2];
//// Read the models from disk
for(int i=0; i<2; i++)
{
// mrcal >= 3.0 should use mrcal_cameramodel_read_file. The function
// name used here will continue to work, but it is suggested to use the
// new name
models[i] = mrcal_read_cameramodel_file(model_filenames[i], false);
if(models[i] == NULL)
{
fprintf(stderr, "Error loading model '%s'\n", model_filenames[i]);
return 1;
}
if(!mrcal_image_uint8_load(&images[i],
image_filenames[i]))
{
fprintf(stderr, "Error loading image '%s'\n", image_filenames[i]);
return 1;
}
}
//// Compute the rectified system
mrcal_cameramodel_LENSMODEL_LATLON_t model_rectified0 =
{ .lensmodel = { .type = MRCAL_LENSMODEL_LATLON } };
double baseline;
if(!mrcal_rectified_system(// output
model_rectified0.imagersize,
model_rectified0.intrinsics,
model_rectified0.rt_cam_ref,
&baseline,
// input, output
&pixels_per_deg_az,
&pixels_per_deg_el,
&azel_fov_deg,
&azel0_deg,
// input
&models[0]->lensmodel,
models[0]->intrinsics,
models[0]->rt_cam_ref,
models[1]->rt_cam_ref,
model_rectified0.lensmodel.type,
// autodetect nothing
false,false,false,false))
{
fprintf(stderr, "Error calling mrcal_rectified_system()\n");
return 1;
}
//// Compute the rectification maps
const int rectified_width = model_rectified0.imagersize[0];
const int rectified_height = model_rectified0.imagersize[1];
float* rectification_maps =
(float*)aligned_alloc(0x10,
rectified_width*
rectified_height*
2*2*sizeof(float));
if(rectification_maps == NULL)
{
fprintf(stderr, "Error calling malloc()\n");
return 1;
}
if(!mrcal_rectification_maps(// output
rectification_maps,
// input
&models[0]->lensmodel,
models [0]->intrinsics,
models [0]->rt_cam_ref,
&models[1]->lensmodel,
models [1]->intrinsics,
models [1]->rt_cam_ref,
model_rectified0.lensmodel.type,
model_rectified0.intrinsics,
model_rectified0.imagersize,
model_rectified0.rt_cam_ref) )
{
fprintf(stderr, "Error calling mrcal_rectification_maps()\n");
return 1;
}
//// Use the rectification maps to compute the rectified images
cv::Mat cv_left_rect =
remap(&rectification_maps[0],
&images[0],
rectified_width,
rectified_height);
cv::Mat cv_right_rect =
remap(&rectification_maps[rectified_width*
rectified_height*
2],
&images[1],
rectified_width,
rectified_height);
//// Write the rectified images to disk
mrcal_image_uint8_save("/tmp/rect-left.png",
&(mrcal_image_uint8_t)
{
.width = rectified_width,
.height = rectified_height,
.stride = rectified_width,
.data = (uint8_t*)cv_left_rect.data
});
fprintf(stderr, "Wrote '/tmp/rect-left.png'\n");
mrcal_image_uint8_save("/tmp/rect-right.png",
&(mrcal_image_uint8_t)
{
.width = rectified_width,
.height = rectified_height,
.stride = rectified_width,
.data = (uint8_t*)cv_right_rect.data
});
fprintf(stderr, "Wrote '/tmp/rect-right.png'\n");
//// Disparity search
cv::Mat cv_disparity;
cv::Ptr<cv::StereoSGBM> sgbm =
cv::StereoSGBM::create(disparity_min,
disparity_max-disparity_min,
3,
600,
2400,
1,
0,
5,
100,
2);
sgbm->compute(cv_left_rect, cv_right_rect, cv_disparity);
mrcal_image_uint16_t image_disparity =
{
.width = rectified_width,
.height = rectified_height,
.stride = rectified_width*2,
.data = (uint16_t*)cv_disparity.data
};
mrcal_image_bgr_t image_color_disparity =
{ .width = rectified_width,
.height = rectified_height,
.stride = (int)(rectified_width*sizeof(mrcal_bgr_t)),
.data = (mrcal_bgr_t*)malloc(rectified_height*
rectified_width*sizeof(mrcal_bgr_t))
};
if(image_color_disparity.data == NULL)
{
fprintf(stderr, "Error: malloc() failed\n");
return 1;
}
if(!mrcal_apply_color_map_uint16(&image_color_disparity,
&image_disparity,
// no auto range
false,false,
// auto function
true,
disparity_min*disparity_scale,
disparity_max*disparity_scale,
// ignored functions
0,0,0))
{
fprintf(stderr, "Error: mrcal_apply_color_map_uint16() failed\n");
return 1;
}
mrcal_image_bgr_save("/tmp/disparity.png", &image_color_disparity);
fprintf(stderr, "Wrote '/tmp/disparity.png'\n");
//// Convert disparities to ranges
mrcal_image_double_t image_range =
{ .width = rectified_width,
.height = rectified_height,
.stride = (int)(rectified_width*sizeof(double)),
.data = (double*)aligned_alloc(0x10,
rectified_height*
rectified_width*sizeof(double))
};
if(image_range.data == NULL)
{
fprintf(stderr, "Error: malloc() failed\n");
return 1;
}
if(!mrcal_stereo_range_dense(&image_range,
&image_disparity,
disparity_scale,
disparity_min * disparity_scale,
disparity_max * disparity_scale,
model_rectified0.lensmodel.type,
model_rectified0.intrinsics,
baseline))
{
fprintf(stderr, "Error: mrcal_stereo_range_dense() failed\n");
return 1;
}
mrcal_image_bgr_t image_color_range =
{ .width = rectified_width,
.height = rectified_height,
.stride = (int)(rectified_width*sizeof(mrcal_bgr_t)),
.data = (mrcal_bgr_t*)malloc(rectified_height*
rectified_width*sizeof(mrcal_bgr_t))
};
if(image_color_range.data == NULL)
{
fprintf(stderr, "Error: malloc() failed\n");
return 1;
}
const double range_min = 0;
const double range_max = 1000;
if(!mrcal_apply_color_map_double(&image_color_range,
&image_range,
// no auto range
false,false,
// auto function
true,
range_min,
range_max,
// ignored functions
0,0,0))
{
fprintf(stderr, "Error: mrcal_apply_color_map_double() failed\n");
return 1;
}
mrcal_image_bgr_save("/tmp/range.png", &image_color_range);
fprintf(stderr, "Wrote '/tmp/range.png'\n");
return 0;
}