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
This commit is contained in:
sz 2020-06-17 21:11:15 -05:00
parent e4b02d4a85
commit e0f36cae69
8 changed files with 104 additions and 49 deletions

View File

@ -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

View File

@ -30,5 +30,6 @@ target_link_libraries(extractor_test
extractor
${OPENCV_LIBS}
${CPPFILESYSTEM}
)

View File

@ -1,7 +1,7 @@
#include "unittest.h"
#include "Deskewer.h"
#include "image_hash/average_hash.h"
#include <iostream>
#include <string>
#include <vector>
@ -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) );
}

View File

@ -1,23 +1,45 @@
#include "unittest.h"
#include "Extractor.h"
#include "image_hash/average_hash.h"
#include "util/MakeTempDirectory.h"
#include <iostream>
#include <string>
#include <vector>
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) );
}

View File

@ -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<Anchor> 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<Anchor> 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<Anchor> 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<point<int>> 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()) );
}

View File

@ -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() );
}

View File

@ -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 <iostream>
#include <string>
#include <vector>
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<SimpleCameraCalibration> 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<SimpleCameraCalibration> 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) );
}

View File

@ -0,0 +1,29 @@
#pragma once
#include <cstdio>
#include <experimental/filesystem>
#include <string>
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;
};