20 std::vector<double>
calc_intensity( std::vector<double>
const & image1, std::vector<double>
const & image2 )
22 std::vector<double>
res( image1.size(), 0 );
24 for(
size_t i = 0;
i < image1.size();
i++ )
26 res[
i] = sqrt( pow( image1[
i], 2 ) + pow( image2[i], 2 ) );
32 double dot_product( std::vector<T>
const & sub_image, std::vector<double>
const &
mask )
36 for(
auto i = 0;
i < sub_image.size();
i++ )
38 res += sub_image[
i] * mask[
i];
45 std::vector<double> convolution(std::vector<T>
const&
image,
46 size_t image_width,
size_t image_height,
47 size_t mask_width,
size_t mask_height,
48 std::function<
double(std::vector<T>
const& sub_image) > convolution_operation
51 std::vector<double>
res(image.size(), 0);
53 for (
size_t i = 0;
i < image_height - mask_height + 1;
i++)
55 for (
size_t j = 0;
j < image_width - mask_width + 1;
j++)
57 std::vector<T> sub_image(mask_width * mask_height, 0);
59 for (
size_t l = 0; l < mask_height; l++)
61 for (
size_t k = 0; k < mask_width; k++)
63 size_t p = (
i + l) * image_width +
j + k;
64 sub_image[
ind++] = (image[
p]);
68 auto mid = (
i + mask_height / 2) * image_width +
j + mask_width / 2;
69 res[mid] = convolution_operation(sub_image);
76 std::vector<double> calc_horizontal_gradient( std::vector<T>
const & image,
size_t image_width,
size_t image_height )
78 std::vector<double> horizontal_gradients = { -1, -2, -1,
82 return convolution<T>(
image, image_width, image_height, 3, 3, [&]( std::vector<T>
const & sub_image )
83 {
return dot_product( sub_image, horizontal_gradients ) / (double)8; } );
87 std::vector<double> calc_vertical_gradient( std::vector<T>
const & image,
size_t image_width,
size_t image_height )
89 std::vector<double> vertical_gradients = { -1, 0, 1,
93 return convolution<T>(
image, image_width, image_height, 3, 3, [&]( std::vector<T>
const & sub_image )
94 {
return dot_product( sub_image, vertical_gradients ) / (double)8; } );;
98 std::vector<double> calc_edges( std::vector<T>
const & image,
size_t image_width,
size_t image_height )
100 auto vertical_edge = calc_vertical_gradient( image, image_width, image_height );
102 auto horizontal_edge = calc_horizontal_gradient( image, image_width, image_height );
144 std::vector<double>
const & gradient_x,
145 std::vector<double>
const & gradient_y
148 std::vector<double>
res( gradient_x.size(),
deg_none );
150 for(
size_t i = 0;
i < gradient_x.size();
i++ )
153 auto angle = atan2( gradient_y[
i], gradient_x[i] )* 180.f / M_PI;
163 std::vector<double>
const& gradient_x,
164 std::vector<double>
const& gradient_y
167 std::vector<double>
res(gradient_x.size(),
deg_none);
169 for (
size_t i = 0;
i < gradient_x.size();
i++)
172 auto angle = atan2(gradient_y[
i], gradient_x[i])*180.f / M_PI;
187 int edge_minus_idx = 0;
188 int edge_minus_idy = 0;
190 auto &
d = dir_map[
dir];
192 edge_minus_idx = int(width) - 1 -
j;
193 else if( j -
d.first >= width )
196 edge_minus_idx = j -
d.first;
198 if( i -
d.second < 0 )
199 edge_minus_idy = int(height) - 1 -
i;
200 else if( i -
d.second >= height )
203 edge_minus_idy = i -
d.second;
205 return { edge_minus_idx, edge_minus_idy };
215 auto &
d = dir_map[
dir];
217 int edge_plus_idx = 0;
218 int edge_plus_idy = 0;
220 if( j +
d.first < 0 )
221 edge_plus_idx = int(width) - 1 -
j;
222 else if( j +
d.first >= width )
225 edge_plus_idx = j +
d.first;
227 if( i +
d.second < 0 )
228 edge_plus_idy = int(height) - 1 -
i;
229 else if( i +
d.second >= height )
232 edge_plus_idy = i +
d.second;
234 return { edge_plus_idx, edge_plus_idy };
239 auto it = gradient.begin();
240 for(
size_t m = 0;
m < margin; ++
m )
245 *(
it +
m * width +
i ) = 0;
246 *(
it + width * ( height -
m - 1 ) +
i ) = 0;
251 *(
it +
i * width +
m ) = 0;
252 *(
it +
i * width + ( width -
m - 1 ) ) = 0;
259 std::vector< T >
const & origin,
260 std::vector< byte >
const & valid_edge_by_ir,
270 for(
size_t x = 0;
x < origin.size(); ++
x )
271 if( valid_edge_by_ir[
x] )
272 filtered.push_back( origin[x] );
277 std::vector< T >
const & origin,
278 std::vector< byte >
const & valid_edge_by_ir,
287 auto idx =
i * width +
j;
288 if( valid_edge_by_ir[
idx] )
290 filtered.push_back( origin[idx] );
297 std::vector<double>& gridx,
298 std::vector<double>& gridy,
306 gridx.push_back(
double(
j ) );
307 gridy.push_back(
double(
i ) );
314 std::vector< double >
const x[],
315 std::vector< double >
const y[],
320 std::vector<double> local_interp;
321 local_interp.reserve( valid_size * dim );
322 auto iedge_it = grid_points.begin();
323 std::vector<double>::const_iterator loc_reg_x[4];
324 std::vector<double>::const_iterator loc_reg_y[4];
325 for(
auto i = 0;
i < dim;
i++ )
327 loc_reg_x[
i] = x[
i].begin();
328 loc_reg_y[
i] = y[
i].begin();
330 for (
size_t i = 0;
i < valid_size;
i++)
332 for (
size_t k = 0; k < dim; k++)
334 auto idx = *(loc_reg_x[k] +
i) - 1;
335 auto idy = *(loc_reg_y[k] +
i) - 1;
337 auto val = *( iedge_it +
size_t( valid_width * idy +
idx ) );
338 local_interp.push_back(
val);
343 std::vector<uint8_t>
is_suppressed(std::vector<double>
const & local_edges,
size_t valid_size)
345 std::vector<uint8_t> is_supressed;
346 auto loc_edg_it = local_edges.begin();
347 for (
size_t i = 0;
i < valid_size;
i++)
350 auto vec2 = *(loc_edg_it + 1);
351 auto vec3 = *(loc_edg_it + 2);
352 auto vec4 = *(loc_edg_it + 3);
354 bool res = (vec3 >= vec2) && (vec3 >= vec4);
355 is_supressed.push_back(res);
360 static std::vector< double >
depth_mean( std::vector< double >
const & local_x,
361 std::vector< double >
const & local_y )
363 std::vector<double>
res;
364 res.reserve( local_x.size() );
365 size_t size = local_x.size() / 2;
366 auto itx = local_x.begin();
367 auto ity = local_y.begin();
368 for (
size_t i = 0;
i <
size;
i++, ity += 2, itx += 2)
370 double valy = (*ity + *(ity + 1)) / 2;
371 double valx = (*itx + *(itx + 1)) / 2;
380 std::vector< double >
const & direction_per_pixel )
382 std::vector<double>
res;
383 size_t size = direction_per_pixel.size() / 2;
385 auto it_dir = direction_per_pixel.begin();
386 auto it_grad = gradient.begin();
387 for (
size_t i = 0;
i <
size;
i++, it_dir+=2, it_grad+=2)
390 auto rorm_dir1 = *it_dir / sqrt(abs(*it_dir) + abs(*(it_dir + 1)));
391 auto rorm_dir2 = *(it_dir+1) / sqrt(abs(*it_dir) + abs(*(it_dir + 1)));
392 auto val = abs(*it_grad * rorm_dir1 + *(it_grad + 1) * rorm_dir2);
400 std::vector< byte >
const & is_supressed,
401 std::vector< double >
const & values_for_subedges,
402 std::vector< double >
const & ir_local_edges,
405 std::vector< byte >
res;
406 res.reserve( grad_in_direction.size() );
410 for (
size_t i = 0;
i < grad_in_direction.size();
i++)
415 bool cond2 = is_supressed[
i];
416 bool cond3 = values_for_subedges[
i] > 0;
417 res.push_back(cond1 && cond2 && cond3);
422 for (
size_t i = 0;
i < grad_in_direction.size();
i++)
425 bool cond2 = is_supressed[
i];
426 bool cond3 = values_for_subedges[
i] > 0;
427 res.push_back(cond1 && cond2 && cond3);
437 std::vector<double>
res;
438 size_t size = local_values.size() / 4;
440 auto it = local_values.begin();
441 for (
size_t i = 0;
i <
size;
i++)
444 auto val2 = *(
it + 1);
445 auto val3 = *(
it + 2);
446 auto val4 = *(
it + 3);
449 res.push_back(res_val);
476 auto z_gradient_x = calc_vertical_gradient(
_z.
frame, depth_intrinsics.
width, depth_intrinsics.
height);
477 auto z_gradient_y = calc_horizontal_gradient(
_z.
frame, depth_intrinsics.
width, depth_intrinsics.
height );
478 auto ir_gradient_x = calc_vertical_gradient(
_ir.
ir_frame, depth_intrinsics.
width, depth_intrinsics.
height );
479 auto ir_gradient_y = calc_horizontal_gradient(
_ir.
ir_frame, depth_intrinsics.
width, depth_intrinsics.
height );
488 std::vector< byte > valid_edge_pixels_by_ir;
491 valid_edge_pixels_by_ir.reserve( ir_edges.size() );
493 for(
auto ir = ir_edges.begin(),
z = z_edges.begin();
494 ir < ir_edges.end() &&
z < z_edges.end();
507 valid_edge_pixels_by_ir.push_back( valid_edge );
520 std::vector< byte > section_map_depth;
531 std::vector< double > valid_location_rc_x;
532 std::vector< double > valid_location_rc_y;
534 std::vector< double > grid_x, grid_y;
539 std::vector< byte > valid_section_map;
540 std::vector< double > valid_gradient_x;
541 std::vector< double > valid_gradient_y;
547 auto itx = valid_location_rc_x.begin();
548 auto ity = valid_location_rc_y.begin();
550 std::vector< double > valid_location_rc;
551 for (
size_t i = 0;
i < valid_location_rc_x.size();
i++)
555 valid_location_rc.push_back(
y);
556 valid_location_rc.push_back(
x);
568 std::vector< direction > dirs =
get_direction2( valid_gradient_x, valid_gradient_y );
583 double directions[8][2] = { {0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1} };
585 std::vector< double > direction_per_pixel;
586 std::vector< double > direction_per_pixel_x;
588 for(
size_t i = 0;
i < dirs.size();
i++ )
591 direction_per_pixel.push_back(directions[idx][0]);
592 direction_per_pixel.push_back(directions[idx][1]);
593 direction_per_pixel_x.push_back(directions[idx][0]);
595 double vec[4] = { -2,-1,0,1 };
597 auto loc_it = valid_location_rc.begin();
598 auto dir_pp_it = direction_per_pixel.begin();
600 std::vector< double > local_region[4];
601 for(
auto k = 0; k < 4; k++ )
603 local_region[k].reserve( direction_per_pixel.size() );
604 for(
size_t i = 0;
i < direction_per_pixel.size();
i++ )
606 double val = *( loc_it +
i ) + *( dir_pp_it +
i ) * vec[k];
607 local_region[k].push_back( val );
610 std::vector< double > local_region_x[4];
611 std::vector< double > local_region_y[4];
612 for(
auto k = 0; k < 4; k++ )
614 local_region_x[k].reserve( 2 * valid_location_rc_x.size() );
615 local_region_y[k].reserve( 2 * valid_location_rc_x.size() );
616 for(
size_t i = 0;
i < 2 * valid_location_rc_x.size();
i++ )
618 local_region_y[k].push_back( *( local_region[k].
begin() +
i ) );
620 local_region_x[k].push_back( *( local_region[k].
begin() +
i ) );
646 std::vector< double > ::iterator loc_edg_it =
_ir.
local_edges.begin();
647 auto valid_loc_rc = valid_location_rc.begin();
648 auto dir_per_pixel_it = direction_per_pixel.begin();
650 std::vector< double > edge_sub_pixel_x;
651 std::vector< double > edge_sub_pixel_y;
652 std::vector< double > fraq_step;
653 std::vector< double > local_rc_subpixel;
654 std::vector< double > edge_sub_pixel;
656 for(
size_t i = 0;
i < valid_location_rc_x.size();
i++ )
658 double vec2 = *(loc_edg_it + 1);
659 double vec3 = *(loc_edg_it + 2);
660 double vec4 = *(loc_edg_it + 3);
666 double const denom = vec4 + vec2 - 2 * vec3;
667 double const res = ( denom == 0 ) ? 0 : ( -0.5 * ( vec4 - vec2 ) / denom );
669 auto valx = *valid_loc_rc + *dir_per_pixel_it *
res;
672 auto valy = *valid_loc_rc + *dir_per_pixel_it *
res;
678 fraq_step.push_back( res );
679 local_rc_subpixel.push_back( valx );
680 local_rc_subpixel.push_back( valy );
681 edge_sub_pixel.push_back( valy );
682 edge_sub_pixel.push_back( valx );
684 edge_sub_pixel_x.push_back(valy);
685 edge_sub_pixel_y.push_back(valx);
691 std::vector< double > grad_in_direction;
693 std::vector< double > local_x;
694 std::vector< double > local_y;
695 std::vector< double > gradient;
701 valid_location_rc_x.size(),
707 valid_location_rc_x.size(),
718 std::vector< double > values_for_subedges;
724 valid_location_rc_x.size(),
757 std::vector< double > valid_edge_sub_pixel_x;
758 std::vector< double > valid_edge_sub_pixel_y;
771 std::vector< double > valid_values_for_subedges;
777 values_for_subedges = valid_values_for_subedges;
807 std::vector< byte > is_inside;
809 std::vector< double3 > vertices_all;
811 std::vector< double > sub_points;
812 sub_points.reserve( valid_edge_sub_pixel_x.size() * 3 );
814 std::vector< double > valid_edge_sub_pixel;
816 valid_edge_sub_pixel.reserve( valid_edge_sub_pixel_x.size() );
817 for(
size_t i = 0;
i < valid_edge_sub_pixel_x.size();
i++ )
821 valid_edge_sub_pixel.push_back( *( valid_edge_sub_pixel_x.begin() +
i ) );
822 valid_edge_sub_pixel.push_back( *( valid_edge_sub_pixel_y.begin() +
i ) );
825 sub_points.push_back( *( valid_edge_sub_pixel_x.begin() +
i ) - 1 );
826 sub_points.push_back( *( valid_edge_sub_pixel_y.begin() +
i ) - 1 );
827 sub_points.push_back( 1 );
833 vertices_all.reserve( valid_edge_sub_pixel_x.size() );
834 for(
size_t i = 0;
i < sub_points.size();
i += 3 )
838 double sub_points_mult[3];
839 double x = sub_points[
i];
840 double y = sub_points[
i + 1];
841 double z = sub_points[
i + 2];
842 for(
auto jj = 0; jj < 3; jj++ )
844 sub_points_mult[jj] = x * k_depth_pinv.
rot[3 * jj + 0]
845 + y * k_depth_pinv.
rot[3 * jj + 1]
846 + z * k_depth_pinv.
rot[3 * jj + 2];
848 auto z_value_for_subedge = values_for_subedges[
i / 3];
852 vertices_all.push_back( { val1, val2, val3 } );
860 for(
size_t i = 0;
i < uvmap.size();
i++ )
866 bool cond_x = ( uvmap[
i].x >= 0 ) && ( uvmap[
i].
x <=
_yuy.
width - 1 );
867 bool cond_y = ( uvmap[
i].y >= 0 ) && ( uvmap[
i].
y <=
_yuy.
height - 1 );
868 is_inside.push_back( cond_x && cond_y );
877 direction_per_pixel_x,
883 std::vector< byte > z_valid_section_map;
895 std::vector< double > valid_directions;
897 std::vector< double > edited_ir_directions;
898 edited_ir_directions.reserve( dirs.size() );
899 for(
size_t i = 0;
i < dirs.size();
i++ )
901 auto val = double( *( dirs.begin() +
i ) );
904 edited_ir_directions.push_back(
val );
907 edited_ir_directions,
928 std::vector< double > valid_weights;
929 valid_weights.reserve( is_inside.size() );
930 for(
size_t i = 0;
i < is_inside.size();
i++ )
935 transform(valid_edge_sub_pixel_x.begin(), valid_edge_sub_pixel_x.end(), valid_edge_sub_pixel_x.begin(), bind2nd(std::plus<double>(), -1.0));
936 transform(valid_edge_sub_pixel_y.begin(), valid_edge_sub_pixel_y.end(), valid_edge_sub_pixel_y.begin(), bind2nd(std::plus<double>(), -1.0));
949 for (
size_t i = 0;
i < sub_pixel_x.size();
i++)
951 auto x = sub_pixel_x[
i];
952 auto y = sub_pixel_y[
i];
977 for (
auto i = 0;
i < 4;
i++)
992 std::vector< yuy_t > && yuy_data,
993 std::vector< yuy_t > && prev_yuy_data,
994 std::vector< yuy_t > && last_successful_yuy_data,
995 calib const & calibration
1008 std::vector< uint8_t > lum_frame;
1009 std::vector< uint8_t > prev_lum_frame;
1010 std::vector< uint8_t > last_successful_lum_frame;
1022 { edges, lum_frame },
1030 " previous calibration image " 1031 << ( last_successful_yuy_data.empty() ?
"was NOT supplied" :
"supplied" ) );
1035 auto last_successful_edges = calc_edges( last_successful_lum_frame,
_yuy.
width,
_yuy.
height );
1038 { last_successful_edges, last_successful_lum_frame },
1039 { edges, lum_frame },
1057 section_map_rgb.data() );
1060 double const gradRgbTh = 15. * 1280 /
_yuy.
width;
1063 for(
auto it = edges.begin();
it != edges.end(); ++
it, ++
i )
1065 if( *
it > gradRgbTh )
1082 std::vector< ir_t > && ir_data,
1112 std::vector<direction>
res( gradient_x.size(),
deg_none );
1116 for(
size_t i = 0;
i < gradient_x.size();
i++ )
1119 auto angle = atan2( gradient_y[
i], gradient_x[i] )* 180.f / M_PI;
1123 for(
auto d : angle_dir_map )
1125 closest = closest == -1 || abs(
dir -
d.first ) < abs(
dir - closest ) ?
d.first : closest;
1127 res[
i] = angle_dir_map[closest];
1133 std::vector<direction>
res(gradient_x.size(),
deg_none);
1135 std::map<int, direction> angle_dir_map = { {0,
deg_0}, {45,
deg_45} , {90,
deg_90}, {135,
deg_135} , { 180,
deg_180 }, { 225,
deg_225 }, { 270,
deg_270 }, { 315,
deg_315 } };
1139 for (
size_t i = 0;
i < gradient_x.size();
i++)
1142 auto angle = atan2(gradient_y[
i], gradient_x[i]) * 180.f / M_PI;
1146 for (
auto d : angle_dir_map)
1148 closest = closest == -1 || abs(
dir -
d.first) < abs(
dir - closest) ?
d.first : closest;
1150 res[
i] = angle_dir_map[closest];
1201 double x = (double)(pixel[0] - intrin->
ppx) / intrin->
fx;
1202 double y = (double)(pixel[1] - intrin->
ppy) / intrin->
fy;
1204 point[0] = depth *
x;
1205 point[1] = depth *
y;
1212 double x = point[0] / point[2],
y = point[1] / point[2];
1216 double r2 = x * x + y *
y;
1217 double f = 1 + intrin->
coeffs[0] * r2 + intrin->
coeffs[1] * r2*r2 + intrin->
coeffs[4] * r2*r2*r2;
1222 double dx = xcd + 2 * intrin->
coeffs[2] * x*y + intrin->
coeffs[3] * (r2 + 2 * x*
x);
1223 double dy = ycd + 2 * intrin->
coeffs[3] * x*y + intrin->
coeffs[2] * (r2 + 2 * y*
y);
1229 pixel[0] = x * (double)intrin->
fx + (
double)intrin->
ppx;
1230 pixel[1] = y * (double)intrin->
fy + (
double)intrin->
ppy;
1235 std::vector<double> res = edges;
1237 for(
size_t i = 0;
i < image_height;
i++ )
1238 for(
size_t j = 0;
j < image_width;
j++ )
1240 if(
i == 0 &&
j == 0 )
1245 res[
i*image_width +
j] = std::max(
1246 res[
i*image_width + j],
1249 res[
i*image_width +
j] = std::max(
1250 res[
i*image_width + j],
1257 for(
int i =
int(image_height) - 1;
i >= 0;
i-- )
1258 for(
int j =
int(image_width) - 1;
j >= 0;
j-- )
1260 if(
i == image_height - 1 &&
j == image_width - 1 )
1262 else if(
i == image_height - 1 )
1263 res[
i*image_width +
j] = std::max( res[
i*image_width +
j], res[
i*image_width +
j + 1] *
_params.
gamma );
1264 else if(
j == image_width - 1 )
1265 res[
i*image_width +
j] = std::max( res[
i*image_width +
j], res[(
i + 1)*image_width +
j] *
_params.
gamma );
1267 res[
i*image_width +
j] = std::max( res[
i*image_width +
j], (std::max( res[
i*image_width +
j + 1] *
_params.
gamma, res[(
i + 1)*image_width +
j] *
_params.
gamma )) );
1270 for(
size_t i = 0;
i < image_height;
i++ )
1271 for(
size_t j = 0;
j < image_width;
j++ )
1279 std::vector<byte>
res( yuy2_imagh.size(), 0 );
1280 auto yuy2 = (
uint8_t*)yuy2_imagh.data();
1281 for(
size_t i = 0;
i < res.size();
i++ )
1282 res[
i] = yuy2[
i * 2];
1289 std::vector<uint8_t> logic_edges( edges.size(), 0 );
1290 auto max = std::max_element( edges.begin(), edges.end() );
1293 for(
size_t i = 0;
i < edges.size();
i++ )
1295 logic_edges[
i] = abs( edges[
i] ) > thresh ? 1 : 0;
1303 std::vector< double > & sum_weights_per_section,
1304 std::vector< byte >
const & section_map,
1305 std::vector< double >
const &
weights,
1306 size_t num_of_sections
1312 if( section_map.size() != weights.size() )
1314 <<
"unexpected size for section_map (" << section_map.size()
1315 <<
") vs weights (" << weights.size() <<
")" );
1316 sum_weights_per_section.resize( num_of_sections );
1317 auto p_sum = sum_weights_per_section.data();
1318 for(
byte i = 0;
i < num_of_sections; ++
i, ++p_sum )
1322 auto p_section = section_map.data();
1323 auto p_weight = weights.data();
1324 for(
size_t ii = 0;
ii < section_map.size(); ++
ii, ++p_section, ++p_weight )
1326 if( *p_section ==
i )
1327 *p_sum += *p_weight;
1335 return x > y ? x :
y;
1339 return x < y ? x :
y;
1358 std::vector<double3>&
points,
1360 std::vector< byte >
const & valid_edges,
1366 auto ptr = (
double*)points.data();
1367 byte const * valid_edge = valid_edges.data();
1368 for (
size_t i = 0;
i < valid_edges.size(); ++
i)
1373 const double pixel[] = { x[
i] - 1, y[
i] - 1 };
1388 const std::vector<double3>& new_vertices,
1390 std::vector<double> interp_IDT_x,
1391 std::vector<double> interp_IDT_y,
1394 const std::vector<double>& rc,
1395 const std::vector<double2>& xy,
1398 auto coefs =
calc_p_coefs(z_data, new_vertices, yuy_data, cal, p_mat, rc, xy);
1402 data->iteration_data_p.coeffs_p = coefs;
1405 auto sum_of_valids = 0;
1407 for (
size_t i = 0;
i < coefs.x_coeffs.size();
i++)
1409 if (interp_IDT_x[
i] == std::numeric_limits<double>::max() || interp_IDT_y[
i] == std::numeric_limits<double>::max())
1414 for (
auto j = 0;
j < 12;
j++)
1416 sums.
vals[
j] +=
w[
i] * (interp_IDT_x[
i] * coefs.x_coeffs[
i].vals[
j] + interp_IDT_y[
i] * coefs.y_coeffs[
i].vals[
j]);
1422 for (
auto i = 0;
i < 8;
i++)
1424 averages.
vals[
i] = (double)sums.
vals[
i] / (
double)sum_of_valids;
1431 std::pair< std::vector<double2>, std::vector<double>>
calc_rc(
1433 const std::vector<double3>& new_vertices,
1439 auto v = new_vertices;
1441 std::vector<double2> f1( z_data.
vertices.size() );
1442 std::vector<double> r2( z_data.
vertices.size() );
1443 std::vector<double> rc( z_data.
vertices.size() );
1448 auto fx = (double)yuy_intrin.fx;
1449 auto fy = (
double)yuy_intrin.fy;
1450 auto ppx = (double)yuy_intrin.ppx;
1451 auto ppy = (
double)yuy_intrin.ppy;
1458 auto mat = p_mat.
vals;
1459 for(
size_t i = 0;
i < z_data.
vertices.size(); ++
i )
1465 double x1 = (double)mat[0] * (
double)x + (double)mat[1] * (
double)y + (double)mat[2] * (
double)z + (double)mat[3];
1466 double y1 = (double)mat[4] * (
double)x + (double)mat[5] * (
double)y + (double)mat[6] * (
double)z + (double)mat[7];
1467 double z1 = (double)mat[8] * (
double)x + (double)mat[9] * (
double)y + (double)mat[10] * (
double)z + (double)mat[11];
1469 auto x_in = x1 / z1;
1470 auto y_in = y1 / z1;
1472 auto x2 = ((x_in -
ppx) /
fx);
1473 auto y2 = ((y_in -
ppy) /
fy);
1480 rc[
i] = 1 + (double)yuy_intrin.coeffs[0] * r2 + (
double)yuy_intrin.coeffs[1] * r2 * r2 + (double)yuy_intrin.coeffs[4] * r2 * r2 * r2;
1488 const std::vector<double3>& new_vertices,
1490 const std::vector<double2>& uv,
1500 auto rc =
calc_rc( z_data, new_vertices, yuy_data, cal, p_mat);
1504 data->iteration_data_p.d_vals_x = interp_IDT_x;
1505 data->iteration_data_p.d_vals_y = interp_IDT_y;
1506 data->iteration_data_p.xy = rc.first;
1507 data->iteration_data_p.rc = rc.second;
1510 res =
calc_p_gradients( z_data, new_vertices, yuy_data, interp_IDT_x, interp_IDT_y, cal, p_mat, rc.second, rc.first,
data );
1516 const std::vector<double3>& new_vertices,
1525 data->iteration_data_p.uvmap = uvmap;
1527 auto cost =
calc_cost(z_data, yuy_data, uvmap,
data ? &
data->iteration_data_p.d_vals :
nullptr );
1529 return { cost, grad };
1534 normalize_mat.vals[0] = 0.353692440000000;
1535 normalize_mat.vals[1] = 0.266197740000000;
1536 normalize_mat.vals[2] = 1.00926010000000;
1537 normalize_mat.vals[3] = 0.000673204490000000;
1538 normalize_mat.vals[4] = 0.355085250000000;
1539 normalize_mat.vals[5] = 0.266275050000000;
1540 normalize_mat.vals[6] = 1.01145800000000;
1541 normalize_mat.vals[7] = 0.000675013750000000;
1542 normalize_mat.vals[8] = 414.205570000000;
1543 normalize_mat.vals[9] = 313.341060000000;
1544 normalize_mat.vals[10] = 1187.34590000000;
1545 normalize_mat.vals[11] = 0.791570250000000;
1558 AC_LOG(
DEBUG,
" depth resolution= " << width <<
"x" << height );
1560 bool const XGA = ( width == 1024 && height == 768 );
1561 bool const VGA = ( width == 640 && height == 480 );
1563 if( ! XGA && ! VGA )
1565 throw std::runtime_error(
to_string() << width <<
"x" << height
1566 <<
" this resolution is not supported" );
1571 AC_LOG(
DEBUG,
" changing IR threshold: " << grad_ir_threshold <<
" -> " << 2.5 <<
" (because of resolution)" );
1572 grad_ir_threshold = 2.5;
1574 if (use_enhanced_preprocessing)
1580 grad_ir_low_th = 1.5;
1581 grad_ir_high_th = 3.5;
1583 grad_z_high_th = 100;
1588 grad_ir_high_th = 2.5;
1590 grad_z_high_th = 80;
1597 grad_ir_low_th = std::numeric_limits<double>::max();
1598 grad_ir_high_th = 3.5;
1600 grad_z_high_th = std::numeric_limits<double>::max();
1604 grad_ir_low_th = std::numeric_limits<double>::max();
1605 grad_ir_high_th = 2.5;
1607 grad_z_high_th = std::numeric_limits<double>::max();
1612 min_weighted_edge_per_section_depth = 50. * ( 480 * 640 ) / ( width * height );
1617 AC_LOG(
DEBUG,
" RGB resolution= " << width <<
"x" << height );
1618 auto area = width *
height;
1619 size_t const hd_area = 1920 * 1080;
1620 move_threshold_pix_num = 3
e-5 * area;
1621 move_last_success_thresh_pix_num = 0.1 * area;
1622 max_xy_movement_per_calibration[0] = 10. * area / hd_area;
1623 max_xy_movement_per_calibration[1] = max_xy_movement_per_calibration[2] = 2. * area / hd_area;
1624 max_xy_movement_from_origin = 20. * area / hd_area;
1625 min_weighted_edge_per_section_rgb = 0.05 * hd_area / area;
1646 template<
typename T >
1649 f.write( (
char const *)&o,
sizeof( o ) );
1654 calib const & rgb_calibration,
1663 throw std::runtime_error(
"failed to open file:\n" + path );
1671 double k_depth[9] = { _intr_depth.
fx, 0, _intr_depth.
ppx,
1672 0, _intr_depth.
fy, _intr_depth.
ppy,
1674 for(
auto i = 0;
i < 9;
i++ )
1685 double k_rgb[9] = { _intr_rgb.
fx, 0, _intr_rgb.
ppx,
1686 0, _intr_rgb.
fy, _intr_rgb.
ppy,
1690 for(
auto i = 0;
i < 9;
i++ )
1695 for(
auto i = 0;
i < 5;
i++ )
1702 for(
auto i = 0;
i < 9;
i++ )
1707 for(
auto i = 0;
i < 3;
i++ )
1730 auto & cal_info =
_k_to_DSM->get_calibration_info();
1731 auto & cal_regs =
_k_to_DSM->get_calibration_registers();
1732 write_to_file( &cal_info,
sizeof( cal_info ), dir,
"cal.info" );
1733 write_to_file( &cal_regs,
sizeof( cal_regs ), dir,
"cal.registers" );
1746 catch( std::exception
const & err )
1748 AC_LOG( ERROR,
"Failed to write data: " << err.what() );
1752 AC_LOG( ERROR,
"Failed to write data (unknown error)" );
1757 const std::vector<double3>& new_vertices,
1764 auto grads_over_norm
1771 auto unit_grad = grad.normalize( grad_norm );
1776 auto t = t_vals.sum();
1782 auto movement = unit_grad * step_size;
1795 auto iter_count = 0;
1796 while( diff >= step_size *
t 1811 if(diff >= step_size *
t )
1813 new_params = curr_params;
1893 const std::vector<double3>& new_vertices,
1895 calib& optimaized_calibration,
1896 calib& new_rgb_calib_for_k_to_dsm,
1906 size_t n_iterations = 0;
1907 auto curr = params_curr;
1912 curr.cost = res.first;
1913 curr.calib_gradients = res.second;
1915 <<
" cost= " <<
AC_D_PREC << curr.cost );
1943 auto delta = params_new.
cost - curr.cost;
1954 AC_LOG(
DEBUG,
" exceeding max iterations --> stopping");
1963 " optimize_p finished after " << n_iterations <<
" iterations; cost " 1974 return n_iterations;
1997 params_orig.
cost = res.first;
2022 std::vector<double3> cand_vertices =
_z.
vertices;
2023 auto dsm_regs_cand = new_dsm_regs;
2026 calib calib_candidate = new_calib;
2027 calib calib_k_to_dsm_candidate = new_calib;
2032 data.cycle_data_p.cycle = cycle;
2036 data.cycle_data_p.new_calib = new_calib;
2037 data.cycle_data_p.new_k_depth = new_k_depth;
2038 data.cycle_data_p.new_params = new_params;
2039 data.cycle_data_p.new_dsm_params = new_dsm_params;
2040 data.cycle_data_p.new_dsm_regs = new_dsm_regs;
2041 data.cycle_data_p.new_vertices = new_vertices;
2042 data.cycle_data_p.optimaized_calib_candidate = optimaized_calib_candidate;
2052 AC_LOG(
INFO,
"CYCLE " << data.cycle_data_p.cycle <<
" started with: cost = " <<
AC_D_PREC << new_params.
cost);
2074 calib_k_to_dsm_candidate = calib_candidate;
2078 data.k2dsm_data_p.dsm_params_cand = dsm_candidate;
2079 data.k2dsm_data_p.vertices = cand_vertices;
2080 data.k2dsm_data_p.dsm_pre_process_data =
_k_to_DSM->get_pre_process_data();
2093 optimaized_calib_candidate,
2099 if( params_candidate.
cost < last_cost )
2103 AC_LOG(
DEBUG,
"No change required (probably very good starting point)");
2104 new_k_to_dsm_calib = new_calib;
2108 AC_LOG(
DEBUG,
" cost is a regression; stopping -- not using last cycle" );
2112 new_params = params_candidate;
2114 new_calib = calib_candidate;
2115 new_k_to_dsm_calib = calib_k_to_dsm_candidate;
2116 new_k_depth = k_depth_candidate;
2117 new_dsm_params = dsm_candidate;
2118 new_dsm_regs = dsm_regs_cand;
2119 last_cost = new_params.
cost;
2120 new_vertices = cand_vertices;
2126 "Calibration converged; cost " <<
AC_D_PREC << params_orig.
cost <<
" --> " 2127 << new_params.
cost );
bool use_enhanced_preprocessing
std::vector< uint8_t > relevant_pixels_image
void adjust_params_to_manual_mode()
std::vector< double3 > vertices
bool is_movement_in_images(movement_inputs_for_frame const &prev, movement_inputs_for_frame const &curr, movement_result_data *result_data, double const move_thresh_pix_val, double const move_threshold_pix_num, size_t width, size_t height)
rs2_extrinsics_double get_extrinsics() const
std::shared_ptr< k_to_DSM > _k_to_DSM
void set_rgb_resolution(size_t width, size_t height)
std::vector< double3 > orig_vertices
double pix_per_section_depth_th
float calc_intensity(const cv::Mat &src)
static void deproject_pixel_to_point(double point[3], const struct rs2_intrinsics_double *intrin, const double pixel[2], double depth)
std::vector< byte > is_inside
p_matrix normalize(double norm) const
std::vector< double > valid_location_rc_y
optimization_params _params_curr
double const move_thresh_pix_val
static p_matrix calc_gradients(const z_frame_data &z_data, const std::vector< double3 > &new_vertices, const yuy2_frame_data &yuy_data, const std::vector< double2 > &uv, const calib &cal, const p_matrix &p_mat, data_collect *data=nullptr)
rs2_dsm_params orig_dsm_params
void write_to_file(void const *data, size_t cb, std::string const &dir, char const *filename)
static std::vector< double > depth_mean(std::vector< double > const &local_x, std::vector< double > const &local_y)
double matrix_norm() const
static std::vector< double > get_direction_deg(std::vector< double > const &gradient_x, std::vector< double > const &gradient_y)
const int N_BASIC_DIRECTIONS
movement_result_data movement_result
calib _optimaized_calibration
std::vector< double > valid_location_rc
static std::vector< double > find_local_values_min(std::vector< double > const &local_values)
optimization_params next_params
void set_depth_resolution(size_t width, size_t height, rs2_digital_gain digital_gain)
GLdouble GLdouble GLdouble y2
Video DSM (Digital Sync Module) parameters for calibration (same layout as in FW ac_depth_params) Thi...
std::vector< double > valid_edge_sub_pixel
std::vector< double > local_y
std::vector< double2 > uvmap
size_t optimize_p(const optimization_params ¶ms_curr, const std::vector< double3 > &new_vertices, optimization_params ¶ms_new, calib &optimaized_calibration, calib &new_rgb_calib_for_k_to_dsm, rs2_intrinsics_double &new_z_k, std::function< void(data_collect const &data)> cb, data_collect &data)
GLsizei const GLchar *const * path
std::vector< double > grad_in_direction_valid
void set_final_data(const std::vector< double3 > &vertices, const p_matrix &p_mat, const p_matrix &p_mat_opt=p_matrix())
cycle_data_params cycle_data_p
std::vector< double > local_edges
rs2_intrinsics_double get_intrinsics() const
std::vector< double > interpolation(std::vector< T > const &grid_points, std::vector< double > const x[], std::vector< double > const y[], size_t dim, size_t valid_size, size_t valid_width)
std::vector< double > edge_sub_pixel
std::vector< double3 > subedges2vertices(z_frame_data &z_data, const rs2_intrinsics_double &intrin, double depth_units)
std::pair< double, p_matrix > calc_cost_and_grad(const z_frame_data &z_data, const std::vector< double3 > &new_vertices, const yuy2_frame_data &yuy_data, const calib &cal, const p_matrix &p_mat, data_collect *data=nullptr)
std::vector< ir_t > ir_frame
calib decompose(p_matrix const &mat, calib const &)
std::vector< uint8_t > last_successful_lum_frame
std::vector< direction > get_direction(std::vector< double > gradient_x, std::vector< double > gradient_y)
GLint GLint GLsizei GLsizei GLsizei depth
double edge_thresh4_logic_lum
std::vector< double > edges
std::vector< double > gradient_x
std::vector< double > local_region[4]
GLdouble GLdouble GLdouble w
GLsizei const GLchar *const * string
std::vector< uint8_t > prev_lum_frame
void set_cycle_data(const std::vector< double3 > &vertices, const rs2_intrinsics_double &k_depth, const p_matrix &p_mat, const algo_calibration_registers &dsm_regs_cand, const rs2_dsm_params_double &dsm_params_cand)
rs2_dsm_params _original_dsm_params
std::vector< double > grad_in_direction
double edges_per_direction_ratio_th
std::vector< double > sub_points
int back_tracking_line_search_iters
void write_obj(std::fstream &f, T const &o)
std::vector< double > gradient
std::vector< double > local_region_x[4]
rs2_dsm_params _final_dsm_params
std::vector< byte > get_luminance_from_yuy2(std::vector< yuy_t > const &yuy2_imagh)
optimizer(settings const &, bool debug_mode=false)
GLenum GLenum GLsizei void * image
std::vector< double > subpixels_y_round
void adjust_params_to_auto_mode()
void write_matlab_camera_params_file(rs2_intrinsics const &_intr_depth, calib const &rgb_calibration, float _depth_units, std::string const &dir, char const *filename)
bool get_cycle_data_from_bin
p_matrix const calc_p_mat() const
double move_threshold_pix_num
void sample_by_mask(std::vector< T > &filtered, std::vector< T > const &origin, std::vector< byte > const &valid_edge_by_ir, size_t const width, size_t const height)
size_t optimize(std::function< void(data_collect const &data) > iteration_callback=nullptr)
std::vector< byte > section_map_depth
rs2_dsm_params_double _dsm_params_cand_from_bin
void clip_ac_scaling(rs2_dsm_params_double const &ac_data_orig, rs2_dsm_params_double &ac_data_new) const
void write_vector_to_file(std::vector< T > const &v, std::string const &dir, char const *filename)
std::vector< byte > supressed_edges
std::vector< yuy_t > orig_frame
rs2_intrinsics_double _k_dapth_from_bin
std::vector< double > subpixels_x
static std::pair< int, int > get_next_index(direction dir, int i, int j, size_t width, size_t height)
rs2_dsm_params const & get_dsm_params() const
std::vector< double3 > _vertices_from_bin
void set_z_data(std::vector< z_t > &&z_data, rs2_intrinsics_double const &depth_intrinsics, rs2_dsm_params const &dms_params, algo_calibration_info const &cal_info, algo_calibration_registers const &cal_regs, float depth_units)
std::vector< byte > is_supressed
std::vector< double > edges_IDTy
std::vector< byte > valid_section_map
int min_section_with_enough_edges
std::string to_string() const
bool movement_from_last_success
std::vector< double > local_values
void sum_per_section(std::vector< double > &sum_weights_per_section, std::vector< byte > const §ion_map, std::vector< double > const &weights, size_t num_of_sections)
p_matrix normalized_grads
std::vector< byte > section_map_edges
GLint GLsizei GLsizei height
std::vector< double > local_region_y[4]
std::vector< double > subpixels_x_round
std::vector< double > valid_direction_per_pixel
std::vector< double > direction_per_pixel
rs2_intrinsics_double orig_intrinsics
double max_global_los_scaling_step
void set_yuy_data(std::vector< yuy_t > &&yuy_data, std::vector< yuy_t > &&prev_yuy_data, std::vector< yuy_t > &&last_successful_yuy_data, calib const &calibration)
double const max_K2DSM_iters
std::vector< double > edges
std::vector< double > local_x
algo_calibration_registers _dsm_regs_cand_from_bin
double dir_std_th[N_BASIC_DIRECTIONS]
std::vector< double > valid_gradient_y
std::vector< double > biliniar_interp(std::vector< double > const &vals, size_t width, size_t height, uvmap_t const &uv)
void depth_filter(std::vector< T > &filtered, std::vector< T > const &origin, std::vector< byte > const &valid_edge_by_ir, size_t const width, size_t const height)
rs2_intrinsics_double get_new_z_intrinsics_from_new_calib(const rs2_intrinsics_double &orig, const calib &new_c, const calib &orig_c)
std::vector< double > gradient_y
double saturation_ratio_th
std::vector< uint8_t > lum_frame
double move_last_success_thresh_pix_num
void adjust_params_to_apd_gain()
std::vector< byte > section_map
static std::vector< double > sum_gradient_depth(std::vector< double > const &gradient, std::vector< double > const &direction_per_pixel)
std::vector< uint8_t > is_suppressed(std::vector< double > const &local_edges, size_t valid_size)
#define AC_LOG(TYPE, MSG)
double const max_scaling_step
std::vector< yuy_t > prev_frame
float dot_product(const float(&a)[3], const float(&b)[3])
optimization_params params
std::vector< direction > directions
size_t num_of_sections_for_edge_distribution_y
float3 transform(const rs2_extrinsics *extrin, const float3 &point)
Cross-stream extrinsics: encodes the topology describing how the different devices are oriented...
static std::pair< int, int > get_prev_index(direction dir, int i, int j, size_t width, size_t height)
void copy_to(rs2_dsm_params &o)
std::vector< double > subpixels_y
double calc_cost_per_vertex_diff(z_frame_data const &z_data, yuy2_frame_data const &yuy_data, const uvmap_t &uvmap_old, const uvmap_t &uvmap_new)
GLuint GLfloat GLfloat GLfloat x1
void set_ir_data(std::vector< ir_t > &&ir_data, size_t width, size_t height)
static const struct @18 vertices[3]
int max_optimization_iters
std::vector< byte > find_valid_depth_edges(std::vector< double > const &grad_in_direction, std::vector< byte > const &is_supressed, std::vector< double > const &values_for_subedges, std::vector< double > const &ir_local_edges, const params &p)
static std::vector< double > get_direction_deg2(std::vector< double > const &gradient_x, std::vector< double > const &gradient_y)
calib const & get_calibration() const
static p_matrix calc_p_gradients(const z_frame_data &z_data, const std::vector< double3 > &new_vertices, const yuy2_frame_data &yuy_data, std::vector< double > interp_IDT_x, std::vector< double > interp_IDT_y, const calib &cal, const p_matrix &p_mat, const std::vector< double > &rc, const std::vector< double2 > &xy, data_collect *data=nullptr)
uvmap_t get_texture_map(std::vector< double3 > const &points, const calib &cal, const p_matrix &p_mat)
std::vector< double > valid_directions
std::vector< double > valid_location_rc_x
const GLuint GLenum const void * binary
std::vector< double > valid_gradient_x
double calc_cost(const z_frame_data &z_data, const yuy2_frame_data &yuy_data, const std::vector< double2 > &uv, std::vector< double > *p_interpolated_edges)
coeffs< p_matrix > calc_p_coefs(const z_frame_data &z_data, const std::vector< double3 > &new_vertices, const yuy2_frame_data &yuy_data, const calib &cal, const p_matrix &p_mat, const std::vector< double > &rc, const std::vector< double2 > &xy)
std::vector< double > direction_deg
struct librealsense::algo::depth_to_rgb_calibration::yuy2_frame_data::@0 debug
void zero_margin(std::vector< double > &gradient, size_t margin, size_t width, size_t height)
typename::boost::move_detail::remove_reference< T >::type && move(T &&t) BOOST_NOEXCEPT
std::vector< double > edges_IDT
std::vector< double > local_rc_subpixel
void deproject_sub_pixel(std::vector< double3 > &points, const rs2_intrinsics_double &intrin, std::vector< byte > const &valid_edges, const double *x, const double *y, const double *depth, double depth_units)
void grid_xy(std::vector< double > &gridx, std::vector< double > &gridy, size_t width, size_t height)
rs2_digital_gain
digital gain for RS2_OPTION_DIGITAL_GAIN option.
calib _original_calibration
std::vector< byte > valid_section_map
std::vector< uint8_t > get_logic_edges(std::vector< double > const &edges)
int minimal_full_directions
std::vector< double > closest
std::vector< double > edges
std::vector< double > blur_edges(std::vector< double > const &edges, size_t image_width, size_t image_height)
std::vector< double > fraq_step
std::vector< direction > get_direction2(std::vector< double > gradient_x, std::vector< double > gradient_y)
iteration_data_params iteration_data_p
std::vector< double > values_for_subedges
std::vector< double > edges_IDTx
p_matrix _p_mat_from_bin_opt
optimization_params back_tracking_line_search(optimization_params const &opt_params, const std::vector< double3 > &new_vertices, data_collect *data=nullptr) const
static std::pair< std::vector< double2 >, std::vector< double > > calc_rc(const z_frame_data &z_data, const std::vector< double3 > &new_vertices, const yuy2_frame_data &yuy_data, const calib &cal, const p_matrix &p_mat)
bool movement_from_prev_frame
void section_per_pixel(frame_data const &, size_t section_w, size_t section_h, byte *section_map)
void pinv_3x3(const double in[9], double out[9])
double get_norma(const std::vector< double3 > &vec)
std::vector< double > gradient_y
std::vector< double > gradient_x
std::vector< double > directions
std::vector< byte > valid_edge_pixels_by_ir
calib decompose_p_mat(p_matrix p)
static void project_point_to_pixel(double pixel[2], const struct rs2_intrinsics_double *intrin, const double point[3])
std::vector< yuy_t > last_successful_frame
GLdouble GLdouble GLint GLint const GLdouble * points
std::vector< double > weights
void copy(void *dst, void const *src, size_t size)
size_t num_of_sections_for_edge_distribution_x
double pix_per_section_rgb_th
void write_data_to(std::string const &directory)
rs2_digital_gain digital_gain
movement_result_data movement_prev_valid_result
std::string to_string(T value)
double const move_last_success_thresh_pix_val