$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
iz.cc
1 // Copyright (C) 2012, 2013 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 
29 
30 #include <mln/core/image/image2d.hh>
31 #include <mln/core/alias/neighb2d.hh>
32 
33 #include <mln/data/fill.hh>
34 #include <mln/data/paste.hh>
35 #include <mln/data/convert.hh>
36 #include <mln/data/saturate.hh>
37 
38 #include <mln/labeling/value.hh>
39 #include <mln/labeling/colorize.hh>
40 
41 #include <mln/morpho/watershed/superpose.hh>
42 #include <mln/morpho/watershed/flooding.hh>
43 #include <mln/morpho/elementary/dilation.hh>
44 #include <mln/morpho/closing/area.hh>
45 #include <mln/morpho/closing/structural.hh>
46 
47 #include <mln/transform/distance_front.hh>
48 #include <mln/transform/influence_zone_front.hh>
49 
50 #include <mln/pw/all.hh>
51 #include <mln/core/image/dmorph/image_if.hh>
52 #include <mln/core/image/dmorph/sub_image.hh>
53 
54 #include <mln/make/w_window2d.hh>
55 
56 #include "apps/morphers/lazy_recorder.hh"
57 
58 #include <mln/io/pbm/load.hh>
59 #include <mln/io/pbm/save.hh>
60 #include <mln/io/pgm/save.hh>
61 #include <mln/io/ppm/save.hh>
62 #include <mln/io/magick/save.hh>
63 
64 #include <mln/literal/colors.hh>
65 
66 
67 // Save as PNG directly, instead of using PPM as intermediate format.
68 template <typename I>
69 inline
70 void
71 save_colorized(const mln::decorated_image< I, lazy_recorder<I> >& rec,
72  const std::string& prefix)
73 {
74  mln_concrete(I) frame = mln::duplicate(rec.decoration().initial);
75  std::stringstream s;
76  s << std::setfill ('0') << std::setw (6) << 0;
77  mln::io::magick::save(mln::labeling::colorize(mln::value::rgb8(), frame),
78  prefix + s.str() + ".png");
79  for (size_t i = 0; i < rec.decoration().sequence.size(); ++i)
80  {
81  // Next change (`frame(p)' is assigned the value `v' in the next
82  // frame).
83  mln_psite(I) p = rec.decoration().sequence[i].first;
84  mln_value(I) v = rec.decoration().sequence[i].second;
85  // Skip consecutive identical frames.
86  if (frame(p) == v)
87  continue;
88  frame(p) = v;
89  std::stringstream s;
90  s << std::setfill ('0') << std::setw (6) << i + 1;
91  mln::io::magick::save(mln::labeling::colorize(mln::value::rgb8(), frame),
92  prefix + s.str() + ".png");
93  }
94 }
95 
96 
97 void usage(char* argv[])
98 {
99  std::cerr << "usage: " << argv[0] << " input.pbm output.ppm" << std::endl;
100  std::abort();
101 }
102 
103 
104 int main(int argc, char* argv[])
105 {
106  // Initialize Magick++.
107  Magick::InitializeMagick(*argv);
108 
109  using namespace mln;
110  using value::int_u8;
111 
112  if (argc != 3)
113  usage(argv);
114 
115  image2d<bool> input, clo;
116  io::pbm::load(input, argv[1]);
117 
118 
120  clo = morpho::closing::structural(input,
121  win::rectangle2d(5, 17));
122 
123  io::pbm::save(clo, "tmp_clo.pgm");
124 
125 
127  int ww[] = { 00, 11, 0, 11, 0,
128  11, 7, 5, 7, 11,
129  00, 5, 0, 5, 0,
130  11, 7, 5, 7, 11,
131  00, 11, 0, 11, 0 };
132 
133 
134  image2d<int_u8> dmap;
135  dmap = transform::distance_front(clo,
136  c4(), make::w_window2d(ww),
137  mln_max(int_u8));
138  dmap = morpho::closing::area(dmap, c4(), 500);
139 
140  io::pgm::save(dmap, "tmp_dmap.pgm");
141 
142 
143  unsigned nbasins;
145  c4(),
146  nbasins);
147 
148  io::ppm::save(labeling::colorize(value::rgb8(), ws), "tmp_ws.ppm");
149 
150  {
152  data::fill((ws_ | (pw::value(ws) == pw::cst(0u))).rw(), literal::red);
153  io::ppm::save(ws_, "tmp_ws_superpose.ppm");
154 
155  // test% g++ -I. main.cc -DNDEBUG -O2
156  // main.cc: In function `int main(int, char**)':
157  // main.cc:85: error: no matching function for call to `convert(mln::image2d<bool>&, mln::value::rgb8)'
158 
159  // /// Convert the image \p input by changing the value type.
160  // ///
161  // /// \param[in] v A value of the destination type.
162  // /// \param[in] input The input image.
163  // //
164  // template <typename V, typename I>
165  // mln_ch_value(I, V)
166  // convert(const V& v, const Image<I>& input);
167  }
168 
169 
170  image2d<unsigned> lab(input.domain());
171  data::fill(lab, 0);
172 
173  unsigned nlabels;
174  data::paste(labeling::value(ws | make::box2d(0,0,input.nrows()-1,0),
175  0, c4(), nlabels),
176  lab);
177 
180  lazy_record(lab);
181 
182  io::pgm::save(data::saturate(value::int_u8(), lab), "tmp_lab.pgm");
183 
184 
188  pw::cst_<unsigned int> > > rec_iz =
189  transform::influence_zone_front(rec_lab | (pw::value(ws) == pw::cst(0u)),
190  c8(),
191  make::w_window2d(ww));
192  data::paste(rec_iz, lab);
193  io::pgm::save(data::saturate(value::int_u8(), lab), "tmp_iz.pgm");
194 
195  save_colorized(rec_iz.unmorph_(), "tmp_iz");
196 
198  io::ppm::save(output, "tmp_iz.ppm");
199 
200  data::fill((output | pw::value(input)).rw(), literal::white);
201  io::ppm::save(output, "tmp_iz_input.ppm");
202 }