aboutsummaryrefslogtreecommitdiff
path: root/svg-flatten/src/nopencv_test.cpp
diff options
context:
space:
mode:
authorjaseg <git@jaseg.de>2021-06-01 23:36:32 +0200
committerjaseg <git@jaseg.de>2021-06-01 23:36:32 +0200
commit3386e586ac0f0ae07535036efe6293f6118f7e2b (patch)
tree8758724de348c39cfd45054cff013abffc4fb27f /svg-flatten/src/nopencv_test.cpp
parentbbf1c02e799573532d1f5416fafe1b2255168bba (diff)
downloadgerbolyze-3386e586ac0f0ae07535036efe6293f6118f7e2b.tar.gz
gerbolyze-3386e586ac0f0ae07535036efe6293f6118f7e2b.tar.bz2
gerbolyze-3386e586ac0f0ae07535036efe6293f6118f7e2b.zip
Work on chain approx
Diffstat (limited to 'svg-flatten/src/nopencv_test.cpp')
-rw-r--r--svg-flatten/src/nopencv_test.cpp52
1 files changed, 46 insertions, 6 deletions
diff --git a/svg-flatten/src/nopencv_test.cpp b/svg-flatten/src/nopencv_test.cpp
index 21710b9..901c7a8 100644
--- a/svg-flatten/src/nopencv_test.cpp
+++ b/svg-flatten/src/nopencv_test.cpp
@@ -40,7 +40,7 @@ MU_TEST(test_complex_example_from_paper) {
};
Image32 test_img(9, 6, static_cast<int*>(img_data));
- const Polygon expected_polys[3] = {
+ const Polygon_i expected_polys[3] = {
{
{1,1}, {1,2}, {1,3}, {1,4}, {1,5},
{2,5}, {3,5}, {4,5}, {5,5}, {6,5}, {7,5}, {8,5},
@@ -64,18 +64,18 @@ MU_TEST(test_complex_example_from_paper) {
const ContourPolarity expected_polarities[3] = {CP_CONTOUR, CP_HOLE, CP_HOLE};
int invocation_count = 0;
- gerbolyze::nopencv::find_blobs(test_img, [&invocation_count, &expected_polarities, &expected_polys](Polygon poly, ContourPolarity pol) {
+ gerbolyze::nopencv::find_blobs(test_img, [&invocation_count, &expected_polarities, &expected_polys](Polygon_i &poly, ContourPolarity pol) {
invocation_count += 1;
mu_assert((invocation_count <= 3), "Too many contours returned");
mu_assert(poly.size() > 0, "Empty contour returned");
mu_assert_int_eq(pol, expected_polarities[invocation_count-1]);
- d2p last;
+ i2p last;
bool first = true;
- Polygon exp = expected_polys[invocation_count-1];
+ Polygon_i exp = expected_polys[invocation_count-1];
//cout << "poly: ";
- for (d2p &p : 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");
@@ -149,7 +149,7 @@ static void testdata_roundtrip(const char *fn) {
<< "xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">" << endl;
svg << "<rect width=\"100%\" height=\"100%\" fill=\"black\"/>" << endl;
- gerbolyze::nopencv::find_blobs(ref_img, [&svg](Polygon poly, ContourPolarity pol) {
+ 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");
@@ -212,7 +212,45 @@ MU_TEST(test_round_trip_two_px) { testdata_roundtrip("testdata/two-p
MU_TEST(test_round_trip_two_px_inv) { testdata_roundtrip("testdata/two-px-inv.png"); }
+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<y; cy++) {
+ for (int cx=0; cx<x; cx++) {
+ ref_img.at(cx, cy) = data[cy*x + cx] / 255;
+ }
+ }
+ stbi_image_free(data);
+
+ TempfileHack tmp_svg(".svg");
+
+ //ofstream svg(tmp_svg.c_str());
+ ofstream svg("/tmp/teh-chin-test.svg");
+
+ svg << "<svg width=\"" << x << "px\" height=\"" << y << "px\" viewBox=\"0 0 "
+ << x << " " << y << "\" "
+ << "xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">" << endl;
+ svg << "<rect width=\"100%\" height=\"100%\" fill=\"black\"/>" << endl;
+
+ gerbolyze::nopencv::find_blobs(ref_img, simplify_contours_teh_chin(2, [&svg](Polygon_i &poly, ContourPolarity pol) {
+ svg << "<path fill=\"" << ((pol == CP_HOLE) ? "black" : "white") << "\" d=\"";
+ svg << "M " << poly[0][0] << " " << poly[0][1];
+ for (size_t i=1; i<poly.size(); i++) {
+ svg << " L " << poly[i][0] << " " << poly[i][1];
+ }
+ svg << " Z\"/>" << endl;
+ }));
+ svg << "</svg>" << endl;
+ svg.close();
+}
+
+
+MU_TEST(chain_approx_test_chromosome) { chain_approx_test("testdata/chain-approx-teh-chin-chromosome.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);
@@ -229,6 +267,8 @@ 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);
};
int main(int argc, char **argv) {