From e0f36cae6957500663195b1453ee7a512bb42073 Mon Sep 17 00:00:00 2001 From: sz Date: Wed, 17 Jun 2020 21:11:15 -0500 Subject: [PATCH] Update/improve tests. Use new samples, check outputs (w/ imagehash) instead of logging to tmp + manual verification, add a MakeTempDirectory class to clean up stuff, etc --- CMakeLists.txt | 10 +++- src/lib/extractor/test/CMakeLists.txt | 1 + src/lib/extractor/test/DeskewerTest.cpp | 6 +-- src/lib/extractor/test/ExtractorTest.cpp | 34 +++++++++--- src/lib/extractor/test/ScannerTest.cpp | 53 +++++++++---------- .../test/SimpleCameraCalibrationTest.cpp | 8 +-- src/lib/extractor/test/UndistortTest.cpp | 12 ++--- src/lib/util/MakeTempDirectory.h | 29 ++++++++++ 8 files changed, 104 insertions(+), 49 deletions(-) create mode 100644 src/lib/util/MakeTempDirectory.h diff --git a/CMakeLists.txt b/CMakeLists.txt index e609757..1c57d8f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,17 +3,23 @@ cmake_minimum_required(VERSION 3.10) project ( libcimbar ) enable_testing() -set(CMAKE_CXX_FLAGS "-DLIBCIMBAR_PROJECT_ROOT=\\\"${libcimbar_SOURCE_DIR}\\\"") +set(CMAKE_CXX_FLAGS "-DLIBCIMBAR_PROJECT_ROOT=\\\"${libcimbar_SOURCE_DIR}\\\" -std=c++17") if("${CMAKE_BUILD_TYPE}" STREQUAL "") set(CMAKE_BUILD_TYPE "RelWithDebInfo") set(CMAKE_C_FLAGS "${CFLAGS} -Wall -std=c99 -Os") - set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-g -O2 -fpic -std=c++17") + set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-g -O2 -fpic") endif() if(NOT DEFINED OPENCV_LIBS) set(OPENCV_LIBS "opencv_core" "opencv_imgcodecs" "opencv_imgproc" "opencv_photo") endif() +if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") + if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 8.0) + set(CPPFILESYSTEM "stdc++fs") + endif() +endif() + set (PROJECTS src/exe/cimbar src/exe/cimbar_scan diff --git a/src/lib/extractor/test/CMakeLists.txt b/src/lib/extractor/test/CMakeLists.txt index 4033367..a54de7a 100644 --- a/src/lib/extractor/test/CMakeLists.txt +++ b/src/lib/extractor/test/CMakeLists.txt @@ -30,5 +30,6 @@ target_link_libraries(extractor_test extractor ${OPENCV_LIBS} + ${CPPFILESYSTEM} ) diff --git a/src/lib/extractor/test/DeskewerTest.cpp b/src/lib/extractor/test/DeskewerTest.cpp index 370b537..ecd9993 100644 --- a/src/lib/extractor/test/DeskewerTest.cpp +++ b/src/lib/extractor/test/DeskewerTest.cpp @@ -1,7 +1,7 @@ #include "unittest.h" #include "Deskewer.h" - +#include "image_hash/average_hash.h" #include #include #include @@ -14,8 +14,6 @@ TEST_CASE( "DeskewerTest/testSimple", "[unit]" ) cv::Mat actual = de.deskew(TestCimbar::getSample("4color1.jpg"), corners); assertEquals(cv::Size(1024, 1024), actual.size()); - // TODO: we'll want to do some verification that the transform worked correctly (perhaps with an imagehash??) - // but for now, this is fine - de.save(actual, "/tmp/foo.png"); + assertEquals( 0x434caa959f7e56c7, image_hash::average_hash(actual) ); } diff --git a/src/lib/extractor/test/ExtractorTest.cpp b/src/lib/extractor/test/ExtractorTest.cpp index 3685827..a857968 100644 --- a/src/lib/extractor/test/ExtractorTest.cpp +++ b/src/lib/extractor/test/ExtractorTest.cpp @@ -1,23 +1,45 @@ #include "unittest.h" #include "Extractor.h" +#include "image_hash/average_hash.h" +#include "util/MakeTempDirectory.h" #include #include #include TEST_CASE( "ExtractorTest/testExtract", "[unit]" ) { - Extractor ext; - ext.extract(TestCimbar::getSample("4color1.jpg"), "/tmp/testExtract.1.jpg"); + MakeTempDirectory tempdir; - // standard image similarity testing questions apply + std::string imgPath = tempdir.path() / "ex.jpg"; + Extractor ext; + ext.extract(TestCimbar::getSample("4color1.jpg"), imgPath); + + cv::Mat out = cv::imread(imgPath); + assertEquals( 0x434caab59f7e56c7, image_hash::average_hash(out) ); +} + +TEST_CASE( "ExtractorTest/testExtractMid", "[unit]" ) +{ + MakeTempDirectory tempdir; + + std::string imgPath = tempdir.path() / "ex.jpg"; + Extractor ext; + ext.extract(TestCimbar::getSample("4c-cam-40-f1.jpg"), imgPath); + + cv::Mat out = cv::imread(imgPath); + assertEquals( 0xe0d3b5b3f6b07075, image_hash::average_hash(out) ); } TEST_CASE( "ExtractorTest/testExtractUpscale", "[unit]" ) { - Extractor ext; - ext.extract(TestCimbar::getSample("4color-cam-140.jpg"), "/tmp/testExtract.2.jpg"); + MakeTempDirectory tempdir; - // standard image similarity testing questions apply + std::string imgPath = tempdir.path() / "exup.jpg"; + Extractor ext; + ext.extract(TestCimbar::getSample("4color-cam-140.jpg"), imgPath); + + cv::Mat out = cv::imread(imgPath); + assertEquals( 0x44c7f08cfd8f4c, image_hash::average_hash(out) ); } diff --git a/src/lib/extractor/test/ScannerTest.cpp b/src/lib/extractor/test/ScannerTest.cpp index 886b9c1..5ba5ce7 100644 --- a/src/lib/extractor/test/ScannerTest.cpp +++ b/src/lib/extractor/test/ScannerTest.cpp @@ -13,76 +13,75 @@ TEST_CASE( "ScannerTest/testPiecemealScan", "[unit]" ) { - cv::Mat img = cv::imread(TestCimbar::getSample("4color-ecc40-fountain.jpg")); + cv::Mat img = cv::imread(TestCimbar::getSample("4c-cam-40-f1.jpg")); Scanner sc(img); std::vector candidates = sc.t1_scan_rows(); std::string res = turbo::str::join(candidates); - assertStringContains("68+-22,187+-0", res); - assertStringContains("840+-23,153+-0", res); - assertStringContains("147+-18,884+-0", res); - assertStringContains("809+-19,884+-0", res); + assertStringContains("51+-26,289+-0", res); + assertStringContains("992+-27,289+-0", res); + assertStringContains("85+-24,1190+-0", res); + assertStringContains("959+-25,1190+-0", res); candidates = sc.t2_scan_columns(candidates); - assertStringContains("68+-22,191+-23", turbo::str::join(candidates)); - assertStringContains("840+-23,156+-25", turbo::str::join(candidates)); - assertStringContains("147+-18,890+-17", turbo::str::join(candidates)); - assertStringContains("809+-19,883+-17", turbo::str::join(candidates)); + assertStringContains("51+-27,286+-28", turbo::str::join(candidates)); + assertStringContains("993+-27,283+-28", turbo::str::join(candidates)); + assertStringContains("85+-24,1188+-24", turbo::str::join(candidates)); + assertStringContains("958+-25,1196+-24", turbo::str::join(candidates)); candidates = sc.t3_scan_diagonal(candidates); - assertStringContains("69+-23,192+-23", turbo::str::join(candidates)); - assertStringContains("840+-22,156+-22", turbo::str::join(candidates)); - assertStringContains("147+-17,890+-17", turbo::str::join(candidates)); - assertStringContains("810+-18,884+-18", turbo::str::join(candidates)); + assertStringContains("52+-28,287+-28", turbo::str::join(candidates)); + assertStringContains("993+-27,283+-27", turbo::str::join(candidates)); + assertStringContains("85+-24,1188+-24", turbo::str::join(candidates)); + assertStringContains("959+-24,1197+-24", turbo::str::join(candidates)); candidates = sc.t4_confirm_scan(candidates); sc.filter_candidates(candidates); // ordered by size assertEquals( - "840+-23,156+-25 69+-23,192+-24 148+-18,890+-17 809+-19,884+-18", + "52+-28,286+-28 992+-27,283+-28 85+-24,1188+-24 959+-25,1196+-24", turbo::str::join(candidates) ); } TEST_CASE( "ScannerTest/testExampleScan", "[unit]" ) { - cv::Mat img = cv::imread(TestCimbar::getSample("4color-ecc40-fountain.jpg")); + cv::Mat img = cv::imread(TestCimbar::getSample("4c-cam-40-f1.jpg")); Scanner sc(img); std::vector candidates = sc.scan(); // order is top-left, top-right, bottom-left, bottom-right assertEquals( - "69+-23,192+-24 " - "840+-23,156+-25 " - "148+-18,890+-17 " - "809+-19,884+-18", + "52+-28,286+-28 " + "992+-27,283+-28 " + "85+-24,1188+-24 " + "959+-25,1196+-24", turbo::str::join(candidates) ); } TEST_CASE( "ScannerTest/testScanEdges", "[unit]" ) { - cv::Mat img = cv::imread(TestCimbar::getSample("4color-ecc40-fountain.jpg")); + cv::Mat img = cv::imread(TestCimbar::getSample("4c-cam-40-f1.jpg")); Scanner sc(img); std::vector candidates = sc.scan(); // order is top-left, top-right, bottom-left, bottom-right assertEquals( - "69+-23,192+-24 " - "840+-23,156+-25 " - "148+-18,890+-17 " - "809+-19,884+-18", + "52+-28,286+-28 " + "992+-27,283+-28 " + "85+-24,1188+-24 " + "959+-25,1196+-24", turbo::str::join(candidates) ); Corners cs(candidates); Midpoints mp; std::vector> edges = sc.scan_edges(cs, mp); - // (444, 152), (846, 548), (472, 906), (91, 569) - assertEquals( "445,151 847,549 472,906 90,569", turbo::str::join(edges) ); + assertEquals( "518,255 1004,757 519,1219 41,754", turbo::str::join(edges) ); // check "expected" midpoints as well. - assertEquals( "446.662,174.366 823.277,548.729 472.746,887.052 111.503,567.53", turbo::str::join(mp.points()) ); + assertEquals( "518.995,284.51 974.896,756.212 519.402,1191.98 69.0967,753.309", turbo::str::join(mp.points()) ); } diff --git a/src/lib/extractor/test/SimpleCameraCalibrationTest.cpp b/src/lib/extractor/test/SimpleCameraCalibrationTest.cpp index 9e25ac5..d1c7a5a 100644 --- a/src/lib/extractor/test/SimpleCameraCalibrationTest.cpp +++ b/src/lib/extractor/test/SimpleCameraCalibrationTest.cpp @@ -11,7 +11,7 @@ TEST_CASE( "SimpleCameraCalibrationTest/testGetParams", "[unit]" ) { - cv::Mat img = cv::imread(TestCimbar::getSample("4color-ecc40-fountain.jpg")); + cv::Mat img = cv::imread(TestCimbar::getSample("4c-cam-40-f1.jpg")); SimpleCameraCalibration scc; @@ -23,8 +23,8 @@ TEST_CASE( "SimpleCameraCalibrationTest/testGetParams", "[unit]" ) std::stringstream dis; dis << dp.distortion; - assertEquals( "[240, 0, 480;\n" - " 0, 320, 640;\n" + assertEquals( "[270, 0, 540;\n" + " 0, 405, 810;\n" " 0, 0, 1]", cam.str() ); - assertEquals( "[-0.002561135546020082, 0, 0, 0]", dis.str() ); + assertEquals( "[-0.002913485439157776, 0, 0, 0]", dis.str() ); } diff --git a/src/lib/extractor/test/UndistortTest.cpp b/src/lib/extractor/test/UndistortTest.cpp index f648139..5b0a5a2 100644 --- a/src/lib/extractor/test/UndistortTest.cpp +++ b/src/lib/extractor/test/UndistortTest.cpp @@ -1,28 +1,28 @@ #include "unittest.h" -#include "Extractor.h" #include "Undistort.h" +#include "Extractor.h" #include "SimpleCameraCalibration.h" +#include "image_hash/average_hash.h" #include #include #include TEST_CASE( "UndistortTest/testUndistort", "[unit]" ) { - cv::Mat img = cv::imread(TestCimbar::getSample("4color-ecc40-fountain.jpg")); + cv::Mat img = cv::imread(TestCimbar::getSample("4c-cam-40-f1.jpg")); cv::Mat out; Undistort und; assertTrue( und.undistort(img, out) ); - cv::imwrite("/tmp/undistort.png", out); - // standard image similarity testing questions apply + assertEquals( 0xe377e7ec100, image_hash::average_hash(out) ); } TEST_CASE( "UndistortTest/testUndistortAndExtract", "[unit]" ) { - cv::Mat img = cv::imread(TestCimbar::getSample("4color-ecc40-fountain.jpg")); + cv::Mat img = cv::imread(TestCimbar::getSample("4c-cam-40-f1.jpg")); cv::Mat out; Undistort und; @@ -31,5 +31,5 @@ TEST_CASE( "UndistortTest/testUndistortAndExtract", "[unit]" ) Extractor ex; assertTrue( ex.extract(out, out) ); - cv::imwrite("/tmp/undistort+extract.png", out); + assertEquals( 0x40589db2779fb200, image_hash::average_hash(out) ); } diff --git a/src/lib/util/MakeTempDirectory.h b/src/lib/util/MakeTempDirectory.h new file mode 100644 index 0000000..49c86fe --- /dev/null +++ b/src/lib/util/MakeTempDirectory.h @@ -0,0 +1,29 @@ +#pragma once + +#include +#include +#include + +class MakeTempDirectory +{ +public: + MakeTempDirectory() + { + _path = std::tmpnam(nullptr); + std::experimental::filesystem::create_directory(_path); + } + + ~MakeTempDirectory() + { + std::error_code ec; + std::experimental::filesystem::remove_all(_path, ec); + } + + std::experimental::filesystem::path path() const + { + return _path; + } + +protected: + std::experimental::filesystem::path _path; +};