From d18b8a1d80185a800e18db935f04dc63cd9e1637 Mon Sep 17 00:00:00 2001 From: jaseg Date: Wed, 2 Jun 2021 11:00:35 +0200 Subject: Finish chain approximation tests --- svg-flatten/src/nopencv.cpp | 94 +++++++++++-------- svg-flatten/src/nopencv_test.cpp | 194 ++++++++++++++++++++++++++------------- 2 files changed, 186 insertions(+), 102 deletions(-) diff --git a/svg-flatten/src/nopencv.cpp b/svg-flatten/src/nopencv.cpp index afe4881..2384fcc 100644 --- a/svg-flatten/src/nopencv.cpp +++ b/svg-flatten/src/nopencv.cpp @@ -247,6 +247,7 @@ double k_cos(const Polygon_i &poly, size_t i, size_t k) { return dp / (sqrt(sq_a)*sqrt(sq_b)); } +constexpr bool debug = false; ContourCallback gerbolyze::nopencv::simplify_contours_teh_chin(ContourCallback cb) { return [&cb](Polygon_i &poly, ContourPolarity cpol) { size_t sz = poly.size(); @@ -261,30 +262,32 @@ ContourCallback gerbolyze::nopencv::simplify_contours_teh_chin(ContourCallback c retain[i] = true; } - cerr << endl; - cerr << "Polarity: " << cpol <" << endl; + m_svg << "" << endl; + } + + ContourCallback callback() { + return [this](Polygon_i &poly, ContourPolarity pol) { + mu_assert(poly.size() > 0, "Empty contour returned"); + mu_assert(poly.size() > 2, "Contour has less than three points, no area"); + mu_assert(pol == CP_CONTOUR || pol == CP_HOLE, "Contour has invalid polarity"); + + m_svg << "" << endl; + }; + } + + void close() { + m_svg << "" << endl; + m_svg.close(); + } + +private: + ofstream m_svg; +}; + MU_TEST(test_complex_example_from_paper) { int32_t img_data[6*9] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -74,16 +108,13 @@ MU_TEST(test_complex_example_from_paper) { i2p last; bool first = true; Polygon_i exp = expected_polys[invocation_count-1]; - //cout << "poly: "; for (auto &p : poly) { - //cout << "(" << p[0] << ", " << p[1] << "), "; if (!first) { mu_assert((fabs(p[0] - last[0]) + fabs(p[1] - last[1]) == 1), "Subsequent contour points have distance other than one"); mu_assert(find(exp.begin(), exp.end(), p) != exp.end(), "Got unexpected contour point"); } last = p; } - //cout << endl; }); mu_assert_int_eq(3, invocation_count); @@ -127,6 +158,27 @@ MU_TEST(test_complex_example_from_paper) { } } +int render_svg(const char *in_svg, const char *out_png) { + const char *command_line[] = {"resvg", in_svg, out_png, nullptr}; + struct subprocess_s subprocess; + int rc = subprocess_create(command_line, subprocess_option_inherit_environment, &subprocess); + if (rc) + return rc; + + int resvg_rc = -1; + rc = subprocess_join(&subprocess, &resvg_rc); + if (rc) + return rc; + if (resvg_rc) + return -resvg_rc; + + rc = subprocess_destroy(&subprocess); + if (rc) + return rc; + + return 0; +} + static void testdata_roundtrip(const char *fn) { int x, y; uint8_t *data = stbi_load(fn, &x, &y, nullptr, 1); @@ -142,40 +194,11 @@ static void testdata_roundtrip(const char *fn) { TempfileHack tmp_svg(".svg"); TempfileHack tmp_png(".png"); - ofstream svg(tmp_svg.c_str()); - - svg << "" << endl; - svg << "" << endl; - - gerbolyze::nopencv::find_blobs(ref_img, [&svg](Polygon_i &poly, ContourPolarity pol) { - mu_assert(poly.size() > 0, "Empty contour returned"); - mu_assert(poly.size() > 2, "Contour has less than three points, no area"); - mu_assert(pol == CP_CONTOUR || pol == CP_HOLE, "Contour has invalid polarity"); + SVGPolyRenderer ctx(tmp_svg.c_str(), x, y); + gerbolyze::nopencv::find_blobs(ref_img, ctx.callback()); + ctx.close(); - svg << "" << endl; - }); - svg << "" << endl; - svg.close(); - - const char *command_line[] = {"resvg", tmp_svg.c_str(), tmp_png.c_str()}; - struct subprocess_s subprocess; - int rc = subprocess_create(command_line, subprocess_option_inherit_environment, &subprocess); - mu_assert_int_eq(0, rc); - - int resvg_rc = -1; - rc = subprocess_join(&subprocess, &resvg_rc); - mu_assert_int_eq(0, rc); - mu_assert_int_eq(0, resvg_rc); - - rc = subprocess_destroy(&subprocess); - mu_assert_int_eq(0, rc); + mu_assert_int_eq(0, render_svg(tmp_svg.c_str(), tmp_png.c_str())); int out_x, out_y; uint8_t *out_data = stbi_load(tmp_png.c_str(), &out_x, &out_y, nullptr, 1); @@ -213,44 +236,77 @@ MU_TEST(test_round_trip_two_px_inv) { testdata_roundtrip("testdata/two-p static void chain_approx_test(const char *fn) { - int x, y; - uint8_t *data = stbi_load(fn, &x, &y, nullptr, 1); - Image32 ref_img(x, y); - for (int cy=0; cy" << endl; - svg << "" << endl; - - gerbolyze::nopencv::find_blobs(ref_img, simplify_contours_teh_chin([&svg](Polygon_i &poly, ContourPolarity pol) { - svg << "" << endl; - })); - svg << "" << endl; - svg.close(); + } + stbi_image_free(data); + stbi_image_free(out_data); + + rms_sum = sqrt(rms_sum / (w*h)); + mean_sum /= w*h; + if (rms_sum > 0.5) { + snprintf(msg, sizeof(msg), "%s: Chain approximation RMS error is above threshold: %.3f > 0.5\n", fn, rms_sum); + mu_fail(msg); + } + if (mean_sum > 0.1) { + snprintf(msg, sizeof(msg), "%s: Chain approximation mean error is above threshold: %.3f > 0.1\n", fn, mean_sum); + mu_fail(msg); + } + //mu_assert(max_abs_deviation < 0.5, "Maximum chain approximation error is above threshold"); } MU_TEST(chain_approx_test_chromosome) { chain_approx_test("testdata/chain-approx-teh-chin-chromosome.png"); } +MU_TEST(chain_approx_test_blank) { chain_approx_test("testdata/blank.png"); } +MU_TEST(chain_approx_test_white) { chain_approx_test("testdata/white.png"); } +MU_TEST(chain_approx_test_blob_border_w) { chain_approx_test("testdata/blob-border-w.png"); } +MU_TEST(chain_approx_test_blobs_borders) { chain_approx_test("testdata/blobs-borders.png"); } +MU_TEST(chain_approx_test_blobs_corners) { chain_approx_test("testdata/blobs-corners.png"); } +MU_TEST(chain_approx_test_blobs_crossing) { chain_approx_test("testdata/blobs-crossing.png"); } +MU_TEST(chain_approx_test_cross) { chain_approx_test("testdata/cross.png"); } +MU_TEST(chain_approx_test_letter_e) { chain_approx_test("testdata/letter-e.png"); } +MU_TEST(chain_approx_test_paper_example) { chain_approx_test("testdata/paper-example.png"); } +MU_TEST(chain_approx_test_paper_example_inv) { chain_approx_test("testdata/paper-example-inv.png"); } +MU_TEST(chain_approx_test_single_px) { chain_approx_test("testdata/single-px.png"); } +MU_TEST(chain_approx_test_single_px_inv) { chain_approx_test("testdata/single-px-inv.png"); } +MU_TEST(chain_approx_test_two_blobs) { chain_approx_test("testdata/two-blobs.png"); } +MU_TEST(chain_approx_test_two_px) { chain_approx_test("testdata/two-px.png"); } +MU_TEST(chain_approx_test_two_px_inv) { chain_approx_test("testdata/two-px-inv.png"); } MU_TEST_SUITE(nopencv_contours_suite) { - /* MU_RUN_TEST(test_complex_example_from_paper); MU_RUN_TEST(test_round_trip_blank); MU_RUN_TEST(test_round_trip_white); @@ -267,8 +323,22 @@ MU_TEST_SUITE(nopencv_contours_suite) { MU_RUN_TEST(test_round_trip_two_blobs); MU_RUN_TEST(test_round_trip_two_px); MU_RUN_TEST(test_round_trip_two_px_inv); - */ MU_RUN_TEST(chain_approx_test_chromosome); + MU_RUN_TEST(chain_approx_test_blank); + MU_RUN_TEST(chain_approx_test_white); + MU_RUN_TEST(chain_approx_test_blob_border_w); + MU_RUN_TEST(chain_approx_test_blobs_borders); + MU_RUN_TEST(chain_approx_test_blobs_corners); + MU_RUN_TEST(chain_approx_test_blobs_crossing); + MU_RUN_TEST(chain_approx_test_cross); + MU_RUN_TEST(chain_approx_test_letter_e); + MU_RUN_TEST(chain_approx_test_paper_example); + MU_RUN_TEST(chain_approx_test_paper_example_inv); + MU_RUN_TEST(chain_approx_test_single_px); + MU_RUN_TEST(chain_approx_test_single_px_inv); + MU_RUN_TEST(chain_approx_test_two_blobs); + MU_RUN_TEST(chain_approx_test_two_px); + MU_RUN_TEST(chain_approx_test_two_px_inv); }; int main(int argc, char **argv) { -- cgit