File: 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 (189 lines) | stat: -rw-r--r-- 6,205 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* -*- 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/core/filter.hh>
#include <mia/2d/imageio.hh>
#include <addons/maxflow/2dmaxflow.hh>
#include <maxflow.h>

#include <mia/core/msgstream.hh>

NS_BEGIN( maxflow_2dimage_filter)

using namespace mia;
using std::string;
using std::invalid_argument;
using std::minmax_element;
using maxflow::Graph_FFF;


C2DMaxflow::C2DMaxflow(const std::string& sink_flow_imagefile, const std::string& source_flow_imagefile):
       m_sink_flow_imagefile(sink_flow_imagefile),
       m_source_flow_imagefile(source_flow_imagefile)
{
}


C2DFImage load_flow_image(const string& imagefile, const string& type, const C2DBounds& expect_size)
{
       auto pimage = load_image2d(imagefile);

       if (pimage->get_size() != expect_size) {
              throw create_exception<invalid_argument>("Maxflow: ", type, " flow image '", imagefile,
                            "' has size (", pimage->get_size(),
                            "), but expect size (", expect_size, ")");
       }

       if (pimage->get_pixel_type() != it_float)
              throw create_exception<invalid_argument>("Maxflow: ", type, " flow image '", imagefile,
                            "' is not of type 'float'");

       return C2DFImage(static_cast<const C2DFImage&>(*pimage));
}


// this needs to become tunable

class FGradToFlow
{
public:
       FGradToFlow(float vmin, float vmax)
       {
              if (vmax <= vmin) {
                     throw create_exception<invalid_argument>("Maxflow: input image seems to be of one value only");
              }

              m_scale = 1.0 / (vmax - vmin);
       }

       float operator() (float x, float y) const
       {
              float delta = (x - y) * m_scale;
              float v = 1 - delta * delta / 0.9;
              return v * v;
       }
private:
       float m_scale;
};



template <class T>
typename C2DMaxflow::result_type C2DMaxflow::operator () (const mia::T2DImage<T>& data) const
{
       // load the sink and source flow images
       // throws if file not available
       auto sink = load_flow_image(m_sink_flow_imagefile, "sink", data.get_size());
       auto source = load_flow_image(m_source_flow_imagefile, "source", data.get_size() );
       // create the maxflow object
       Graph_FFF graph(data.size(), 2 * data.get_size().size());
       graph.add_node(data.size());
// add the capacities towards sinks ans sources
       int idx = 0;
       auto isink = sink.begin();
       auto esink = sink.end();
       auto isource = source.begin();

       while (isink != esink) {
              if (*isink > 0 || *isource > 0) {
                     graph.add_tweights(idx, *isource, *isink);
              }

              ++isink;
              ++isource;
              ++idx;
       }

       // this should become a configurable function
       auto minmax_vals  = minmax_element(data.begin(), data.end());
       FGradToFlow grad_to_flow(*minmax_vals.first, *minmax_vals.second);
       // add the inter-pixel capacities
       idx = 0;

       for (unsigned y = 0; y < data.get_size().y; ++y) {
              for (unsigned x = 0; x < data.get_size().x - 1; ++x, ++idx) {
                     auto xflow = grad_to_flow(data(x, y), data(x + 1, y));
                     graph.add_edge(idx, idx + 1, xflow, xflow);
              }

              ++idx;
       }

       idx = 0;

       for (unsigned y = 0; y < data.get_size().y - 1; ++y) {
              for (unsigned x = 0; x < data.get_size().x; ++x, ++idx) {
                     auto yflow = grad_to_flow(data(x, y), data(x, y + 1));
                     graph.add_edge(idx, idx + data.get_size().x, yflow, yflow);
              }
       }

       float flow = graph.maxflow();
       cvinfo() << "get flow " << flow << "\n";
       C2DBitImage *result = new C2DBitImage(data.get_size(), data);
       idx = 0;

       for (auto ir = result->begin(); ir != result->end(); ++ir, ++idx) {
              *ir = (graph.what_segment(idx) == Graph_FFF::SOURCE);
       }

       return P2DImage(result);
}

mia::P2DImage C2DMaxflow::do_filter(const mia::C2DImage& image) const
{
       return mia::filter(*this, image);
}


C2DMaxflowFilterPluginFactory::C2DMaxflowFilterPluginFactory():
       C2DFilterPlugin("maxflow")
{
       add_parameter("sink-flow",
                     new CStringParameter(m_sink_flow_imagefile,
                                          CCmdOptionFlags::required_input,
                                          "Image of float type to define the per-pixel flow to the sink",
                                          &C2DImageIOPluginHandler::instance()));
       add_parameter("source-flow",
                     new CStringParameter(m_source_flow_imagefile,
                                          CCmdOptionFlags::required_input,
                                          "Image of float type to define the per-pixel flow to the source",
                                          &C2DImageIOPluginHandler::instance()));
}


mia::C2DFilter *C2DMaxflowFilterPluginFactory::do_create()const
{
       return new C2DMaxflow(m_sink_flow_imagefile, m_source_flow_imagefile);
}

const std::string C2DMaxflowFilterPluginFactory::do_get_descr()const
{
       return "This filter implements the uses the max-flow min-cut algorithm"
              "for image segmentation";
}

extern "C" EXPORT CPluginBase *get_plugin_interface()
{
       return new C2DMaxflowFilterPluginFactory();
}


NS_END