$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
group_from_rag.cc
1 // Copyright (C) 2009, 2014 EPITA Research and Development Laboratory (LRDE).
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #include <iostream>
27 
28 #include <mln/core/image/image2d.hh>
29 #include <mln/core/image/edge_image.hh>
30 #include <mln/core/image/vertex_image.hh>
31 #include <mln/core/image/dmorph/image_if.hh>
32 #include <mln/value/label_16.hh>
33 #include <mln/io/ppm/save.hh>
34 #include <mln/io/pbm/load.hh>
35 #include <mln/core/alias/neighb2d.hh>
36 #include <mln/literal/colors.hh>
37 #include <mln/labeling/colorize.hh>
38 #include <mln/labeling/compute.hh>
39 #include <mln/make/p_vertices_with_mass_centers.hh>
40 #include <mln/make/edge_image.hh>
41 #include <mln/make/vertex_image.hh>
42 #include <mln/debug/draw_graph.hh>
43 #include <mln/util/graph.hh>
44 #include <mln/accu/center.hh>
45 
46 #include <scribo/primitive/extract/components.hh>
47 #include <scribo/primitive/link/with_rag.hh>
48 //#include <scribo/primitive/group/from_rag.hh>
49 
50 #include <scribo/filter/objects_small.hh>
51 #include <scribo/filter/objects_thin.hh>
52 #include <scribo/filter/objects_thick.hh>
53 
54 
55 #include <scribo/fun/v2b/objects_small_filter.hh>
56 #include <scribo/debug/bboxes_image.hh>
57 #include <scribo/debug/linked_bboxes_image.hh>
58 #include <scribo/make/debug_filename.hh>
59 
60 int usage(const char *name)
61 {
62  std::cout << "Usage: " << name << " <input.pbm> " << std::endl;
63  return 1;
64 }
65 
66 
67 
68 namespace scribo
69 {
70 
71  using namespace mln;
72 
73 
74  namespace graph
75  {
76 
77  template <typename A, typename G, typename I>
78  inline
80  compute_vertex(const Accumulator<A>& accu,
81  const Graph<G>& g_,
82  const Image<I>& lbl_,
83  const mln_value(I)& nlabels)
84  {
85  const G& g = exact(g_);
86  const I& lbl = exact(lbl_);
87 
88  mln_precondition(g.is_valid());
89  mln_precondition(lbl.is_valid());
90 
91  util::array<mln_result(A)>
92  values = labeling::compute(accu, lbl, nlabels);
93 
95  v_ima = mln::make::vertex_image(g, values);
96 
97  return v_ima;
98  }
99 
100  } // end of namespace scribo::graph
101 
102 
103  template <typename I>
104  struct edge_color : mln::Function_v2b< edge_color<I> >
105  {
106  typedef value::rgb8 result;
107 
108  edge_color(const Image<I>& mask) : mask_(exact(mask)) {}
109 
110  value::rgb8 operator()(const unsigned id) const
111  {
112  if (mask_(id))
113  return literal::green;
114  return literal::red;
115  }
116 
117  I mask_;
118  };
119 
120 
122  struct dist : Function_vv2v< dist >
123  {
124 
125  typedef bool result;
126 
127  bool operator()(const point2d& p1, const point2d& p2) const
128  {
129  return (math::sqrt(math::sqr(static_cast<float>(p1.row() - p2.row()))
130  + math::sqr(static_cast<float>(p1.col() - p2.col())))) < 5;
131  }
132 
133  };
134 
135 
136  namespace filter
137  {
138 
139  template <typename P, typename V, typename G, typename F, typename FP>
141  graph_edges(const vertex_image<P,V,G>& v_ima,
142  const Function<F>& /* edge_values */,
143  const Function<FP>& /* predicate */)
144  {
145  typedef edge_image<void,bool,util::graph> e_filter_t;
146  e_filter_t e_filter = mln::make::edge_image(v_ima, dist());
147 
148 
149  }
150 
151  } // end of namespace scribo::filter
152 
153 
154 }
155 
156 
157 
158 int main(int argc, char* argv[])
159 {
160  using namespace scribo;
161  using namespace mln;
162 
163  if (argc != 2)
164  return usage(argv[0]);
165 
166  scribo::make::internal::debug_filename_prefix = "group_with_rag";
167 
168  image2d<bool> input;
169  io::pbm::load(input, argv[1]);
170 
171 
172  typedef image2d<value::label_16> L;
173  value::label_16 nbboxes;
174  typedef component_set<L> components_t;
175  components_t components = primitive::extract::components(input, c8(), nbboxes);
176 
178  components_t filtered_components
180 
181  filtered_components
182  = scribo::filter::components_thin(filtered_components, 3);
183 
184  filtered_components
185  = scribo::filter::objects_thick(filtered_components,
186  math::min(input.ncols(), input.nrows()) / 6);
187 
188 
190  typedef util::couple< util::graph, image2d<value::label_16> > rag_data_t;
191  rag_data_t rag_data = primitive::link::with_rag(filtered_components, c8());
192 
193 
194  typedef vertex_image<void, algebra::vec<2u, float>, util::graph> v_ima_t;
195  v_ima_t v_ima =
196  scribo::graph::compute_vertex(accu::center<point2d>(),
197  rag_data.first(),
198  filtered_components.labeled_image(),
199  filtered_components.nelements());
200 
201  //FOR DEBUGGING PURPOSE
202  {
204  before_grouping = data::convert(value::rgb8(), input);
205 
206  // FIXME: Disabled, no longer compiles.
207 #if 0
208  scribo::draw::bounding_boxes(before_grouping,
209  filtered_components.bboxes(),
210  literal::blue);
211 #endif
212 
214  pv_t pv =
215  mln::make::p_vertices_with_mass_centers(filtered_components.labeled_image(),
216  rag_data.first());
217  mln::debug::draw_graph(before_grouping, pv, literal::green, literal::green);
218 
219  io::ppm::save(before_grouping,
220  scribo::make::debug_filename("before_grouping.ppm"));
221  }
222 
223 
224 
225  typedef edge_image<void,bool,util::graph> e_filter_t;
226  e_filter_t e_filter = mln::make::edge_image(v_ima, dist());
227 
228 
229 
230 
231  //FOR DEBUGGING PURPOSE
232 // {
233 // image2d<value::rgb8>
234 // after_grouping = data::convert(value::rgb8(), input);
235 
236 
237 // scribo::draw::bounding_boxes(after_grouping,
238 // filtered_components.bboxes(),
239 // literal::blue);
240 
241 // mln::debug::draw_graph(after_grouping, v_ima.domain(),
242 // pw::cst(literal::black), edge_color<e_filter_t>(e_filter));
243 
244 // io::ppm::save(after_grouping, "after_grouping.ppm");
245 // }
246 }
247 
248