File: test_2dmaxflow.cc

package info (click to toggle)
mia 2.4.7-17
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,420 kB
  • sloc: cpp: 162,689; python: 1,322; sh: 321; xml: 127; makefile: 42; csh: 24; ansic: 9
file content (101 lines) | stat: -rw-r--r-- 4,045 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* -*- mia-c++  -*-
 *
 * This file is part of MIA - a toolbox for medical image analysis
 * Copyright (c) Leipzig, Madrid 1999-2017 Gert Wollny
 *
 * MIA is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MIA; if not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <mia/internal/plugintester.hh>
#include <mia/2d/imageio.hh>

#include <addons/maxflow/2dmaxflow.hh>

using namespace std;
using namespace mia;
using maxflow_2dimage_filter::C2DMaxflowFilterPluginFactory;

BOOST_AUTO_TEST_CASE ( test_2d_simple_maxflow )
{
// one source, one sink and a pretty simple 4x4 image
       // sink and source
       vector<unsigned char> image = {
              255, 255, 245, 233,
              200, 170, 170, 150,
              20,   30,  40,  30,
              40,   20,  50,  10
       };
       vector<bool> expect = {
              false, false, false, false,
              false, false, false, false,
              true, true, true, true,
              true, true, true, true
       };
       C2DBounds sink(0, 0);
       C2DBounds source(3, 3);
       C2DBounds size(4, 4);
       C2DUBImage in_image(size, image);
       // define a flow image
       // max flow = 1
       // min flow = 0
       C2DFImage *fimage = new C2DFImage(size);
       (*fimage)(sink) = 16.0f;
       P2DImage sink_image(fimage);
       fimage = new C2DFImage(size);
       (*fimage)(source) = 16.0f;
       P2DImage source_image(fimage);
       auto maxflow = BOOST_TEST_create_from_plugin<C2DMaxflowFilterPluginFactory>("maxflow:source-flow=src.@,sink-flow=sink.@");
       save_image("src.@", source_image);
       save_image("sink.@", sink_image);
       auto result = maxflow->filter( in_image );
       BOOST_REQUIRE(result);
       BOOST_CHECK_EQUAL ( result->get_size(), size);
       BOOST_CHECK_EQUAL ( result->get_pixel_type(), it_bit);
       auto& bit_result = dynamic_cast<const C2DBitImage&>(*result);
       auto iexp = expect.begin();

       for (auto ires = bit_result.begin(); ires != bit_result.end(); ++ires, ++iexp) {
              BOOST_CHECK_EQUAL(*ires, *iexp);
       }
}


BOOST_AUTO_TEST_CASE( test_throw_missing_or_wrong_parameter )
{
       C2DUBImage in_image(C2DBounds(4, 4));
       save_image("test.@", in_image);
       BOOST_CHECK_THROW(BOOST_TEST_create_from_plugin<C2DMaxflowFilterPluginFactory>("maxflow"),
                         invalid_argument);
       BOOST_CHECK_THROW(BOOST_TEST_create_from_plugin<C2DMaxflowFilterPluginFactory>("maxflow:sink-flow=test.@"),
                         invalid_argument);
       BOOST_CHECK_THROW(BOOST_TEST_create_from_plugin<C2DMaxflowFilterPluginFactory>("maxflow:source-flow=test.@"),
                         invalid_argument);
       auto maxflow = BOOST_TEST_create_from_plugin<C2DMaxflowFilterPluginFactory>("maxflow:source-flow=test.@,sink-flow=test.@");
       // should throw because we don't accept non-float flow images
       BOOST_CHECK_THROW(maxflow->filter(in_image), invalid_argument);
}

BOOST_AUTO_TEST_CASE( test_throw_wrong_sizes )
{
       C2DFImage flow_image(C2DBounds(4, 4));
       save_image("test.@", flow_image);
       C2DFImage wrong_image_size(C2DBounds(4, 5));
       wrong_image_size(0, 0) = 1.0;
       auto maxflow = BOOST_TEST_create_from_plugin<C2DMaxflowFilterPluginFactory>("maxflow:source-flow=test.@,sink-flow=test.@");
       BOOST_CHECK_THROW(maxflow->filter(wrong_image_size), invalid_argument);
       C2DFImage flat_image(C2DBounds(4, 4));
       BOOST_CHECK_THROW(maxflow->filter(flat_image), invalid_argument);
}