diff --git a/unit-tests/algo/depth-to-rgb-calibration/compare-scene.h b/unit-tests/algo/depth-to-rgb-calibration/compare-scene.h new file mode 100644 index 0000000000..3df131308d --- /dev/null +++ b/unit-tests/algo/depth-to-rgb-calibration/compare-scene.h @@ -0,0 +1,183 @@ +// License: Apache 2.0. See LICENSE file in root directory. +// Copyright(c) 2020 Intel Corporation. All Rights Reserved. + + +void compare_scene( std::string const & scene_dir ) +{ + TRACE( "Loading " << scene_dir << " ..." ); + + camera_params ci = read_camera_params( scene_dir, "camera_params" ); + scene_metadata md( scene_dir ); + + algo::optimizer cal; + init_algo( cal, scene_dir, + md.rgb_file, + md.rgb_prev_file, + md.ir_file, + md.z_file, + ci ); + + auto& z_data = cal.get_z_data(); + auto& ir_data = cal.get_ir_data(); + auto& yuy_data = cal.get_yuy_data(); + auto& depth_data = cal.get_depth_data(); + + //--- + auto rgb_h = ci.rgb.height; + auto rgb_w = ci.rgb.width; + auto z_h = ci.z.height; + auto z_w = ci.z.width; + auto num_of_calib_elements = 32; + + CHECK( compare_to_bin_file< double >( yuy_data.edges, scene_dir, "YUY2_edge", rgb_w, rgb_h, "double_00", compare_same_vectors ) ); + CHECK( compare_to_bin_file< double >( yuy_data.edges_IDT, scene_dir, "YUY2_IDT", rgb_w, rgb_h, "double_00", compare_same_vectors ) ); + CHECK( compare_to_bin_file< double >( yuy_data.edges_IDTx, scene_dir, "YUY2_IDTx", rgb_w, rgb_h, "double_00", compare_same_vectors ) ); + CHECK( compare_to_bin_file< double >( yuy_data.edges_IDTy, scene_dir, "YUY2_IDTy", rgb_w, rgb_h, "double_00", compare_same_vectors ) ); + + //--- + CHECK( compare_to_bin_file< double >( ir_data.ir_edges, scene_dir, "I_edge", z_w, z_h, "double_00", compare_same_vectors ) ); + + //--- + CHECK( compare_to_bin_file< double >( z_data.edges, scene_dir, "Z_edge", z_w, z_h, "double_00", compare_same_vectors ) ); + CHECK( compare_to_bin_file< double >( z_data.supressed_edges, scene_dir, "Z_edgeSupressed", z_w, z_h, "double_00", compare_same_vectors ) ); + CHECK( compare_to_bin_file< byte >( z_data.directions, scene_dir, "Z_dir", z_w, z_h, "uint8_00", compare_same_vectors ) ); + + CHECK( compare_to_bin_file< double >( z_data.subpixels_x, scene_dir, "Z_edgeSubPixel", z_w, z_h, "double_01", compare_same_vectors ) ); + CHECK( compare_to_bin_file< double >( z_data.subpixels_y, scene_dir, "Z_edgeSubPixel", z_w, z_h, "double_00", compare_same_vectors ) ); + + CHECK( compare_to_bin_file< double >( z_data.weights, scene_dir, "weightsT", 1, md.n_edges, "double_00", compare_same_vectors ) ); + CHECK( compare_to_bin_file< double >( z_data.closest, scene_dir, "Z_valuesForSubEdges", z_w, z_h, "double_00", compare_same_vectors ) ); + CHECK( compare_to_bin_file< algo::double3 >( z_data.vertices, scene_dir, bin_file( "vertices", 3, md.n_edges, "double_00" ) + ".bin", md.n_edges, 1, compare_same_vectors ) ); + +#if 0 + // smearing + CHECK( compare_to_bin_file< double >( depth_data.gradient_x, scene_dir, "Zx", z_w, z_h, "double_00", compare_same_vectors ) ); + CHECK( compare_to_bin_file< double >( depth_data.gradient_y, scene_dir, "Zy", z_w, z_h, "double_00", compare_same_vectors ) ); + CHECK( compare_to_bin_file< double >( ir_data.gradient_x, scene_dir, "Ix", z_w, z_h, "double_00", compare_same_vectors ) ); + CHECK( compare_to_bin_file< double >( ir_data.gradient_y, scene_dir, "Iy", z_w, z_h, "double_00", compare_same_vectors ) ); + CHECK( compare_to_bin_file< double >( ir_data.edges2, scene_dir, "iEdge", z_w, z_h, "double_00", compare_same_vectors ) ); + //CHECK(compare_to_bin_file< double >(depth_data.edges2, scene_dir, "zedge", z_w, z_h, "double_00", compare_same_vectors)); + CHECK( compare_to_bin_file< uint8_t >( depth_data.section_map_depth, scene_dir, "sectionMapDepth", z_w, z_h, "uint8_00", compare_same_vectors ) ); + CHECK( compare_to_bin_file< uint8_t >( ir_data.valid_edge_pixels_by_ir, scene_dir, "validEdgePixelsByIR", z_w, z_h, "uint8_00", compare_same_vectors ) ); + CHECK( compare_to_bin_file< double >( ir_data.valid_location_rc_x, scene_dir, "gridXValid", 1, 105794, "double_00" ), 105794, 1, compare_same_vectors )); + CHECK( compare_to_bin_file< double >( ir_data.valid_location_rc_y, scene_dir, "gridYValid", 1, 105794, "double_00" ), 105794, 1, compare_same_vectors )); + CHECK( compare_to_bin_file< double >( ir_data.valid_location_rc, scene_dir, "locRC", 2, 105794, "double_00" ), 105794, 2, compare_same_vectors )); + CHECK( compare_to_bin_file< uint8_t >( ir_data.valid_section_map, scene_dir, "sectionMapValid", 1, 105794, "uint8_00" ), 105794, 1, compare_same_vectors )); + CHECK( compare_to_bin_file< double >( ir_data.valid_gradient_x, scene_dir, "IxValid", 1, 105794, "double_00" ), 105794, 1, compare_same_vectors )); + CHECK( compare_to_bin_file< double >( ir_data.valid_gradient_y, scene_dir, "IyValid", 1, 105794, "double_00" ), 105794, 1, compare_same_vectors )); + CHECK( compare_to_bin_file< double >( ir_data.direction_deg, scene_dir, "directionInDeg", 1, 105794, "double_00" ), 105794, 1, compare_same_vectors )); + //CHECK(compare_to_bin_file< double >(ir_data.directions, scene_dir, "directionIndex", 1, 105794, "double_00"), 105794, 1, compare_same_vectors)); // it passed, depends on index definition + CHECK( compare_to_bin_file< double >( ir_data.direction_per_pixel, scene_dir, "dirPerPixel", 2, 105794, "double_00" ), 105794, 2, compare_same_vectors )); + CHECK( compare_to_bin_file< double >( ir_data.local_region[0], scene_dir, "localRegion", 2, 105794, "double_00" ), 105794, 2, compare_same_vectors )); + CHECK( compare_to_bin_file< double >( ir_data.local_region[1], scene_dir, "localRegion", 2, 105794, "double_01" ), 105794, 2, compare_same_vectors )); + CHECK( compare_to_bin_file< double >( ir_data.local_region[2], scene_dir, "localRegion", 2, 105794, "double_02" ), 105794, 2, compare_same_vectors )); + CHECK( compare_to_bin_file< double >( ir_data.local_region[3], scene_dir, "localRegion", 2, 105794, "double_03" ), 105794, 2, compare_same_vectors )); + + CHECK( compare_to_bin_file< double >( ir_data.local_region_x[0], scene_dir, "localRegion_x", 1, 105794, "double_00" ), 105794, 1, compare_same_vectors )); + CHECK( compare_to_bin_file< double >( ir_data.local_region_x[1], scene_dir, "localRegion_x", 1, 105794, "double_01" ), 105794, 1, compare_same_vectors )); + CHECK( compare_to_bin_file< double >( ir_data.local_region_x[2], scene_dir, "localRegion_x", 1, 105794, "double_02" ), 105794, 1, compare_same_vectors )); + CHECK( compare_to_bin_file< double >( ir_data.local_region_x[3], scene_dir, "localRegion_x", 1, 105794, "double_03" ), 105794, 1, compare_same_vectors )); + + CHECK( compare_to_bin_file< double >( ir_data.local_region_y[0], scene_dir, "localRegion_y", 1, 105794, "double_00" ), 105794, 1, compare_same_vectors )); + CHECK( compare_to_bin_file< double >( ir_data.local_region_y[1], scene_dir, "localRegion_y", 1, 105794, "double_01" ), 105794, 1, compare_same_vectors )); + CHECK( compare_to_bin_file< double >( ir_data.local_region_y[2], scene_dir, "localRegion_y", 1, 105794, "double_02" ), 105794, 1, compare_same_vectors )); + CHECK( compare_to_bin_file< double >( ir_data.local_region_y[3], scene_dir, "localRegion_y", 1, 105794, "double_03" ), 105794, 1, compare_same_vectors )); + + CHECK( compare_to_bin_file< double >( ir_data.local_edges, scene_dir, "localEdges", 4, 105794, "double_00" ), 105794, 4, compare_same_vectors )); + CHECK( compare_to_bin_file< uint8_t >( ir_data.is_supressed, scene_dir, "isSupressed", 1, 105794, "uint8_00" ), 105794, 1, compare_same_vectors )); +#endif + + // --- + TRACE( "\nChecking scene validity:" ); + + CHECK( cal.is_scene_valid() == md.is_scene_valid ); + + // edge distribution + CHECK( compare_to_bin_file< double >( z_data.sum_weights_per_section, scene_dir, "depthEdgeWeightDistributionPerSectionDepth", 1, 4, "double_00", compare_same_vectors ) ); + + CHECK( compare_to_bin_file< byte >( z_data.section_map, scene_dir, "sectionMapDepth_trans", 1, md.n_edges, "uint8_00", compare_same_vectors ) ); + CHECK( compare_to_bin_file< byte >( yuy_data.section_map, scene_dir, "sectionMapRgb_trans", 1, rgb_w*rgb_h, "uint8_00", compare_same_vectors ) ); + + CHECK( compare_to_bin_file< double >( yuy_data.sum_weights_per_section, scene_dir, "edgeWeightDistributionPerSectionRgb", 1, 4, "double_00", compare_same_vectors ) ); + + // gradient balanced + // TODO NOHA + //CHECK(compare_to_bin_file< double >(z_data.sum_weights_per_direction, scene_dir, "edgeWeightsPerDir", 1, 4, "double_00", compare_same_vectors)); + + // movment check + // 1. dilation + CHECK( compare_to_bin_file< uint8_t >( yuy_data.prev_logic_edges, scene_dir, "logicEdges", rgb_w, rgb_h, "uint8_00", compare_same_vectors ) ); + CHECK( compare_to_bin_file< double >( yuy_data.dilated_image, scene_dir, "dilatedIm", rgb_w, rgb_h, "double_00", compare_same_vectors ) ); + + // 2. gausssian + CHECK( compare_to_bin_file< double >( yuy_data.yuy_diff, scene_dir, "diffIm_01", rgb_w, rgb_h, "double_00", compare_same_vectors ) ); + CHECK( compare_to_bin_file< double >( yuy_data.gaussian_filtered_image, scene_dir, "diffIm", rgb_w, rgb_h, "double_00", compare_same_vectors ) ); + + // 3. movement + CHECK( compare_to_bin_file< double >( yuy_data.gaussian_diff_masked, scene_dir, "IDiffMasked", rgb_w, rgb_h, "double_00", compare_same_vectors ) ); + CHECK( compare_to_bin_file< uint8_t >( yuy_data.move_suspect, scene_dir, "ixMoveSuspect", rgb_w, rgb_h, "uint8_00", compare_same_vectors ) ); + + + //-- + TRACE( "\nOptimizing:" ); + auto cb = [&]( algo::iteration_data_collect const & data ) + { + // data.iteration is 0-based! + REQUIRE( data.iteration < md.n_iterations ); + + auto file = bin_file( "calib_iteration", data.iteration + 1, num_of_calib_elements, 1, "double_00.bin" ); + CHECK( compare_calib_to_bin_file( data.params.curr_calib, data.params.cost, scene_dir, file ) ); + + file = bin_file( "uvmap_iteration", data.iteration + 1, 2, md.n_edges, "double_00.bin" ); + CHECK( compare_to_bin_file< algo::double2 >( data.uvmap, scene_dir, file, md.n_edges, 1, compare_same_vectors ) ); + + file = bin_file( "DVals_iteration", data.iteration + 1, 1, md.n_edges, "double_00.bin" ); + CHECK( compare_to_bin_file< double >( data.d_vals, scene_dir, file, md.n_edges, 1, compare_same_vectors ) ); + + file = bin_file( "DxVals_iteration", data.iteration + 1, 1, md.n_edges, "double_00.bin" ); + CHECK( compare_to_bin_file< double >( data.d_vals_x, scene_dir, file, md.n_edges, 1, compare_same_vectors ) ); + + file = bin_file( "DyVals_iteration", data.iteration + 1, 1, md.n_edges, "double_00.bin" ); + CHECK( compare_to_bin_file< double >( data.d_vals_y, scene_dir, file, md.n_edges, 1, compare_same_vectors ) ); + + file = bin_file( "xy_iteration", data.iteration + 1, 2, md.n_edges, "double_00.bin" ); + CHECK( compare_to_bin_file< algo::double2 >( data.xy, scene_dir, file, md.n_edges, 1, compare_same_vectors ) ); + + file = bin_file( "rc_iteration", data.iteration + 1, 1, md.n_edges, "double_00.bin" ); + CHECK( compare_to_bin_file< double >( data.rc, scene_dir, file, md.n_edges, 1, compare_same_vectors ) ); + + file = bin_file( "xCoeff_P", data.iteration + 1, sizeof( algo::p_matrix ) / sizeof( double ), md.n_edges, "double_00.bin" ); + CHECK( compare_to_bin_file< algo::p_matrix>( data.coeffs_p.x_coeffs, scene_dir, file, md.n_edges, 1, compare_same_vectors ) ); + + /*file = bin_file("xCoeff_Krgb", data.iteration + 1, sizeof(algo::k_matrix) / sizeof(double), md.n_edges, "double_00.bin"); + CHECK(compare_to_bin_file< algo::k_matrix>(data.coeffs_k.x_coeffs, scene_dir, file, md.n_edges, 1, compare_same_vectors, sort_vectors)); + + file = bin_file("yCoeff_Krgb", data.iteration + 1, sizeof(algo::k_matrix) / sizeof(double), md.n_edges, "double_00.bin"); + CHECK(compare_to_bin_file< algo::k_matrix>(data.coeffs_k.y_coeffs, scene_dir, file, md.n_edges, 1, compare_same_vectors, sort_vectors)); + + file = bin_file("xCoeff_R", data.iteration + 1, sizeof(algo::rotation_in_angles) / sizeof(double), md.n_edges, "double_00.bin"); + CHECK(compare_to_bin_file< algo::rotation_in_angles>(data.coeffs_r.x_coeffs, scene_dir, file, md.n_edges, 1, compare_same_vectors, sort_vectors));*/ + + // TODO Avishag + //file = bin_file("grad_iteration", data.iteration + 1, num_of_calib_elements, 1, "double_00.bin"); + //CHECK(compare_calib_to_bin_file(data.params.calib_gradients, 0, scene_dir, file, true)); + }; + + // Our code doesn't count the first iteration; the Matlab code starts at 1 even if it doesn't do anything... + REQUIRE( cal.optimize( cb ) + 1 == md.n_iterations ); + + auto new_calibration = cal.get_calibration(); + auto cost = cal.get_cost(); + + CHECK( compare_calib_to_bin_file( new_calibration, cost, scene_dir, "new_calib", num_of_calib_elements, 1, "double_00" ) ); + + + //-- + TRACE( "\nChecking output validity:" ); + // Pixel movement is OK, but some sections have negative cost + CHECK( cal.is_valid_results() == md.is_output_valid ); + + CHECK( cal.calc_correction_in_pixels() == approx( md.correction_in_pixels ) ); + + CHECK( compare_to_bin_file< double >( z_data.cost_diff_per_section, scene_dir, "costDiffPerSection", 4, 1, "double_00", compare_same_vectors ) ); +} diff --git a/unit-tests/algo/depth-to-rgb-calibration/scene-data.h b/unit-tests/algo/depth-to-rgb-calibration/scene-data.h index 9c8063e622..bca06ffd61 100644 --- a/unit-tests/algo/depth-to-rgb-calibration/scene-data.h +++ b/unit-tests/algo/depth-to-rgb-calibration/scene-data.h @@ -108,6 +108,8 @@ struct scene_metadata uint64_t n_iterations; // how many steps through optimization, and how many iteration file sets double correction_in_pixels; // XY movement uint64_t n_edges; // strong edges, i.e. after suppression + bool is_scene_valid; + bool is_output_valid; std::string rgb_file; std::string rgb_prev_file; // TODO: looks like these need to be turned around!!! std::string ir_file; @@ -125,6 +127,9 @@ struct scene_metadata f.read( (char*)&correction_in_pixels, sizeof( correction_in_pixels ) ); f.read( (char*)&n_edges, sizeof( n_edges ) ); f.read( (char*)&n_iterations, sizeof( n_iterations ) ); + byte b; + f.read( (char*)&b, 1 ); is_scene_valid = b; + f.read( (char*)&b, 1 ); is_output_valid = b; f.close(); } }; diff --git a/unit-tests/algo/depth-to-rgb-calibration/test-reproduction.cpp b/unit-tests/algo/depth-to-rgb-calibration/test-reproduction.cpp index 01cfb3c1a0..982041052b 100644 --- a/unit-tests/algo/depth-to-rgb-calibration/test-reproduction.cpp +++ b/unit-tests/algo/depth-to-rgb-calibration/test-reproduction.cpp @@ -46,7 +46,7 @@ int main( int argc, char * argv[] ) algo::calib calibration; read_binary_file( dir, "rgb.calib", &calibration ); - camera_info camera; + camera_params camera; camera.rgb = calibration.get_intrinsics(); camera.extrinsics = calibration.get_extrinsics(); algo::rs2_intrinsics_double d_intr; // intrinsics written in double! diff --git a/unit-tests/algo/depth-to-rgb-calibration/test-scene-2.cpp b/unit-tests/algo/depth-to-rgb-calibration/test-scene-2.cpp new file mode 100644 index 0000000000..f530b7300d --- /dev/null +++ b/unit-tests/algo/depth-to-rgb-calibration/test-scene-2.cpp @@ -0,0 +1,18 @@ +// License: Apache 2.0. See LICENSE file in root directory. +// Copyright(c) 2020 Intel Corporation. All Rights Reserved. + +//#cmake:add-file ../../../src/algo/depth-to-rgb-calibration/*.cpp + +#include "d2rgb-common.h" +#include "compare-to-bin-file.h" +#include "compare-scene.h" + + +TEST_CASE("Scene 2", "[d2rgb]") +{ + std::string scene_dir( "..\\unit-tests\\algo\\depth-to-rgb-calibration\\19.2.20" ); +// std::string scene_dir( "C:\\work\\autocal" ); + scene_dir += "\\F9440687\\LongRange_D_768x1024_RGB_1920x1080\\2\\"; + + compare_scene( scene_dir ); +} diff --git a/unit-tests/algo/depth-to-rgb-calibration/test-scenes.cpp b/unit-tests/algo/depth-to-rgb-calibration/test-scenes.cpp new file mode 100644 index 0000000000..f52579ef48 --- /dev/null +++ b/unit-tests/algo/depth-to-rgb-calibration/test-scenes.cpp @@ -0,0 +1,65 @@ +// License: Apache 2.0. See LICENSE file in root directory. +// Copyright(c) 2020 Intel Corporation. All Rights Reserved. + +//#cmake:add-file ../../../src/algo/depth-to-rgb-calibration/*.cpp + +// We have our own main +#define NO_CATCH_CONFIG_MAIN +#define CATCH_CONFIG_RUNNER + +#include "d2rgb-common.h" +#include "compare-to-bin-file.h" + +#include "compare-scene.h" +#include "../../filesystem.h" + + +int main( int argc, char * argv[] ) +{ + Catch::Session session; + LOG_TO_STDOUT.enable( false ); + + bool ok = true; + // Each of the arguments is the path to a directory to simulate + // We skip argv[0] which is the path to the executable + // We don't complain if no arguments -- that's how we'll run as part of unit-testing + for( int i = 1; i < argc; ++i ) + { + try + { + char const * dir = argv[i]; + if( !strcmp( dir, "-v" ) ) + { + LOG_TO_STDOUT.enable(); + continue; + } + TRACE( "\n\nProcessing: " << dir << " ..." ); + + glob( dir, "*.rsc", + [&]( std::string const & match ) + { + + + Catch::Ptr< Catch::Config > config( new Catch::Config ); + Catch::Ptr< Catch::IStreamingReporter > m_reporter = Catch::getRegistryHub().getReporterRegistry().create( "compact", config.get() ); + Catch::RunContext context( config.get(), m_reporter ); + + REQUIRE_NOTHROW( compare_scene( get_parent( join( dir, match ) ) + native_separator )); + } ); + + TRACE( "done!\n\n" ); + } + catch( std::exception const & e ) + { + std::cerr << "caught exception: " << e.what() << std::endl; + ok = false; + } + catch( ... ) + { + std::cerr << "caught unknown exception!" << std::endl; + ok = false; + } + } + + return !ok; +} diff --git a/unit-tests/algo/depth-to-rgb-calibration/test-weights-2.cpp b/unit-tests/algo/depth-to-rgb-calibration/test-weights-2.cpp deleted file mode 100644 index 76197dd300..0000000000 --- a/unit-tests/algo/depth-to-rgb-calibration/test-weights-2.cpp +++ /dev/null @@ -1,192 +0,0 @@ -// License: Apache 2.0. See LICENSE file in root directory. -// Copyright(c) 2020 Intel Corporation. All Rights Reserved. - -//#cmake:add-file ../../../src/algo/depth-to-rgb-calibration/*.cpp - -#include "d2rgb-common.h" -#include "compare-to-bin-file.h" - - -TEST_CASE("Weights calc", "[d2rgb]") -{ - std::string scene_dir( "..\\unit-tests\\algo\\depth-to-rgb-calibration\\19.2.20" ); -// std::string scene_dir( "C:\\work\\autocal" ); - scene_dir += "\\F9440687\\LongRange_D_768x1024_RGB_1920x1080\\2\\"; - - TRACE( "Loading " << scene_dir << " ..." ); - - camera_params ci = read_camera_params( scene_dir, "camera_params" ); - scene_metadata md( scene_dir ); - - algo::optimizer cal; - init_algo( cal, scene_dir, - md.rgb_file, - md.rgb_prev_file, - md.ir_file, - md.z_file, - ci); - - auto& z_data = cal.get_z_data(); - auto& ir_data = cal.get_ir_data(); - auto& yuy_data = cal.get_yuy_data(); - auto& depth_data = cal.get_depth_data(); - - //--- - auto rgb_h = ci.rgb.height; - auto rgb_w = ci.rgb.width; - auto z_h = ci.z.height; - auto z_w = ci.z.width; - auto num_of_calib_elements = 32; - - CHECK(compare_to_bin_file< double >(yuy_data.edges, scene_dir, "YUY2_edge", rgb_w, rgb_h, "double_00", compare_same_vectors)); - CHECK(compare_to_bin_file< double >(yuy_data.edges_IDT, scene_dir, "YUY2_IDT", rgb_w, rgb_h, "double_00", compare_same_vectors)); - CHECK(compare_to_bin_file< double >(yuy_data.edges_IDTx, scene_dir, "YUY2_IDTx", rgb_w, rgb_h, "double_00", compare_same_vectors)); - CHECK(compare_to_bin_file< double >(yuy_data.edges_IDTy, scene_dir, "YUY2_IDTy", rgb_w, rgb_h, "double_00", compare_same_vectors)); - - //--- - CHECK(compare_to_bin_file< double >(ir_data.ir_edges, scene_dir, "I_edge", z_w, z_h, "double_00", compare_same_vectors)); - - //--- - CHECK(compare_to_bin_file< double >(z_data.edges, scene_dir, "Z_edge", z_w, z_h, "double_00", compare_same_vectors)); - CHECK(compare_to_bin_file< double >(z_data.supressed_edges, scene_dir, "Z_edgeSupressed", z_w, z_h, "double_00", compare_same_vectors)); - CHECK(compare_to_bin_file< byte >(z_data.directions, scene_dir, "Z_dir", z_w, z_h, "uint8_00", compare_same_vectors)); - - CHECK(compare_to_bin_file< double >(z_data.subpixels_x, scene_dir, "Z_edgeSubPixel", z_w, z_h, "double_01", compare_same_vectors)); - CHECK(compare_to_bin_file< double >(z_data.subpixels_y, scene_dir, "Z_edgeSubPixel", z_w, z_h, "double_00", compare_same_vectors)); - - CHECK(compare_to_bin_file< double >(z_data.weights, scene_dir, "weightsT", 1, md.n_edges,"double_00", compare_same_vectors)); - CHECK(compare_to_bin_file< double >(z_data.closest, scene_dir, "Z_valuesForSubEdges", z_w, z_h, "double_00", compare_same_vectors)); - CHECK(compare_to_bin_file< algo::double3 >( z_data.vertices, scene_dir, bin_file( "vertices", 3, md.n_edges, "double_00" ) + ".bin", md.n_edges, 1, compare_same_vectors)); - -#if 0 - // smearing - CHECK(compare_to_bin_file< double >(depth_data.gradient_x, scene_dir, "Zx", z_w, z_h, "double_00", compare_same_vectors)); - CHECK(compare_to_bin_file< double >(depth_data.gradient_y, scene_dir, "Zy", z_w, z_h, "double_00", compare_same_vectors)); - CHECK(compare_to_bin_file< double >(ir_data.gradient_x, scene_dir, "Ix", z_w, z_h, "double_00", compare_same_vectors)); - CHECK(compare_to_bin_file< double >(ir_data.gradient_y, scene_dir, "Iy", z_w, z_h, "double_00", compare_same_vectors)); - CHECK(compare_to_bin_file< double >(ir_data.edges2, scene_dir, "iEdge", z_w, z_h, "double_00", compare_same_vectors)); - //CHECK(compare_to_bin_file< double >(depth_data.edges2, scene_dir, "zedge", z_w, z_h, "double_00", compare_same_vectors)); - CHECK(compare_to_bin_file< uint8_t >(depth_data.section_map_depth, scene_dir, "sectionMapDepth", z_w, z_h, "uint8_00", compare_same_vectors)); - CHECK(compare_to_bin_file< uint8_t >(ir_data.valid_edge_pixels_by_ir, scene_dir, "validEdgePixelsByIR", z_w, z_h, "uint8_00", compare_same_vectors)); - CHECK(compare_to_bin_file< double >(ir_data.valid_location_rc_x, scene_dir, "gridXValid", 1, 105794, "double_00"), 105794, 1, compare_same_vectors)); - CHECK(compare_to_bin_file< double >(ir_data.valid_location_rc_y, scene_dir, "gridYValid", 1, 105794, "double_00"), 105794, 1, compare_same_vectors)); - CHECK(compare_to_bin_file< double >(ir_data.valid_location_rc, scene_dir, "locRC", 2, 105794, "double_00"), 105794, 2, compare_same_vectors)); - CHECK(compare_to_bin_file< uint8_t >(ir_data.valid_section_map, scene_dir, "sectionMapValid", 1, 105794, "uint8_00"), 105794, 1, compare_same_vectors)); - CHECK(compare_to_bin_file< double >(ir_data.valid_gradient_x, scene_dir, "IxValid", 1, 105794, "double_00"), 105794, 1, compare_same_vectors)); - CHECK(compare_to_bin_file< double >(ir_data.valid_gradient_y, scene_dir, "IyValid", 1, 105794, "double_00"), 105794, 1, compare_same_vectors)); - CHECK(compare_to_bin_file< double >(ir_data.direction_deg, scene_dir, "directionInDeg", 1, 105794, "double_00"), 105794, 1, compare_same_vectors)); - //CHECK(compare_to_bin_file< double >(ir_data.directions, scene_dir, "directionIndex", 1, 105794, "double_00"), 105794, 1, compare_same_vectors)); // it passed, depends on index definition - CHECK(compare_to_bin_file< double >(ir_data.direction_per_pixel, scene_dir, "dirPerPixel", 2, 105794, "double_00"), 105794, 2, compare_same_vectors)); - CHECK(compare_to_bin_file< double >(ir_data.local_region[0], scene_dir, "localRegion", 2, 105794, "double_00"), 105794, 2, compare_same_vectors)); - CHECK(compare_to_bin_file< double >(ir_data.local_region[1], scene_dir, "localRegion", 2, 105794, "double_01"), 105794, 2, compare_same_vectors)); - CHECK(compare_to_bin_file< double >(ir_data.local_region[2], scene_dir, "localRegion", 2, 105794, "double_02"), 105794, 2, compare_same_vectors)); - CHECK(compare_to_bin_file< double >(ir_data.local_region[3], scene_dir, "localRegion", 2, 105794, "double_03"), 105794, 2, compare_same_vectors)); - - CHECK(compare_to_bin_file< double >(ir_data.local_region_x[0], scene_dir, "localRegion_x", 1, 105794, "double_00"), 105794, 1, compare_same_vectors)); - CHECK(compare_to_bin_file< double >(ir_data.local_region_x[1], scene_dir, "localRegion_x", 1, 105794, "double_01"), 105794, 1, compare_same_vectors)); - CHECK(compare_to_bin_file< double >(ir_data.local_region_x[2], scene_dir, "localRegion_x", 1, 105794, "double_02"), 105794, 1, compare_same_vectors)); - CHECK(compare_to_bin_file< double >(ir_data.local_region_x[3], scene_dir, "localRegion_x", 1, 105794, "double_03"), 105794, 1, compare_same_vectors)); - - CHECK(compare_to_bin_file< double >(ir_data.local_region_y[0], scene_dir, "localRegion_y", 1, 105794, "double_00"), 105794, 1, compare_same_vectors)); - CHECK(compare_to_bin_file< double >(ir_data.local_region_y[1], scene_dir, "localRegion_y", 1, 105794, "double_01"), 105794, 1, compare_same_vectors)); - CHECK(compare_to_bin_file< double >(ir_data.local_region_y[2], scene_dir, "localRegion_y", 1, 105794, "double_02"), 105794, 1, compare_same_vectors)); - CHECK(compare_to_bin_file< double >(ir_data.local_region_y[3], scene_dir, "localRegion_y", 1, 105794, "double_03"), 105794, 1, compare_same_vectors)); - - CHECK(compare_to_bin_file< double >(ir_data.local_edges, scene_dir, "localEdges", 4, 105794, "double_00"), 105794, 4, compare_same_vectors)); - CHECK(compare_to_bin_file< uint8_t >(ir_data.is_supressed, scene_dir, "isSupressed", 1, 105794, "uint8_00"), 105794, 1, compare_same_vectors)); -#endif - - // --- - TRACE( "\nChecking scene validity:" ); - - CHECK(!cal.is_scene_valid()); - - // edge distribution - CHECK( compare_to_bin_file< double >( z_data.sum_weights_per_section, scene_dir, "depthEdgeWeightDistributionPerSectionDepth", 1, 4,"double_00", compare_same_vectors ) ); - - CHECK( compare_to_bin_file< byte >( z_data.section_map, scene_dir, "sectionMapDepth_trans", 1, md.n_edges, "uint8_00", compare_same_vectors ) ); - CHECK( compare_to_bin_file< byte >( yuy_data.section_map, scene_dir, "sectionMapRgb_trans", 1, rgb_w*rgb_h, "uint8_00", compare_same_vectors ) ); - - CHECK( compare_to_bin_file< double >(yuy_data.sum_weights_per_section, scene_dir, "edgeWeightDistributionPerSectionRgb", 1, 4, "double_00", compare_same_vectors)); - - // gradient balanced - // TODO NOHA - //CHECK(compare_to_bin_file< double >(z_data.sum_weights_per_direction, scene_dir, "edgeWeightsPerDir", 1, 4, "double_00", compare_same_vectors)); - - // movment check - // 1. dilation - CHECK( compare_to_bin_file< uint8_t >( yuy_data.prev_logic_edges, scene_dir, "logicEdges", rgb_w, rgb_h, "uint8_00", compare_same_vectors ) ); - CHECK( compare_to_bin_file< double >( yuy_data.dilated_image, scene_dir, "dilatedIm", rgb_w, rgb_h, "double_00", compare_same_vectors ) ); - - // 2. gausssian - CHECK( compare_to_bin_file< double >( yuy_data.yuy_diff, scene_dir, "diffIm_01", rgb_w, rgb_h, "double_00", compare_same_vectors ) ); - CHECK( compare_to_bin_file< double >( yuy_data.gaussian_filtered_image, scene_dir, "diffIm", rgb_w, rgb_h, "double_00", compare_same_vectors ) ); - - // 3. movement - CHECK( compare_to_bin_file< double >( yuy_data.gaussian_diff_masked, scene_dir, "IDiffMasked", rgb_w, rgb_h, "double_00", compare_same_vectors ) ); - CHECK( compare_to_bin_file< uint8_t >( yuy_data.move_suspect, scene_dir, "ixMoveSuspect", rgb_w, rgb_h, "uint8_00", compare_same_vectors ) ); - - - //-- - TRACE( "\nOptimizing:" ); - auto cb = [&]( algo::iteration_data_collect const & data ) - { - // data.iteration is 0-based! - REQUIRE( data.iteration < md.n_iterations ); - - auto file = bin_file("calib_iteration", data.iteration + 1, num_of_calib_elements, 1, "double_00.bin"); - CHECK(compare_calib_to_bin_file(data.params.curr_calib, data.params.cost, scene_dir, file)); - - file = bin_file("uvmap_iteration", data.iteration + 1, 2, md.n_edges, "double_00.bin"); - CHECK(compare_to_bin_file< algo::double2 >(data.uvmap, scene_dir, file, md.n_edges, 1, compare_same_vectors)); - - file = bin_file("DVals_iteration", data.iteration + 1, 1, md.n_edges, "double_00.bin"); - CHECK(compare_to_bin_file< double >(data.d_vals, scene_dir, file, md.n_edges, 1, compare_same_vectors)); - - file = bin_file("DxVals_iteration", data.iteration + 1, 1, md.n_edges, "double_00.bin"); - CHECK(compare_to_bin_file< double >(data.d_vals_x, scene_dir, file, md.n_edges, 1, compare_same_vectors)); - - file = bin_file("DyVals_iteration", data.iteration + 1, 1, md.n_edges,"double_00.bin"); - CHECK(compare_to_bin_file< double >(data.d_vals_y, scene_dir, file, md.n_edges, 1, compare_same_vectors)); - - file = bin_file("xy_iteration", data.iteration + 1, 2, md.n_edges, "double_00.bin"); - CHECK(compare_to_bin_file< algo::double2 >(data.xy, scene_dir, file, md.n_edges, 1, compare_same_vectors)); - - file = bin_file("rc_iteration", data.iteration + 1, 1, md.n_edges, "double_00.bin"); - CHECK(compare_to_bin_file< double >(data.rc, scene_dir, file, md.n_edges, 1, compare_same_vectors)); - - file = bin_file("xCoeff_P", data.iteration + 1, sizeof(algo::p_matrix) / sizeof(double), md.n_edges, "double_00.bin"); - CHECK(compare_to_bin_file< algo::p_matrix>(data.coeffs_p.x_coeffs, scene_dir, file, md.n_edges, 1, compare_same_vectors)); - - /*file = bin_file("xCoeff_Krgb", data.iteration + 1, sizeof(algo::k_matrix) / sizeof(double), md.n_edges, "double_00.bin"); - CHECK(compare_to_bin_file< algo::k_matrix>(data.coeffs_k.x_coeffs, scene_dir, file, md.n_edges, 1, compare_same_vectors, sort_vectors)); - - file = bin_file("yCoeff_Krgb", data.iteration + 1, sizeof(algo::k_matrix) / sizeof(double), md.n_edges, "double_00.bin"); - CHECK(compare_to_bin_file< algo::k_matrix>(data.coeffs_k.y_coeffs, scene_dir, file, md.n_edges, 1, compare_same_vectors, sort_vectors)); - - file = bin_file("xCoeff_R", data.iteration + 1, sizeof(algo::rotation_in_angles) / sizeof(double), md.n_edges, "double_00.bin"); - CHECK(compare_to_bin_file< algo::rotation_in_angles>(data.coeffs_r.x_coeffs, scene_dir, file, md.n_edges, 1, compare_same_vectors, sort_vectors));*/ - - // TODO Avishag - //file = bin_file("grad_iteration", data.iteration + 1, num_of_calib_elements, 1, "double_00.bin"); - //CHECK(compare_calib_to_bin_file(data.params.calib_gradients, 0, scene_dir, file, true)); - }; - - // Our code doesn't count the first iteration; the Matlab code starts at 1 even if it doesn't do anything... - REQUIRE( cal.optimize( cb ) + 1 == md.n_iterations ); - - auto new_calibration = cal.get_calibration(); - auto cost = cal.get_cost(); - - CHECK(compare_calib_to_bin_file(new_calibration, cost, scene_dir, "new_calib", num_of_calib_elements, 1, "double_00")); - - - //-- - TRACE( "\nChecking output validity:" ); - // Pixel movement is OK, but some sections have negative cost - CHECK( ! cal.is_valid_results() ); - - CHECK( cal.calc_correction_in_pixels() == approx(md.correction_in_pixels)); - - CHECK( compare_to_bin_file< double >( z_data.cost_diff_per_section, scene_dir, "costDiffPerSection", 4, 1, "double_00", compare_same_vectors ) ); -} diff --git a/unit-tests/filesystem.h b/unit-tests/filesystem.h new file mode 100644 index 0000000000..2efb8046fb --- /dev/null +++ b/unit-tests/filesystem.h @@ -0,0 +1,290 @@ +// This file is part of OpenCV project. +// It is subject to the license terms in the LICENSE file found in the top-level directory +// of this distribution and at http://opencv.org/license.html. + +#pragma once + +#if defined _WIN32 || defined WINCE +# include +const char dir_separators[] = "/\\"; + +namespace +{ + struct dirent + { + const char* d_name; + }; + + struct DIR + { +#if defined(WINRT) || defined(_WIN32_WCE) + WIN32_FIND_DATAW data; +#else + WIN32_FIND_DATAA data; +#endif + HANDLE handle; + dirent ent; +#ifdef WINRT + DIR() { } + ~DIR() + { + if( ent.d_name ) + delete[] ent.d_name; + } +#endif + }; + + DIR* opendir( const char* path ) + { + DIR* dir = new DIR; + dir->ent.d_name = 0; +#if defined(WINRT) || defined(_WIN32_WCE) + cv::String full_path = cv::String( path ) + "\\*"; + wchar_t wfull_path[MAX_PATH]; + size_t copied = mbstowcs( wfull_path, full_path.c_str(), MAX_PATH ); + CV_Assert( (copied != MAX_PATH) && (copied != (size_t)-1) ); + dir->handle = ::FindFirstFileExW( wfull_path, FindExInfoStandard, + &dir->data, FindExSearchNameMatch, NULL, 0 ); +#else + dir->handle = ::FindFirstFileExA( (std::string( path ) + "\\*").c_str(), + FindExInfoStandard, &dir->data, FindExSearchNameMatch, NULL, 0 ); +#endif + if( dir->handle == INVALID_HANDLE_VALUE ) + { + /*closedir will do all cleanup*/ + delete dir; + return 0; + } + return dir; + } + + dirent* readdir( DIR* dir ) + { +#if defined(WINRT) || defined(_WIN32_WCE) + if( dir->ent.d_name != 0 ) + { + if( ::FindNextFileW( dir->handle, &dir->data ) != TRUE ) + return 0; + } + size_t asize = wcstombs( NULL, dir->data.cFileName, 0 ); + CV_Assert( (asize != 0) && (asize != (size_t)-1) ); + char* aname = new char[asize + 1]; + aname[asize] = 0; + wcstombs( aname, dir->data.cFileName, asize ); + dir->ent.d_name = aname; +#else + if( dir->ent.d_name != 0 ) + { + if( ::FindNextFileA( dir->handle, &dir->data ) != TRUE ) + return 0; + } + dir->ent.d_name = dir->data.cFileName; +#endif + return &dir->ent; + } + + void closedir( DIR* dir ) + { + ::FindClose( dir->handle ); + delete dir; + } + + +} +#else +# include +# include +const char dir_separators[] = "/"; +#endif + + +#ifdef _WIN32 +static const char native_separator = '\\'; +#else +static const char native_separator = '/'; +#endif + +static inline +bool isPathSeparator( char c ) +{ + return c == '/' || c == '\\'; +} + +std::string join( const std::string& base, const std::string& path ) +{ + if( base.empty() ) + return path; + if( path.empty() ) + return base; + + bool baseSep = isPathSeparator( base[base.size() - 1] ); + bool pathSep = isPathSeparator( path[0] ); + std::string result; + if( baseSep && pathSep ) + { + result = base + path.substr( 1 ); + } + else if( !baseSep && !pathSep ) + { + result = base + native_separator + path; + } + else + { + result = base + path; + } + return result; +} + +static bool wildcmp( const char *string, const char *wild ) +{ + // Based on wildcmp written by Jack Handy - jakkhandy@hotmail.com + const char *cp = 0, *mp = 0; + + while( (*string) && (*wild != '*') ) + { + if( (*wild != *string) && (*wild != '?') ) + { + return false; + } + + wild++; + string++; + } + + while( *string ) + { + if( *wild == '*' ) + { + if( !*++wild ) + { + return true; + } + + mp = wild; + cp = string + 1; + } + else if( (*wild == *string) || (*wild == '?') ) + { + wild++; + string++; + } + else + { + wild = mp; + string = cp++; + } + } + + while( *wild == '*' ) + { + wild++; + } + + return *wild == 0; +} + + +static bool isDir( const std::string& path, DIR* dir ) +{ +#if defined _WIN32 || defined WINCE + DWORD attributes; + BOOL status = TRUE; + if( dir ) + attributes = dir->data.dwFileAttributes; + else + { + WIN32_FILE_ATTRIBUTE_DATA all_attrs; +#ifdef WINRT + wchar_t wpath[MAX_PATH]; + size_t copied = mbstowcs( wpath, path.c_str(), MAX_PATH ); + CV_Assert( (copied != MAX_PATH) && (copied != (size_t)-1) ); + status = ::GetFileAttributesExW( wpath, GetFileExInfoStandard, &all_attrs ); +#else + status = ::GetFileAttributesExA( path.c_str(), GetFileExInfoStandard, &all_attrs ); +#endif + attributes = all_attrs.dwFileAttributes; + } + + return status && ((attributes & FILE_ATTRIBUTE_DIRECTORY) != 0); +#else + CV_UNUSED( dir ); + struct stat stat_buf; + if( 0 != stat( path.c_str(), &stat_buf ) ) + return false; + int is_dir = S_ISDIR( stat_buf.st_mode ); + return is_dir != 0; +#endif +} + +static void glob_rec( const std::string & directory, + const std::string & wildchart, + std::vector& result, + bool recursive, + bool includeDirectories, + const std::string & pathPrefix +) +{ + DIR *dir; + + if( (dir = opendir( directory.c_str() )) != 0 ) + { + /* find all the files and directories within directory */ + try + { + struct dirent *ent; + while( (ent = readdir( dir )) != 0 ) + { + const char* name = ent->d_name; + if( (name[0] == 0) || (name[0] == '.' && name[1] == 0) || (name[0] == '.' && name[1] == '.' && name[2] == 0) ) + continue; + + std::string path = join( directory, name ); + std::string entry = join( pathPrefix, name ); + + if( isDir( path, dir ) ) + { + if( recursive ) + glob_rec( path, wildchart, result, recursive, includeDirectories, entry ); + if( !includeDirectories ) + continue; + } + + if( wildchart.empty() || wildcmp( name, wildchart.c_str() ) ) + result.push_back( entry ); + } + } + catch( ... ) + { + closedir( dir ); + throw; + } + closedir( dir ); + } + else + { + throw std::runtime_error( to_string() << "could not open directory: " << directory.c_str() ); + } +} + +static void glob( + const std::string & directory, + const std::string & spec, + std::function< void( std::string const & ) > fn, + bool recursive = true, + bool includeDirectories = false +) +{ + std::vector< std::string > results; + glob_rec( directory, spec, results, recursive, includeDirectories, "" ); + for( auto r : results ) + fn( r ); +} + +static +std::string get_parent( std::string const & path ) +{ + auto x = path.find_last_of( dir_separators ); + if( x == std::string::npos ) + return std::string(); + return std::string( path, 0, x ); +}