Olena-patches
Threads by month
- ----- 2025 -----
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2010 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2009 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2008 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2007 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2006 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2005 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2004 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
November 2008
- 14 participants
- 266 discussions
cleanup-2008 2772: Inim: Add the code related to edge detection in the sandbox.
by Nicolas Ballas 03 Nov '08
by Nicolas Ballas 03 Nov '08
03 Nov '08
https://svn.lrde.epita.fr/svn/oln/branches/cleanup-2008/milena/sandbox
Index: ChangeLog
from Nicolas Ballas <ballas(a)lrde.epita.fr>
Inim: Add the code related to edge detection in the sandbox.
* ballas/color: New.
* ballas/color/min_tree_volume_filter.cc: Min tree using volume filter.
* ballas/color/reference.cc: Method using a gradient.
* ballas/color/min_tree_area_filter.cc: Min tree using area filter.
* ballas/color/min_tree_color.cc: Min tree using filter based on color.
* ballas/color/reference2.cc: Method using the laplacien.
* ballas/color/src: New.
* ballas/color/src/graph.hh,
* ballas/color/src/io.hh,
* ballas/color/src/distance.hh,
* ballas/color/src/convert.hh: New, factorize some code.
* ballas/color/laplacien.cc: Laplacien tests.
laplacien.cc | 129 ++++++++++
min_tree_area_filter.cc | 505 +++++++++++++++++++++++++++++++++++++++++
min_tree_color.cc | 525 +++++++++++++++++++++++++++++++++++++++++++
min_tree_volume_filter.cc | 514 ++++++++++++++++++++++++++++++++++++++++++
reference.cc | 556 ++++++++++++++++++++++++++++++++++++++++++++++
reference2.cc | 406 +++++++++++++++++++++++++++++++++
src/convert.hh | 37 +++
src/distance.hh | 52 ++++
src/graph.hh | 61 +++++
src/io.hh | 57 ++++
10 files changed, 2842 insertions(+)
Index: ballas/color/min_tree_volume_filter.cc
--- ballas/color/min_tree_volume_filter.cc (revision 0)
+++ ballas/color/min_tree_volume_filter.cc (revision 0)
@@ -0,0 +1,514 @@
+# include <mln/core/var.hh>
+
+# include <mln/core/image/image2d.hh>
+# include <mln/core/image/image_if.hh>
+# include <mln/core/image/extended.hh>
+# include <mln/core/routine/extend.hh>
+
+# include <mln/core/alias/window2d.hh>
+# include <mln/core/alias/neighb2d.hh>
+# include <mln/make/double_neighb2d.hh>
+# include <mln/core/site_set/p_centered.hh>
+
+# include <mln/literal/origin.hh>
+# include <mln/literal/black.hh>
+# include <mln/literal/white.hh>
+
+# include <mln/value/int_u8.hh>
+# include <mln/value/int_u16.hh>
+# include <mln/io/pgm/load.hh>
+# include <mln/io/pgm/save.hh>
+
+# include <mln/value/rgb8.hh>
+# include <mln/io/ppm/load.hh>
+# include <mln/io/ppm/save.hh>
+
+# include <mln/morpho/closing_area.hh>
+
+# include <mln/level/paste.hh>
+# include <mln/level/fill.hh>
+# include <mln/level/transform.hh>
+# include <mln/extension/fill.hh>
+
+# include <mln/debug/println.hh>
+
+# include "src/distance.hh"
+
+namespace mln
+{
+ template <typename I, typename N, typename Ic, typename Nc>
+ struct min_tree_
+ {
+ typedef mln_site(I) point;
+ typedef p_array<point> S;
+
+ // in:
+ const I& f;
+ const N& nbh;
+ const Ic& ref;
+ const Nc& nbhc;
+
+ // aux:
+ S s;
+ mln_ch_value(I, bool) deja_vu;
+ mln_ch_value(I, point) parent;
+ mln_ch_value(I, bool) resp;
+ mln_ch_value(I, point) zpar;
+
+ // attached data:
+ int lambda;
+ mln_ch_value(I, int) volume;
+ //mln_ch_value(Ic, value::rgb8) values;
+ //initialize(values, ref);
+ //mln_ch_value(I, int) comp;
+
+ min_tree_(const I& f, const N& nbh, const Ic& ref, const Nc& nbhc,
+ int lambda)
+ : f(f),
+ nbh(nbh),
+ ref(ref),
+ nbhc(nbhc),
+ lambda(lambda)
+ {
+ run();
+ }
+
+ void run()
+ {
+ // init
+ {
+ initialize(deja_vu, f);
+ initialize(parent, f);
+ initialize(resp, f);
+ initialize(zpar, f);
+ initialize(volume, f);
+ //initialize(comp, f);
+
+ mln::level::fill(deja_vu, false);
+ //mln::level::fill(resp, false);
+ mln::level::fill(volume, 0);
+
+ s = level::sort_psites_increasing(f);
+ }
+
+ // first pass
+ {
+ mln_fwd_piter(S) p(s);
+ mln_niter(N) n(nbh, p);
+ for_all(p)
+ {
+ make_set(p);
+ for_all(n)
+ if (f.has(n) && deja_vu(n))
+ do_union(n, p);
+ deja_vu(p) = true;
+ }
+ }
+
+ // second pass: canonization
+ {
+ mln_bkd_piter(S) p(s);
+ for_all(p)
+ {
+ point q = parent(p);
+ if (f(parent(q)) == f(q))
+ {
+ parent(p) = parent(q);
+ resp(q) = false;
+ }
+ }
+ }
+
+ // third pass: Merging region with volume < lambda
+ {
+ mln_fwd_piter(S) p(s);
+ for_all(p)
+ {
+ if (resp(p) && (volume(p) < lambda))
+ {
+ resp(p) = false;
+ update_data(parent(p), volume(p));
+ }
+ }
+ }
+
+ } // end of run()
+
+ void make_set(const point& p)
+ {
+ parent(p) = p;
+ zpar(p) = p;
+ init_data(p);
+ }
+
+ void set_parent(const point& r, const point& p)
+ {
+ parent(r) = p;
+ merge_data(r, p);
+ }
+
+ bool is_root(const point& p) const
+ {
+ return parent(p) == p;
+ }
+
+ bool is_node(const point& p) const
+ {
+ //return is_root(p) || f(parent(p)) != f(p);
+ return (is_root(p) || resp(p));
+ }
+
+ point find_root(const point& x)
+ {
+ if (zpar(x) == x)
+ return x;
+ else
+ return zpar(x) = find_root(zpar(x));
+ }
+
+ point find_representative(const point& x)
+ {
+ if (parent(x) == x || resp(x))
+ return x;
+ else
+ return find_representative(parent(x));
+ }
+
+ void do_union(const point& n, const point& p)
+ {
+ point r = find_root(n);
+ if (r != p)
+ {
+ set_parent(r, p);
+ zpar(r) = p;
+ }
+ }
+
+ void init_data(const point& p)
+ {
+ int red =0, green = 0, blue = 0;
+
+ mln_niter(Nc) n(nbhc, p);
+ for_all(n)
+ {
+ red += ref(n).red();
+ green += ref(n).green();
+ blue += ref(n).blue();
+ }
+
+ red /= 2;
+ green /= 2;
+ blue /= 2;
+
+ volume(p) = distance(value::rgb8(red, green, blue),
+ value::rgb8(0, 0, 0));
+ resp(p) = true;
+ }
+
+ void merge_data(const point& r, const point& p)
+ {
+ if (f(p) == f(r))
+ {
+ resp(p) = false;
+ volume(r) += volume(p);
+ }
+ }
+
+ void update_data(const point& p, int val)
+ {
+ volume(p) += val;
+ if (parent(p) != p && !resp(p))
+ update_data(parent(p), val);
+ }
+
+ };
+}
+
+namespace mln
+{
+ image2d<value::int_u16> convert_to_grey(const image2d<value::rgb8>& data)
+ {
+ image2d<value::int_u16> output(data.domain());
+ mln_piter_(image2d<value::int_u16>) p(output.domain());
+ for_all(p)
+ output(p) = (int) (data(p).red() * 0.3 + data(p).green() * 0.58 + data(p).blue()) * 0.12;
+ return output;
+ }
+} // end of mln
+
+namespace mln
+{
+
+ struct colorize : Function_v2v< colorize >
+ {
+ typedef value::rgb8 result;
+ colorize(unsigned max)
+ : lut(max + 1)
+ {
+ lut[0] = literal::black;
+ for (unsigned i = 1; i <= max; ++i)
+ lut[i] = result(100 + std::rand() % 150,
+ 100 + std::rand() % 150,
+ 100 + std::rand() % 150);
+ }
+ result operator()(unsigned i) const
+ {
+ return lut[i];
+ }
+ std::vector<result> lut;
+ };
+
+ template <typename I>
+ I display_edge(const I& ima, mln_value(I) bg, unsigned zoom)
+ {
+ unsigned nrows = ima.nrows() / 2 + 1;
+ unsigned ncols = ima.ncols() / 2 + 1;
+ I output(nrows * (zoom + 1) - 1,
+ ncols * (zoom + 1) - 1);
+ level::fill(output, bg);
+
+ mln_VAR(edge, ima | is_edge);
+ mln_piter(edge_t) p(edge.domain());
+ for_all(p)
+ if (p.row() % 2) // horizontal edge
+ {
+ unsigned row = (p.row() / 2 + 1) * (zoom + 1) - 1;
+ unsigned col = (p.col() / 2) * (zoom + 1);
+ for (unsigned i = 0; i < zoom; ++i)
+ output.at(row, col + i) = ima(p);
+ }
+ else // vertical edge
+ {
+ unsigned row = (p.row() / 2) * (zoom + 1);
+ unsigned col = (p.col() / 2 + 1) * (zoom + 1) - 1;
+ for (unsigned i = 0; i < zoom; ++i)
+ output.at(row + i, col) = ima(p);
+ }
+ return output;
+ }
+
+
+ template <typename I>
+ I display_edge(const I& ima, unsigned zoom)
+ {
+ unsigned nrows = ima.nrows() / 2 + 1;
+ unsigned ncols = ima.ncols() / 2 + 1;
+ I output(nrows * (zoom + 1) - 1,
+ ncols * (zoom + 1) - 1);
+
+ mln_VAR( cell, ima | is_cell );
+ mln_piter(cell_t) q(cell.domain());
+ for_all(q)
+ {
+ unsigned row = (q.row() / 2) * (zoom + 1);
+ unsigned col = (q.col() / 2) * (zoom + 1);
+ for (unsigned i = 0; i < zoom; ++i)
+ for (unsigned j = 0; j < zoom; ++j)
+ output.at(row + i, col + j) = ima(q);
+ }
+
+ mln_VAR( edge, ima | is_edge );
+ mln_piter(edge_t) p(edge.domain());
+ for_all(p)
+ if (p.row() % 2) // horizontal edge
+ {
+ unsigned row = (p.row() / 2 + 1) * (zoom + 1) - 1;
+ unsigned col = (p.col() / 2) * (zoom + 1);
+ for (unsigned i = 0; i < zoom; ++i)
+ output.at(row, col + i) = ima(p);
+ }
+ else // vertical edge
+ {
+ unsigned row = (p.row() / 2) * (zoom + 1);
+ unsigned col = (p.col() / 2 + 1) * (zoom + 1) - 1;
+ for (unsigned i = 0; i < zoom; ++i)
+ output.at(row + i, col) = ima(p);
+ }
+ return output;
+ }
+
+
+ namespace morpho
+ {
+
+ template <typename I, typename N>
+ mln_concrete(I)
+ dilation(const I& input, const N& nbh)
+ {
+ typedef mln_value(I) V;
+
+ mln_concrete(I) output;
+ initialize(output, input);
+
+ mln_piter(I) p(input.domain());
+ mln_niter(N) n(nbh, p);
+ for_all(p)
+ {
+ for_all(n)
+ if (input.has(n) && input(n) != value::rgb8(0,0,0))
+ output(p) = input(n);
+ }
+ return output;
+ }
+ } // mln::morpho
+
+} // mln
+
+
+
+template <typename T>
+mln::image2d<T>
+image2cells(const mln::image2d<T>& input)
+{
+ mln::image2d<T> output(2 * input.nrows() - 1,
+ 2 * input.ncols() - 1);
+ for (unsigned row = 0; row < input.nrows(); ++row)
+ for (unsigned col = 0; col < input.ncols(); ++col)
+ output.at(2 * row, 2 * col) = input.at(row, col);
+ return output;
+}
+
+
+template <typename T>
+mln::image2d<T>
+cells2image(const mln::image2d<T>& input)
+{
+ mln::image2d<T> output((input.nrows() + 1) / 2,
+ (input.ncols() + 1) / 2);
+ for (unsigned row = 0; row < input.nrows(); row += 2)
+ for (unsigned col = 0; col < input.ncols(); col += 2)
+ output.at(row / 2, col / 2) = input.at(row, col);
+ return output;
+}
+
+
+template <typename I, typename N, typename Ic, typename Nc>
+unsigned min_tree(const I& f, const N& nbh, const Ic& ref, const Nc& nbhc,
+ int lambda)
+{
+ using namespace mln;
+
+ min_tree_<I,N,Ic,Nc> run(f, nbh, ref, nbhc, lambda);
+
+ mln_piter(I) p(f.domain());
+ unsigned nnodes = 0;
+ for_all(p)
+ {
+ if (run.is_node(p))
+ {
+ ++nnodes;
+ }
+ }
+
+ colorize colors(nnodes);
+ image2d<value::rgb8> tmp(ref.domain());
+ level::fill(tmp, ref);
+
+ mln_piter(I) q(f.domain());
+ unsigned int i = 0;
+ for_all(q)
+ {
+ if (run.is_node(q))
+ {
+ tmp(q) = colors(i);
+ i++;
+ }
+ }
+ mln_piter(I) r(f.domain());
+ for_all(r)
+ {
+ if (!run.is_node(r))
+ {
+ tmp(r) = tmp(run.find_representative(r));
+ }
+ }
+
+ image2d<value::rgb8> to_display(tmp.domain());
+
+ level::fill(to_display, value::rgb8(255, 255, 255));
+ level::paste((tmp | is_edge), to_display);
+ level::paste(morpho::dilation(to_display, c4()), to_display);
+
+ io::ppm::save(display_edge(tmp, literal::black, 3),
+ "edge.ppm");
+ io::ppm::save(tmp, "full.ppm");
+ io::ppm::save(cells2image(to_display), "colorize.ppm");
+
+
+ return nnodes;
+}
+
+
+template <typename I>
+I
+do_it(I& input, int lambda, unsigned& nbasins)
+{
+ using namespace mln;
+
+ /// Graph creation
+ I graph;
+ create_graph(input, graph, value::rgb8(0, 0, 0));
+
+ // Initialization
+ image2d<value::int_u16> ima = convert_to_grey(graph);
+
+ // Neigbhorhood
+ // e2c
+ bool e2c_h[] = { 0, 1, 0,
+ 0, 0, 0,
+ 0, 1, 0 };
+ bool e2c_v[] = { 0, 0, 0,
+ 1, 0, 1,
+ 0, 0, 0 };
+
+ mln_VAR(e2c, make::double_neighb2d(is_row_odd, e2c_h, e2c_v));
+
+ bool e2e_h[] = { 0, 0, 1, 0, 0,
+ 0, 1, 0, 1, 0,
+ 0, 0, 0, 0, 0,
+ 0, 1, 0, 1, 0,
+ 0, 0, 1, 0, 0 };
+
+ bool e2e_v[] = { 0, 0, 0, 0, 0,
+ 0, 1, 0, 1, 0,
+ 1, 0, 0, 0, 1,
+ 0, 1, 0, 1, 0,
+ 0, 0, 0, 0, 0 };
+ mln_VAR(e2e, make::double_neighb2d(is_row_odd, e2e_h, e2e_v));
+
+ // Algorithm
+ distance(extend((graph | is_edge).rw(), pw::value(graph)), e2c, ima);
+
+ io::pgm::save(ima, "edge.pgm");
+
+ nbasins = min_tree((ima | is_edge), e2e, graph, e2c, lambda);
+
+ return graph;
+}
+
+void usage(char* argv[])
+{
+ std::cerr << "usage: " << argv[0] << " input.pgm lambda" << std::endl;
+ std::cerr << " lambda >= 0" << std::endl;
+ abort();
+}
+
+int main(int argc, char* argv[])
+{
+ using namespace mln;
+
+ if (argc != 3)
+ usage(argv);
+
+ int lambda = atoi(argv[2]);
+ if (lambda < 0)
+ usage(argv);
+
+ image2d<value::rgb8> ima;
+ io::ppm::load(ima, argv[1]);
+
+ unsigned nbasins;
+ image2d<value::rgb8> output = do_it(ima, lambda, nbasins);
+
+ //io::ppm::save(output, argv[3]);
+}
Index: ballas/color/reference.cc
--- ballas/color/reference.cc (revision 0)
+++ ballas/color/reference.cc (revision 0)
@@ -0,0 +1,556 @@
+# include <mln/core/var.hh>
+
+# include <mln/core/image/image2d.hh>
+# include <mln/core/image/image_if.hh>
+# include <mln/core/image/extended.hh>
+# include <mln/core/routine/extend.hh>
+
+# include <mln/core/alias/window2d.hh>
+# include <mln/core/alias/neighb2d.hh>
+# include <mln/make/double_neighb2d.hh>
+# include <mln/core/site_set/p_centered.hh>
+
+# include <mln/literal/origin.hh>
+# include <mln/literal/black.hh>
+# include <mln/literal/white.hh>
+
+# include <mln/value/int_u8.hh>
+# include <mln/io/pgm/load.hh>
+# include <mln/io/pgm/save.hh>
+
+# include <mln/value/rgb8.hh>
+# include <mln/io/ppm/load.hh>
+# include <mln/io/ppm/save.hh>
+
+# include <mln/accu/min_max.hh>
+# include <mln/accu/mean.hh>
+
+# include <mln/fun/i2v/array.hh>
+# include <mln/fun/p2v/iota.hh>
+
+# include <mln/level/paste.hh>
+# include <mln/level/fill.hh>
+# include <mln/level/transform.hh>
+# include <mln/extension/fill.hh>
+# include <mln/convert/to.hh>
+
+# include <mln/linear/gaussian.hh>
+
+# include <mln/morpho/meyer_wst.hh>
+# include <mln/morpho/closing_volume.hh>
+
+# include <mln/make/w_window2d.hh>
+
+# include <mln/debug/println.hh>
+
+// Laplacian method
+namespace mln
+{
+ namespace linear
+ {
+ // required to deal with a input image that differ from the output since I
+ //don't succeed in using a float image in entry.
+ template <class I, class O>
+ inline
+ void
+ gaussian_2nd_derivative(const Image<I>& input, float sigma, Image<O>& output)
+ {
+ mln_precondition(exact(input).has_data());
+
+ impl::recursivefilter_coef_
+ coef(-1.331f, 3.661f,
+ 1.24f, 1.314f,
+ 0.3225f, -1.738f,
+ 0.748f, 2.166f,
+ sigma, impl::gaussian_2nd_deriv_coef_norm_);
+ impl::generic_filter_common_(mln_trait_value_nature(mln_value(I))(),
+ input, coef, sigma, output);
+ }
+
+ template <class I, class O>
+ inline
+ void
+ gaussian_1st_derivative(const Image<I>& input, float sigma, Image<O>& output)
+ {
+ mln_precondition(exact(input).has_data());
+
+ impl::recursivefilter_coef_
+ coef(-0.6472f, -4.531f,
+ 1.527f, 1.516f,
+ 0.6494f, 0.9557f,
+ 0.6719f, 2.072f,
+ sigma, impl::gaussian_1st_deriv_coef_norm_);
+ impl::generic_filter_common_(mln_trait_value_nature(mln_value(I))(),
+ input, coef, sigma, output);
+ }
+ }
+}
+
+// Gradient + watershed method
+namespace mln
+{
+ namespace morpho
+ {
+ template <typename I, typename N>
+ mln_concrete(I)
+ closing_volume(const I& input, const Neighborhood<N>& nbh, std::size_t lambda)
+ {
+ mln_concrete(I) output;
+ initialize(output, input);
+ closing_volume(input, nbh, lambda, output);
+ return output;
+ }
+ }
+} // !mln
+
+namespace mln
+{
+ image2d<value::int_u8> convert_to_grey(const image2d<value::rgb8>& data)
+ {
+ image2d<value::int_u8> output(data.domain());
+ mln_piter_(image2d<value::int_u8>) p(output.domain());
+ for_all(p)
+ output(p) = (int) (data(p).red() * 0.3 + data(p).green() * 0.58 + data(p).blue()) * 0.12;
+ return output;
+ }
+} // !mln
+
+// Functions
+
+inline
+bool is_row_odd(const mln::point2d& p)
+{
+ return p.row() % 2;
+}
+
+inline
+bool is_cell(const mln::point2d& p)
+{
+ return p.row() % 2 == 0 && p.col() % 2 == 0;
+}
+
+inline
+bool is_edge(const mln::point2d& p)
+{
+ return p.row() % 2 + p.col() % 2 == 1;
+}
+
+inline
+bool is_point(const mln::point2d& p)
+{
+ return p.row() % 2 && p.col() % 2;
+}
+
+inline
+bool is_not_edge(const mln::point2d& p)
+{
+ return ! is_edge(p);
+}
+
+
+
+namespace mln
+{
+
+ namespace border
+ {
+
+ template <typename I>
+ void
+ fill(I& ima, const mln_value(I)& v)
+ {
+ const int nrows = ima.nrows();
+ const int ncols = ima.ncols();
+ for (int r = -1; r <= nrows; ++r)
+ {
+ ima.at(r, -1) = v;
+ ima.at(r, ncols) = v;
+ }
+ for (int c = -1; c <= ncols; ++c)
+ {
+ ima.at(-1, c) = v;
+ ima.at(nrows, c) = v;
+ }
+ }
+
+ } // mln::border
+
+ namespace accu
+ {
+
+ template <typename I, typename L, typename A, typename V>
+ inline
+ void
+ compute(const Image<I>& input_,
+ const Image<L>& label_,
+ const Accumulator<A>&,
+ V& v)
+ {
+ trace::entering("accu::compute");
+
+ const I& input = exact(input_);
+ const L& label = exact(label_);
+
+ const unsigned n = v.size();
+ std::vector<A> a(n);
+
+ mln_piter(I) p(input.domain());
+ for_all(p)
+ a[label(p)].take(input(p));
+
+ for (unsigned l = 1; l < n; ++l)
+ v(l) = a[l].to_result();
+
+ trace::exiting("accu::compute");
+ }
+
+ } // mln::accu
+
+ namespace morpho
+ {
+
+ template <typename I, typename N>
+ mln_concrete(I)
+ gradient(const I& input, const N& nbh)
+ {
+ mln_concrete(I) output;
+ initialize(output, input);
+ accu::min_max<mln_value(I)> mm;
+
+ mln_piter(I) p(input.domain());
+ mln_niter(N) n(nbh, p);
+ for_all(p)
+ {
+ mm.init();
+ for_all(n) if (input.has(n))
+ mm.take(input(n));
+ output(p) = mm.second() - mm.first();
+ }
+ return output;
+ }
+
+ template <typename I, typename N>
+ mln_concrete(I)
+ dilation(const I& input, const N& nbh)
+ {
+ typedef mln_value(I) V;
+
+ mln_concrete(I) output;
+ initialize(output, input);
+
+ mln_piter(I) p(input.domain());
+ mln_niter(N) n(nbh, p);
+ for_all(p)
+ {
+ for_all(n)
+ if (input.has(n) && input(n) != value::rgb8(0,0,0))
+ output(p) = input(n);
+ }
+ return output;
+ }
+ } // mln::morpho
+
+
+ struct colorize : Function_v2v< colorize >
+ {
+ typedef value::rgb8 result;
+ colorize(unsigned max)
+ : lut(max + 1)
+ {
+ lut[0] = literal::black;
+ for (unsigned i = 1; i <= max; ++i)
+ lut[i] = result(100 + std::rand() % 150,
+ 100 + std::rand() % 150,
+ 100 + std::rand() % 150);
+ }
+ result operator()(unsigned i) const
+ {
+ return lut[i];
+ }
+ std::vector<result> lut;
+ };
+
+
+ template <typename I>
+ I display_edge_with_bg(const I& ima, unsigned zoom, mln_value(I) bg)
+ {
+ unsigned nrows = ima.nrows() / 2 + 1;
+ unsigned ncols = ima.ncols() / 2 + 1;
+ I output(nrows * (zoom + 1) - 1,
+ ncols * (zoom + 1) - 1);
+ level::fill(output, bg);
+ mln_VAR( edge, ima | is_edge );
+ mln_piter(edge_t) p(edge.domain());
+ for_all(p)
+ if (p.row() % 2) // horizontal edge
+ {
+ unsigned row = (p.row() / 2 + 1) * (zoom + 1) - 1;
+ unsigned col = (p.col() / 2) * (zoom + 1);
+ for (unsigned i = 0; i < zoom; ++i)
+ output.at(row, col + i) = ima(p);
+ }
+ else // vertical edge
+ {
+ unsigned row = (p.row() / 2) * (zoom + 1);
+ unsigned col = (p.col() / 2 + 1) * (zoom + 1) - 1;
+ for (unsigned i = 0; i < zoom; ++i)
+ output.at(row + i, col) = ima(p);
+ }
+ return output;
+ }
+
+ template <typename I>
+ I display_edge(const I& ima, unsigned zoom)
+ {
+ unsigned nrows = ima.nrows() / 2 + 1;
+ unsigned ncols = ima.ncols() / 2 + 1;
+ I output(nrows * (zoom + 1) - 1,
+ ncols * (zoom + 1) - 1);
+
+ mln_VAR( cell, ima | is_cell );
+ mln_piter(cell_t) q(cell.domain());
+ for_all(q)
+ {
+ unsigned row = (q.row() / 2) * (zoom + 1);
+ unsigned col = (q.col() / 2) * (zoom + 1);
+ for (unsigned i = 0; i < zoom; ++i)
+ for (unsigned j = 0; j < zoom; ++j)
+ output.at(row + i, col + j) = ima(q);
+ }
+
+ mln_VAR( edge, ima | is_edge );
+ mln_piter(edge_t) p(edge.domain());
+ for_all(p)
+ if (p.row() % 2) // horizontal edge
+ {
+ unsigned row = (p.row() / 2 + 1) * (zoom + 1) - 1;
+ unsigned col = (p.col() / 2) * (zoom + 1);
+ for (unsigned i = 0; i < zoom; ++i)
+ output.at(row, col + i) = ima(p);
+ }
+ else // vertical edge
+ {
+ unsigned row = (p.row() / 2) * (zoom + 1);
+ unsigned col = (p.col() / 2 + 1) * (zoom + 1) - 1;
+ for (unsigned i = 0; i < zoom; ++i)
+ output.at(row + i, col) = ima(p);
+ }
+ return output;
+ }
+
+} // mln
+
+
+
+template <typename T>
+mln::image2d<T>
+image2cells(const mln::image2d<T>& input)
+{
+ mln::image2d<T> output(2 * input.nrows() - 1,
+ 2 * input.ncols() - 1);
+ for (int row = 0; row < input.nrows(); ++row)
+ for (int col = 0; col < input.ncols(); ++col)
+ output.at(2 * row, 2 * col) = input.at(row, col);
+ return output;
+}
+
+namespace mln {
+
+ template <typename I, typename N, typename M>
+ mln_concrete(I)
+ mean(const I& input, const N& nbh, const M& nbh2)
+ {
+ mln_concrete(I) output;
+ initialize(output, input);
+
+ mln_piter(I) p(input.domain());
+ mln_niter(N) n(nbh, p);
+ mln_niter(M) m(nbh2, p);
+ for_all(p)
+ {
+ if (is_edge(p))
+ {
+ int nb = 0;
+ int r = 0, g = 0, b = 0;
+ for_all(n)
+ {
+ if (input.has(n))
+ {
+ r += input(n).red();
+ g += input(n).green();
+ b += input(n).blue();
+ ++nb;
+ }
+ }
+ output(p) = value::rgb8(r / nb, g / nb, b / nb);
+ }
+ if (is_point(p))
+ {
+ int nb = 0;
+ int r = 0, g = 0, b = 0;
+ for_all(m)
+ {
+ if (input.has(m))
+ {
+ r += input(m).red();
+ g += input(m).green();
+ b += input(m).blue();
+ ++nb;
+ }
+ }
+ output(p) = value::rgb8(r / nb, g / nb, b / nb);
+ }
+ if (is_cell(p))
+ output(p) = input(p);
+ }
+ return output;
+ }
+
+}
+
+template <typename T>
+mln::image2d<T>
+cells2image(const mln::image2d<T>& input)
+{
+ mln::image2d<T> output((input.nrows() + 1) / 2,
+ (input.ncols() + 1) / 2);
+ for (int row = 0; row < input.nrows(); row += 2)
+ for (int col = 0; col < input.ncols(); col += 2)
+ output.at(row / 2, col / 2) = input.at(row, col);
+ return output;
+}
+
+
+
+
+template <typename I>
+mln_concrete(I)
+do_it(I& input, float lambda, unsigned& nbasins)
+{
+ using namespace mln;
+
+ /**************************/
+ /* Neighborhood defintion */
+ /**************************/
+
+ // e2c
+ bool e2c_h[] = { 0, 1, 0,
+ 0, 0, 0,
+ 0, 1, 0 };
+ bool e2c_v[] = { 0, 0, 0,
+ 1, 0, 1,
+ 0, 0, 0 };
+ mln_VAR( e2c, make::double_neighb2d(is_row_odd, e2c_h, e2c_v) );
+
+ // e2e
+ bool e2e_h[] = { 0, 0, 1, 0, 0,
+ 0, 1, 0, 1, 0,
+ 0, 0, 0, 0, 0,
+ 0, 1, 0, 1, 0,
+ 0, 0, 1, 0, 0 };
+ bool e2e_v[] = { 0, 0, 0, 0, 0,
+ 0, 1, 0, 1, 0,
+ 1, 0, 0, 0, 1,
+ 0, 1, 0, 1, 0,
+ 0, 0, 0, 0, 0 };
+ mln_VAR( e2e, make::double_neighb2d(is_row_odd, e2e_h, e2e_v) );
+
+ // e2p
+ bool e2p_h[] = { 1, 0, 1,
+ 0, 0, 0,
+ 1, 0, 1 };
+ bool e2p_v[] = { 1, 0, 1,
+ 0, 0, 0,
+ 1, 0, 1 };
+ mln_VAR( e2p, make::double_neighb2d(is_row_odd, e2p_h, e2p_v) );
+
+ /******************/
+ /* Initialisation */
+ /******************/
+
+ I output = mean(image2cells(input), e2c, e2p);
+ io::ppm::save(output, "tmp_input.ppm");
+ //image2d<value::int_u8> ima = convert_to_grey(output);
+ image2d<value::int_u8> imau = convert_to_grey(output);
+ io::pgm::save(imau, "tmp_grey_input.pgm");
+
+ image2d<float> ima(exact(imau).domain());
+
+ // cell
+ mln_VAR(cell, imau | is_cell);
+
+ // edge
+ mln_VAR(edge, extend((imau | is_edge).rw(), pw::value(imau)));
+
+ // FIXME until laplacian is working use gradient / closing_area / wst
+
+ linear::gaussian_2nd_derivative(imau, lambda, ima);
+
+ mln_piter(edge_t) p(edge.domain());
+ for_all(p)
+ {
+ if (is_row_odd(p))
+ {
+ mln_value(image2d<float>) t = ima.at(p.row() - 1, p.col());
+ mln_value(image2d<float>) b = ima.at(p.row() + 1, p.col());
+ if ((t > 0 && b < 0) || (t < 0 && b > 0))
+ output(p) = value::rgb8(255,0,0);
+ }
+ else
+ {
+ mln_value(image2d<float>) r = ima.at(p.row(), p.col() - 1);
+ mln_value(image2d<float>) d = ima.at(p.row(), p.col() + 1);
+ if ((r > 0 && d < 0) || (r < 0 && d > 0))
+ output(p) = value::rgb8(255,0,0);
+ }
+ }
+
+#if 0
+ level::paste(morpho::gradient(edge, e2c), edge);
+ level::paste(morpho::closing_volume(edge, e2e, lambda), edge);
+ level::fill(edge, morpho::meyer_wst(edge, e2e, nbasins));
+
+ // Fill regions (with colorize) (won't work with laplacian...)
+
+ colorize colors(nbasins);
+
+ image2d<value::rgb8> cells(ima.domain());
+ level::fill(cells, literal::white);
+ level::paste(level::transform(edge, colors), cells);
+ io::ppm::save(display_edge_with_bg(cells, 3, literal::white), "tmp_edge.ppm");
+
+ // Move the color of an edge which is non black in the cell
+ level::paste(morpho::dilation(cells, c4()), cells);
+#endif
+
+ //cells = convert_to_rgb8(ima);
+
+ return output;
+}
+
+void usage(char* argv[])
+{
+ std::cerr << "usage: " << argv[0] << " input.pgm lambda output.ppm" << std::endl;
+ std::cerr << " lambda >= 0" << std::endl;
+ abort();
+}
+
+int main(int argc, char* argv[])
+{
+ using namespace mln;
+
+ if (argc != 4)
+ usage(argv);
+
+ float lambda = atof(argv[2]);
+ if (lambda < 0)
+ usage(argv);
+
+ image2d<value::rgb8> ima;
+ io::ppm::load(ima, argv[1]);
+
+ unsigned nbasins;
+ image2d<value::rgb8> output = do_it(ima, lambda, nbasins);
+
+ io::ppm::save(display_edge(output, 3), argv[3]);
+}
Index: ballas/color/min_tree_area_filter.cc
--- ballas/color/min_tree_area_filter.cc (revision 0)
+++ ballas/color/min_tree_area_filter.cc (revision 0)
@@ -0,0 +1,505 @@
+# include <mln/core/var.hh>
+
+# include <mln/core/image/image2d.hh>
+# include <mln/core/image/image_if.hh>
+# include <mln/core/image/extended.hh>
+# include <mln/core/routine/extend.hh>
+
+# include <mln/core/alias/window2d.hh>
+# include <mln/core/alias/neighb2d.hh>
+# include <mln/make/double_neighb2d.hh>
+# include <mln/core/site_set/p_centered.hh>
+
+# include <mln/literal/origin.hh>
+# include <mln/literal/black.hh>
+# include <mln/literal/white.hh>
+
+# include <mln/value/int_u8.hh>
+# include <mln/value/int_u16.hh>
+# include <mln/io/pgm/load.hh>
+# include <mln/io/pgm/save.hh>
+
+# include <mln/value/rgb8.hh>
+# include <mln/io/ppm/load.hh>
+# include <mln/io/ppm/save.hh>
+
+# include <mln/accu/min_max.hh>
+
+# include <mln/fun/i2v/array.hh>
+# include <mln/fun/p2v/iota.hh>
+
+# include <mln/level/paste.hh>
+# include <mln/level/fill.hh>
+# include <mln/level/transform.hh>
+# include <mln/extension/fill.hh>
+
+# include <mln/morpho/meyer_wst.hh>
+# include <mln/morpho/closing_area.hh>
+
+# include <mln/debug/println.hh>
+
+# include "src/distance.hh"
+
+namespace mln
+{
+ template <typename I, typename N, typename Ic, typename Nc>
+ struct min_tree_
+ {
+ typedef mln_site(I) point;
+ typedef p_array<point> S;
+
+ // in:
+ const I& f;
+ const N& nbh;
+ const Ic& ref;
+ const Nc& nbhc;
+
+ // aux:
+ S s;
+ mln_ch_value(I, bool) deja_vu;
+ mln_ch_value(I, point) parent;
+ mln_ch_value(I, bool) resp;
+ mln_ch_value(I, point) zpar;
+
+ // attached data:
+ int lambda;
+ mln_ch_value(I, int) area;
+ //mln_ch_value(Ic, value::rgb8) values;
+ //initialize(values, ref);
+ //mln_ch_value(I, int) comp;
+
+ min_tree_(const I& f, const N& nbh, const Ic& ref, const Nc& nbhc,
+ int lambda)
+ : f(f),
+ nbh(nbh),
+ ref(ref),
+ nbhc(nbhc),
+ lambda(lambda)
+ {
+ run();
+ }
+
+ void run()
+ {
+ // init
+ {
+ initialize(deja_vu, f);
+ initialize(parent, f);
+ initialize(resp, f);
+ initialize(zpar, f);
+ initialize(area, f);
+ //initialize(comp, f);
+
+ mln::level::fill(deja_vu, false);
+ //mln::level::fill(resp, false);
+ mln::level::fill(area, 0);
+
+ s = level::sort_psites_increasing(f);
+ }
+
+ // first pass
+ {
+ mln_fwd_piter(S) p(s);
+ mln_niter(N) n(nbh, p);
+ for_all(p)
+ {
+ make_set(p);
+ for_all(n)
+ if (f.has(n) && deja_vu(n))
+ do_union(n, p);
+ deja_vu(p) = true;
+ }
+ }
+
+ // second pass: canonization
+ {
+ mln_bkd_piter(S) p(s);
+ for_all(p)
+ {
+ point q = parent(p);
+ if (f(parent(q)) == f(q))
+ {
+ parent(p) = parent(q);
+ resp(q) = false;
+ }
+ }
+ }
+
+ // third pass: Merging region with area < lambda
+ {
+ mln_fwd_piter(S) p(s);
+ for_all(p)
+ {
+ if (resp(p) && area(p) < lambda)
+ {
+ resp(p) = false;
+ update_data(parent(p), area(p));
+ }
+ }
+ }
+ } // end of run()
+
+ void make_set(const point& p)
+ {
+ parent(p) = p;
+ zpar(p) = p;
+ init_data(p);
+ }
+
+ void set_parent(const point& r, const point& p)
+ {
+ parent(r) = p;
+ merge_data(r, p);
+ }
+
+ bool is_root(const point& p) const
+ {
+ return parent(p) == p;
+ }
+
+ bool is_node(const point& p) const
+ {
+ //return is_root(p) || f(parent(p)) != f(p);
+ return (is_root(p) || resp(p));
+ }
+
+ point find_root(const point& x)
+ {
+ if (zpar(x) == x)
+ return x;
+ else
+ return zpar(x) = find_root(zpar(x));
+ }
+
+ point find_representative(const point& x)
+ {
+ if (parent(x) == x || resp(x))
+ return x;
+ else
+ return find_representative(parent(x));
+ }
+
+ void do_union(const point& n, const point& p)
+ {
+ point r = find_root(n);
+ if (r != p)
+ {
+ set_parent(r, p);
+ zpar(r) = p;
+ }
+ }
+
+ void init_data(const point& p)
+ {
+ area(p) = 1;
+ resp(p) = true;
+ }
+
+ void merge_data(const point& r, const point& p)
+ {
+ if (f(p) == f(r))
+ {
+ resp(p) = false;
+ area(p) += area(r);
+ }
+ }
+
+ void update_data(const point& p, int val)
+ {
+ area(p) += val;
+ if (parent(p) != p && !resp(p))
+ update_data(parent(p), val);
+ }
+
+ };
+}
+
+namespace mln
+{
+ image2d<value::int_u16> convert_to_grey(const image2d<value::rgb8>& data)
+ {
+ image2d<value::int_u16> output(data.domain());
+ mln_piter_(image2d<value::int_u16>) p(output.domain());
+ for_all(p)
+ output(p) = (int) (data(p).red() * 0.3 + data(p).green() * 0.58 + data(p).blue()) * 0.12;
+ return output;
+ }
+} // end of mln
+
+namespace mln
+{
+
+ struct colorize : Function_v2v< colorize >
+ {
+ typedef value::rgb8 result;
+ colorize(unsigned max)
+ : lut(max + 1)
+ {
+ lut[0] = literal::black;
+ for (unsigned i = 1; i <= max; ++i)
+ lut[i] = result(100 + std::rand() % 150,
+ 100 + std::rand() % 150,
+ 100 + std::rand() % 150);
+ }
+ result operator()(unsigned i) const
+ {
+ return lut[i];
+ }
+ std::vector<result> lut;
+ };
+
+ template <typename I>
+ I display_edge(const I& ima, mln_value(I) bg, unsigned zoom)
+ {
+ unsigned nrows = ima.nrows() / 2 + 1;
+ unsigned ncols = ima.ncols() / 2 + 1;
+ I output(nrows * (zoom + 1) - 1,
+ ncols * (zoom + 1) - 1);
+ level::fill(output, bg);
+
+ mln_VAR(edge, ima | is_edge);
+ mln_piter(edge_t) p(edge.domain());
+ for_all(p)
+ if (p.row() % 2) // horizontal edge
+ {
+ unsigned row = (p.row() / 2 + 1) * (zoom + 1) - 1;
+ unsigned col = (p.col() / 2) * (zoom + 1);
+ for (unsigned i = 0; i < zoom; ++i)
+ output.at(row, col + i) = ima(p);
+ }
+ else // vertical edge
+ {
+ unsigned row = (p.row() / 2) * (zoom + 1);
+ unsigned col = (p.col() / 2 + 1) * (zoom + 1) - 1;
+ for (unsigned i = 0; i < zoom; ++i)
+ output.at(row + i, col) = ima(p);
+ }
+ return output;
+ }
+
+ template <typename I>
+ I display_edge(const I& ima, unsigned zoom)
+ {
+ unsigned nrows = ima.nrows() / 2 + 1;
+ unsigned ncols = ima.ncols() / 2 + 1;
+ I output(nrows * (zoom + 1) - 1,
+ ncols * (zoom + 1) - 1);
+
+ mln_VAR( cell, ima | is_cell );
+ mln_piter(cell_t) q(cell.domain());
+ for_all(q)
+ {
+ unsigned row = (q.row() / 2) * (zoom + 1);
+ unsigned col = (q.col() / 2) * (zoom + 1);
+ for (unsigned i = 0; i < zoom; ++i)
+ for (unsigned j = 0; j < zoom; ++j)
+ output.at(row + i, col + j) = ima(q);
+ }
+
+ mln_VAR( edge, ima | is_edge );
+ mln_piter(edge_t) p(edge.domain());
+ for_all(p)
+ if (p.row() % 2) // horizontal edge
+ {
+ unsigned row = (p.row() / 2 + 1) * (zoom + 1) - 1;
+ unsigned col = (p.col() / 2) * (zoom + 1);
+ for (unsigned i = 0; i < zoom; ++i)
+ output.at(row, col + i) = ima(p);
+ }
+ else // vertical edge
+ {
+ unsigned row = (p.row() / 2) * (zoom + 1);
+ unsigned col = (p.col() / 2 + 1) * (zoom + 1) - 1;
+ for (unsigned i = 0; i < zoom; ++i)
+ output.at(row + i, col) = ima(p);
+ }
+ return output;
+ }
+
+
+ namespace morpho
+ {
+
+ template <typename I, typename N>
+ mln_concrete(I)
+ dilation(const I& input, const N& nbh)
+ {
+ typedef mln_value(I) V;
+
+ mln_concrete(I) output;
+ initialize(output, input);
+
+ mln_piter(I) p(input.domain());
+ mln_niter(N) n(nbh, p);
+ for_all(p)
+ {
+ for_all(n)
+ if (input.has(n) && input(n) != value::rgb8(0,0,0))
+ output(p) = input(n);
+ }
+ return output;
+ }
+ } // mln::morpho
+
+
+} // mln
+
+
+
+template <typename T>
+mln::image2d<T>
+image2cells(const mln::image2d<T>& input)
+{
+ mln::image2d<T> output(2 * input.nrows() - 1,
+ 2 * input.ncols() - 1);
+ for (unsigned row = 0; row < input.nrows(); ++row)
+ for (unsigned col = 0; col < input.ncols(); ++col)
+ output.at(2 * row, 2 * col) = input.at(row, col);
+ return output;
+}
+
+
+template <typename T>
+mln::image2d<T>
+cells2image(const mln::image2d<T>& input)
+{
+ mln::image2d<T> output((input.nrows() + 1) / 2,
+ (input.ncols() + 1) / 2);
+ for (unsigned row = 0; row < input.nrows(); row += 2)
+ for (unsigned col = 0; col < input.ncols(); col += 2)
+ output.at(row / 2, col / 2) = input.at(row, col);
+ return output;
+}
+
+
+template <typename I, typename N, typename Ic, typename Nc>
+unsigned min_tree(const I& f, const N& nbh, const Ic& ref, const Nc& nbhc,
+ int lambda)
+{
+ using namespace mln;
+
+ min_tree_<I,N,Ic,Nc> run(f, nbh, ref, nbhc, lambda);
+
+
+ mln_piter(I) p(f.domain());
+ unsigned nnodes = 0;
+ for_all(p)
+ {
+ if (run.is_node(p))
+ {
+ std::cout << "nodes: " << p << std::endl;
+ ++nnodes;
+ }
+ }
+
+ colorize colors(nnodes);
+ image2d<value::rgb8> tmp(ref.domain());
+ level::fill(tmp, ref);
+
+ mln_piter(I) q(f.domain());
+ unsigned int i = 0;
+ for_all(q)
+ {
+ if (run.is_node(q))
+ {
+ tmp(q) = colors(i);
+ i++;
+ }
+ }
+ mln_piter(I) r(f.domain());
+ for_all(r)
+ {
+ if (!run.is_node(r))
+ {
+ tmp(r) = tmp(run.find_representative(r));
+ }
+ }
+
+
+ image2d<value::rgb8> to_display(tmp.domain());
+
+ level::fill(to_display, value::rgb8(255, 255, 255));
+ level::paste((tmp | is_edge), to_display);
+ level::paste(morpho::dilation(to_display, c4()), to_display);
+
+ io::ppm::save(display_edge(tmp, literal::white, 3),
+ "edge.ppm");
+ io::ppm::save(tmp, "full.ppm");
+ io::ppm::save(cells2image(to_display), "colorize.ppm");
+
+ return nnodes;
+}
+
+
+template <typename I>
+I
+do_it(I& input, int lambda, unsigned& nbasins)
+{
+ using namespace mln;
+
+ /// Graph creation
+ I graph;
+ create_graph(input, graph, value::rgb8(0, 0, 0));
+
+ // Initialization
+ image2d<value::int_u16> ima = convert_to_grey(graph);
+
+ // Neigbhorhood
+ // e2c
+ bool e2c_h[] = { 0, 1, 0,
+ 0, 0, 0,
+ 0, 1, 0 };
+ bool e2c_v[] = { 0, 0, 0,
+ 1, 0, 1,
+ 0, 0, 0 };
+
+ mln_VAR(e2c, make::double_neighb2d(is_row_odd, e2c_h, e2c_v));
+
+ bool e2e_h[] = { 0, 0, 1, 0, 0,
+ 0, 1, 0, 1, 0,
+ 0, 0, 0, 0, 0,
+ 0, 1, 0, 1, 0,
+ 0, 0, 1, 0, 0 };
+
+ bool e2e_v[] = { 0, 0, 0, 0, 0,
+ 0, 1, 0, 1, 0,
+ 1, 0, 0, 0, 1,
+ 0, 1, 0, 1, 0,
+ 0, 0, 0, 0, 0 };
+ mln_VAR(e2e, make::double_neighb2d(is_row_odd, e2e_h, e2e_v));
+
+ // Algorithm
+ distance(extend((graph | is_edge).rw(), pw::value(graph)), e2c, ima);
+ io::pgm::save(ima, "edge.pgm");
+
+ nbasins = min_tree((ima | is_edge), e2e, graph, e2c, lambda);
+
+ return graph;
+}
+
+void usage(char* argv[])
+{
+ std::cerr << "usage: " << argv[0] << " input.pgm lambda" << std::endl;
+ std::cerr << " lambda >= 0" << std::endl;
+ abort();
+}
+
+int main(int argc, char* argv[])
+{
+ using namespace mln;
+
+ if (argc != 3)
+ usage(argv);
+
+ int lambda = atoi(argv[2]);
+ if (lambda < 0)
+ usage(argv);
+
+ image2d<value::rgb8> ima;
+ io::ppm::load(ima, argv[1]);
+
+ unsigned nbasins;
+ image2d<value::rgb8> output = do_it(ima, lambda, nbasins);
+
+ //io::ppm::save(output, argv[3]);
+}
Index: ballas/color/min_tree_color.cc
--- ballas/color/min_tree_color.cc (revision 0)
+++ ballas/color/min_tree_color.cc (revision 0)
@@ -0,0 +1,525 @@
+# include <mln/core/var.hh>
+
+# include <mln/core/image/image2d.hh>
+# include <mln/core/image/image_if.hh>
+# include <mln/core/image/extended.hh>
+# include <mln/core/routine/extend.hh>
+
+# include <mln/core/alias/window2d.hh>
+# include <mln/core/alias/neighb2d.hh>
+# include <mln/make/double_neighb2d.hh>
+# include <mln/core/site_set/p_centered.hh>
+
+# include <mln/literal/origin.hh>
+# include <mln/literal/black.hh>
+# include <mln/literal/white.hh>
+
+# include <mln/value/int_u8.hh>
+# include <mln/value/int_u16.hh>
+# include <mln/io/pgm/load.hh>
+# include <mln/io/pgm/save.hh>
+
+# include <mln/value/rgb8.hh>
+# include <mln/io/ppm/load.hh>
+# include <mln/io/ppm/save.hh>
+
+# include <mln/accu/min_max.hh>
+
+# include <mln/fun/i2v/array.hh>
+# include <mln/fun/p2v/iota.hh>
+
+# include <mln/level/paste.hh>
+# include <mln/level/fill.hh>
+# include <mln/level/transform.hh>
+# include <mln/extension/fill.hh>
+
+# include <mln/morpho/closing_area.hh>
+
+
+# include <mln/debug/println.hh>
+
+# include "src/distance.hh"
+
+namespace mln
+{
+ template <typename I, typename N, typename Ic, typename Nc>
+ struct min_tree_
+ {
+ typedef mln_site(I) point;
+ typedef p_array<point> S;
+
+ // in:
+ const I& f;
+ const N& nbh;
+ const Ic& ref;
+ const Nc& nbhc;
+
+ // aux:
+ S s;
+ mln_ch_value(I, bool) deja_vu;
+ mln_ch_value(I, point) parent;
+ mln_ch_value(I, bool) resp;
+ mln_ch_value(I, point) zpar;
+
+ // attached data:
+ unsigned lambda;
+ mln_ch_value(I, value::rgb8) color;
+ //mln_ch_value(Ic, value::rgb8) values;
+ //initialize(values, ref);
+ //mln_ch_value(I, int) comp;
+
+ min_tree_(const I& f, const N& nbh, const Ic& ref, const Nc& nbhc,
+ int lambda)
+ : f(f),
+ nbh(nbh),
+ ref(ref),
+ nbhc(nbhc),
+ lambda(lambda)
+ {
+ run();
+ }
+
+ void run()
+ {
+ // init
+ {
+ initialize(deja_vu, f);
+ initialize(parent, f);
+ initialize(resp, f);
+ initialize(zpar, f);
+ initialize(color, f);
+ //initialize(comp, f);
+
+ mln::level::fill(deja_vu, false);
+ //mln::level::fill(resp, false);
+ mln::level::fill(color, value::rgb8(0, 0, 0));
+
+ s = level::sort_psites_increasing(f);
+ }
+
+ // first pass
+ {
+ mln_fwd_piter(S) p(s);
+ mln_niter(N) n(nbh, p);
+ for_all(p)
+ {
+ make_set(p);
+ for_all(n)
+ if (f.has(n) && deja_vu(n))
+ do_union(n, p);
+ deja_vu(p) = true;
+ }
+ }
+
+ // second pass: canonization
+ {
+ mln_bkd_piter(S) p(s);
+ for_all(p)
+ {
+ point q = parent(p);
+ if (f(parent(q)) == f(q))
+ {
+ parent(p) = parent(q);
+ resp(q) = false;
+ }
+ }
+ }
+
+ // third pass: Merging region with distance(color) < lambda
+ {
+ mln_fwd_piter(S) p(s);
+ for_all(p)
+ {
+ point q = parent(p);
+ if (resp(p) && distance(color(p), color(q)) < lambda)
+ {
+ resp(p) = false;
+ update_data(q, color(p));
+ }
+ }
+ }
+
+ } // end of run()
+
+ void make_set(const point& p)
+ {
+ parent(p) = p;
+ zpar(p) = p;
+ init_data(p);
+ }
+
+ void set_parent(const point& r, const point& p)
+ {
+ parent(r) = p;
+ merge_data(r, p);
+ }
+
+ bool is_root(const point& p) const
+ {
+ return parent(p) == p;
+ }
+
+ bool is_node(const point& p) const
+ {
+ //return is_root(p) || f(parent(p)) != f(p);
+ return (is_root(p) || resp(p));
+ }
+
+ point find_root(const point& x)
+ {
+ if (zpar(x) == x)
+ return x;
+ else
+ return zpar(x) = find_root(zpar(x));
+ }
+
+ point find_representative(const point& x)
+ {
+ if (parent(x) == x || resp(x))
+ return x;
+ else
+ return find_representative(parent(x));
+ }
+
+ void do_union(const point& n, const point& p)
+ {
+ point r = find_root(n);
+ if (r != p)
+ {
+ set_parent(r, p);
+ zpar(r) = p;
+ }
+ }
+
+ void init_data(const point& p)
+ {
+ int red =0, green = 0, blue = 0;
+
+ mln_niter(Nc) n(nbhc, p);
+ for_all(n)
+ {
+ red += ref(n).red();
+ green += ref(n).green();
+ blue += ref(n).blue();
+ }
+
+ red /= 2;
+ green /= 2;
+ blue /= 2;
+
+ color(p).red() = red;
+ color(p).green() = green;
+ color(p).blue() = blue;
+
+ resp(p) = true;
+ }
+
+ void merge_data(const point& r, const point& p)
+ {
+ if (f(p) == f(r))
+ {
+ resp(p) = false;
+ color(r) = (color(r) + color(p)) / 2;
+ }
+ }
+
+ void update_data(const point& p, value::rgb8 val)
+ {
+ color(p) = (color(p) + val) / 2;
+ if (parent(p) != p && !resp(p))
+ update_data(parent(p), color(p));
+ }
+
+ };
+}
+
+namespace mln
+{
+ image2d<value::int_u16> convert_to_grey(const image2d<value::rgb8>& data)
+ {
+ image2d<value::int_u16> output(data.domain());
+ mln_piter_(image2d<value::int_u16>) p(output.domain());
+ for_all(p)
+ output(p) = (int) (data(p).red() * 0.3 + data(p).green() * 0.58 + data(p).blue()) * 0.12;
+ return output;
+ }
+} // end of mln
+
+namespace mln
+{
+
+ struct colorize : Function_v2v< colorize >
+ {
+ typedef value::rgb8 result;
+ colorize(unsigned max)
+ : lut(max + 1)
+ {
+ lut[0] = literal::black;
+ for (unsigned i = 1; i <= max; ++i)
+ lut[i] = result(100 + std::rand() % 150,
+ 100 + std::rand() % 150,
+ 100 + std::rand() % 150);
+ }
+ result operator()(unsigned i) const
+ {
+ return lut[i];
+ }
+ std::vector<result> lut;
+ };
+
+ template <typename I>
+ I display_edge(const I& ima, mln_value(I) bg, unsigned zoom)
+ {
+ unsigned nrows = ima.nrows() / 2 + 1;
+ unsigned ncols = ima.ncols() / 2 + 1;
+ I output(nrows * (zoom + 1) - 1,
+ ncols * (zoom + 1) - 1);
+ level::fill(output, bg);
+
+ mln_VAR(edge, ima | is_edge);
+ mln_piter(edge_t) p(edge.domain());
+ for_all(p)
+ if (p.row() % 2) // horizontal edge
+ {
+ unsigned row = (p.row() / 2 + 1) * (zoom + 1) - 1;
+ unsigned col = (p.col() / 2) * (zoom + 1);
+ for (unsigned i = 0; i < zoom; ++i)
+ output.at(row, col + i) = ima(p);
+ }
+ else // vertical edge
+ {
+ unsigned row = (p.row() / 2) * (zoom + 1);
+ unsigned col = (p.col() / 2 + 1) * (zoom + 1) - 1;
+ for (unsigned i = 0; i < zoom; ++i)
+ output.at(row + i, col) = ima(p);
+ }
+ return output;
+ }
+
+ template <typename I>
+ I display_edge(const I& ima, unsigned zoom)
+ {
+ unsigned nrows = ima.nrows() / 2 + 1;
+ unsigned ncols = ima.ncols() / 2 + 1;
+ I output(nrows * (zoom + 1) - 1,
+ ncols * (zoom + 1) - 1);
+
+ mln_VAR( cell, ima | is_cell );
+ mln_piter(cell_t) q(cell.domain());
+ for_all(q)
+ {
+ unsigned row = (q.row() / 2) * (zoom + 1);
+ unsigned col = (q.col() / 2) * (zoom + 1);
+ for (unsigned i = 0; i < zoom; ++i)
+ for (unsigned j = 0; j < zoom; ++j)
+ output.at(row + i, col + j) = ima(q);
+ }
+
+ mln_VAR( edge, ima | is_edge );
+ mln_piter(edge_t) p(edge.domain());
+ for_all(p)
+ if (p.row() % 2) // horizontal edge
+ {
+ unsigned row = (p.row() / 2 + 1) * (zoom + 1) - 1;
+ unsigned col = (p.col() / 2) * (zoom + 1);
+ for (unsigned i = 0; i < zoom; ++i)
+ output.at(row, col + i) = ima(p);
+ }
+ else // vertical edge
+ {
+ unsigned row = (p.row() / 2) * (zoom + 1);
+ unsigned col = (p.col() / 2 + 1) * (zoom + 1) - 1;
+ for (unsigned i = 0; i < zoom; ++i)
+ output.at(row + i, col) = ima(p);
+ }
+ return output;
+ }
+
+
+ namespace morpho
+ {
+
+ template <typename I, typename N>
+ mln_concrete(I)
+ dilation(const I& input, const N& nbh)
+ {
+ typedef mln_value(I) V;
+
+ mln_concrete(I) output;
+ initialize(output, input);
+
+ mln_piter(I) p(input.domain());
+ mln_niter(N) n(nbh, p);
+ for_all(p)
+ {
+ for_all(n)
+ if (input.has(n) && input(n) != value::rgb8(0,0,0))
+ output(p) = input(n);
+ }
+ return output;
+ }
+ } // mln::morpho
+
+} // mln
+
+
+
+template <typename T>
+mln::image2d<T>
+image2cells(const mln::image2d<T>& input)
+{
+ mln::image2d<T> output(2 * input.nrows() - 1,
+ 2 * input.ncols() - 1);
+ for (unsigned row = 0; row < input.nrows(); ++row)
+ for (unsigned col = 0; col < input.ncols(); ++col)
+ output.at(2 * row, 2 * col) = input.at(row, col);
+ return output;
+}
+
+
+template <typename T>
+mln::image2d<T>
+cells2image(const mln::image2d<T>& input)
+{
+ mln::image2d<T> output((input.nrows() + 1) / 2,
+ (input.ncols() + 1) / 2);
+ for (unsigned row = 0; row < input.nrows(); row += 2)
+ for (unsigned col = 0; col < input.ncols(); col += 2)
+ output.at(row / 2, col / 2) = input.at(row, col);
+ return output;
+}
+
+
+template <typename I, typename N, typename Ic, typename Nc>
+unsigned min_tree(const I& f, const N& nbh, const Ic& ref, const Nc& nbhc,
+ int lambda)
+{
+ using namespace mln;
+
+ min_tree_<I,N,Ic,Nc> run(f, nbh, ref, nbhc, lambda);
+
+
+ mln_piter(I) p(f.domain());
+ unsigned nnodes = 0;
+ for_all(p)
+ {
+ if (run.is_node(p))
+ ++nnodes;
+ }
+
+#if 1
+ colorize colors(nnodes);
+ image2d<value::rgb8> tmp(ref.domain());
+ level::fill(tmp, ref);
+
+
+ mln_piter(I) q(f.domain());
+ unsigned int i = 0;
+ for_all(q)
+ {
+ if (run.is_node(q))
+ {
+ tmp(q) = colors(i);
+ i++;
+ }
+ }
+ mln_piter(I) r(f.domain());
+ for_all(r)
+ {
+ if (!run.is_node(r))
+ tmp(r) = tmp(run.find_representative(r));
+ }
+
+ image2d<value::rgb8> to_display(tmp.domain());
+
+ level::fill(to_display, value::rgb8(255, 255, 255));
+ level::paste((tmp | is_edge), to_display);
+ level::paste(morpho::dilation(to_display, c4()), to_display);
+
+ io::ppm::save(display_edge(tmp, literal::white, 3),
+ "edge.ppm");
+ io::ppm::save(tmp, "full.ppm");
+ io::ppm::save(cells2image(to_display), "colorize.ppm");
+#endif
+
+#if 1
+ // io::ppm::save(display_edge(run.values, literal::white, 3), "tmp_tree_colored.pgm");
+#endif
+
+ return nnodes;
+}
+
+
+template <typename I>
+I
+do_it(I& input, int lambda, unsigned& nbasins)
+{
+ using namespace mln;
+
+ /// Graph creation
+ I graph;
+ create_graph(input, graph, value::rgb8(0, 0, 0));
+
+ // Initialization
+ image2d<value::int_u16> ima = convert_to_grey(graph);
+
+ // Neigbhorhood
+ // e2c
+ bool e2c_h[] = { 0, 1, 0,
+ 0, 0, 0,
+ 0, 1, 0 };
+ bool e2c_v[] = { 0, 0, 0,
+ 1, 0, 1,
+ 0, 0, 0 };
+
+ mln_VAR(e2c, make::double_neighb2d(is_row_odd, e2c_h, e2c_v));
+
+ bool e2e_h[] = { 0, 0, 1, 0, 0,
+ 0, 1, 0, 1, 0,
+ 0, 0, 0, 0, 0,
+ 0, 1, 0, 1, 0,
+ 0, 0, 1, 0, 0 };
+
+ bool e2e_v[] = { 0, 0, 0, 0, 0,
+ 0, 1, 0, 1, 0,
+ 1, 0, 0, 0, 1,
+ 0, 1, 0, 1, 0,
+ 0, 0, 0, 0, 0 };
+ mln_VAR(e2e, make::double_neighb2d(is_row_odd, e2e_h, e2e_v));
+
+ // Algorithm
+ distance(extend((graph | is_edge).rw(), pw::value(graph)), e2c, ima);
+
+ io::pgm::save(ima, "edge.pgm");
+
+ nbasins = min_tree((ima | is_edge), e2e, graph, e2c, lambda);
+
+ return graph;
+}
+
+void usage(char* argv[])
+{
+ std::cerr << "usage: " << argv[0] << " input.pgm lambda" << std::endl;
+ std::cerr << " lambda >= 0" << std::endl;
+ abort();
+}
+
+int main(int argc, char* argv[])
+{
+ using namespace mln;
+
+ if (argc != 3)
+ usage(argv);
+
+ int lambda = atoi(argv[2]);
+ if (lambda < 0)
+ usage(argv);
+
+ image2d<value::rgb8> ima;
+ io::ppm::load(ima, argv[1]);
+
+ unsigned nbasins;
+ image2d<value::rgb8> output = do_it(ima, lambda, nbasins);
+
+ //io::ppm::save(output, argv[3]);
+}
Index: ballas/color/reference2.cc
--- ballas/color/reference2.cc (revision 0)
+++ ballas/color/reference2.cc (revision 0)
@@ -0,0 +1,406 @@
+# include <mln/core/var.hh>
+
+# include <mln/core/image/image2d.hh>
+# include <mln/core/image/image_if.hh>
+# include <mln/core/image/extended.hh>
+# include <mln/core/routine/extend.hh>
+
+# include <mln/core/alias/window2d.hh>
+# include <mln/core/alias/neighb2d.hh>
+# include <mln/make/double_neighb2d.hh>
+# include <mln/core/site_set/p_centered.hh>
+
+# include <mln/literal/origin.hh>
+# include <mln/literal/black.hh>
+# include <mln/literal/white.hh>
+
+# include <mln/value/int_u8.hh>
+# include <mln/io/pgm/load.hh>
+# include <mln/io/pgm/save.hh>
+
+# include <mln/value/rgb8.hh>
+# include <mln/io/ppm/load.hh>
+# include <mln/io/ppm/save.hh>
+
+# include <mln/accu/min_max.hh>
+# include <mln/accu/mean.hh>
+
+# include <mln/fun/i2v/array.hh>
+# include <mln/fun/p2v/iota.hh>
+
+# include <mln/level/paste.hh>
+# include <mln/level/fill.hh>
+# include <mln/level/transform.hh>
+# include <mln/extension/fill.hh>
+
+# include <mln/morpho/meyer_wst.hh>
+# include <mln/morpho/closing_volume.hh>
+
+# include <mln/linear/convolve.hh>
+# include <mln/make/w_window2d.hh>
+
+# include <mln/debug/println.hh>
+
+namespace mln
+{
+ namespace morpho
+ {
+ template <typename I, typename N>
+ mln_concrete(I)
+ closing_volume(const I& input, const Neighborhood<N>& nbh, std::size_t lambda)
+ {
+ mln_concrete(I) output;
+ initialize(output, input);
+ closing_volume(input, nbh, lambda, output);
+ return output;
+ }
+ }
+}
+
+namespace mln
+{
+ template <typename I, typename O>
+ void
+ LoG_17x17(const I& input, const O& output)
+ {
+ mln_precondition(exact(output).domain() == exact(input).domain());
+ int ws[] = { +0, 0, 0, 0, 0, 0,-1,-1,-1,-1,-1, 0, 0, 0, 0, 0, 0,
+ +0, 0, 0, 0,-1,-1,-1,-1,-1,-1,-1,-1,-1, 0, 0, 0, 0,
+ +0, 0,-1,-1,-1,-2,-3,-3,-3,-3,-3,-2,-1,-1,-1, 0, 0,
+ +0, 0,-1,-1,-2,-3,-3,-3,-3,-3,-3,-3,-2,-1,-1, 0, 0,
+ +0,-1,-1,-2,-3,-3,-3,-2,-3,-2,-3,-3,-3,-2,-1,-1, 0,
+ +0,-1,-2,-3,-3,-3, 0, 2, 4, 2, 0,-3,-3,-3,-2,-1, 0,
+ -1,-1,-3,-3,-3, 0, 4,10,12,10, 4, 0,-3,-3,-3,-1,-1,
+ -1,-1,-3,-3,-2, 2,10,18,21,18,10, 2,-2,-3,-3,-1,-1,
+ -1,-1,-3,-3,-3, 4,12,21,24,21,12, 4,-3,-3,-3,-1,-1,
+ -1,-1,-3,-3,-2, 2,10,18,21,18,10, 2,-2,-3,-3,-1,-1,
+ -1,-1,-3,-3,-3, 0, 4,10,12,10, 4, 0,-3,-3,-3,-1,-1,
+ +0,-1,-2,-3,-3,-3, 0, 2, 4, 2, 0,-3,-3,-3,-2,-1, 0,
+ +0,-1,-1,-2,-3,-3,-3,-2,-3,-2,-3,-3,-3,-2,-1,-1, 0,
+ +0, 0,-1,-1,-2,-3,-3,-3,-3,-3,-3,-3,-2,-1,-1, 0, 0,
+ +0, 0,-1,-1,-1,-2,-3,-3,-3,-3,-3,-2,-1,-1,-1, 0, 0,
+ +0, 0, 0, 0,-1,-1,-1,-1,-1,-1,-1,-1,-1, 0, 0, 0, 0,
+ +0, 0, 0, 0, 0, 0,-1,-1,-1,-1,-1, 0, 0, 0, 0, 0, 0 };
+ linear::convolve(input, make::w_window2d(ws), output);
+ }
+} // !mln
+
+namespace mln
+{
+ image2d<value::int_u8> convert_to_grey(const image2d<value::rgb8>& data)
+ {
+ image2d<value::int_u8> output(data.domain());
+ mln_piter_(image2d<value::int_u8>) p(output.domain());
+ for_all(p)
+ output(p) = (int) (data(p).red() * 0.3 + data(p).green() * 0.58 + data(p).blue()) * 0.12;
+ return output;
+ }
+} // !mln
+
+// Functions
+
+inline
+bool is_row_odd(const mln::point2d& p)
+{
+ return p.row() % 2;
+}
+
+inline
+bool is_cell(const mln::point2d& p)
+{
+ return p.row() % 2 == 0 && p.col() % 2 == 0;
+}
+
+inline
+bool is_edge(const mln::point2d& p)
+{
+ return p.row() % 2 + p.col() % 2 == 1;
+}
+
+inline
+bool is_point(const mln::point2d& p)
+{
+ return p.row() % 2 && p.col() % 2;
+}
+
+inline
+bool is_not_edge(const mln::point2d& p)
+{
+ return ! is_edge(p);
+}
+
+
+
+namespace mln
+{
+
+ namespace border
+ {
+
+ template <typename I>
+ void
+ fill(I& ima, const mln_value(I)& v)
+ {
+ const int nrows = ima.nrows();
+ const int ncols = ima.ncols();
+ for (int r = -1; r <= nrows; ++r)
+ {
+ ima.at(r, -1) = v;
+ ima.at(r, ncols) = v;
+ }
+ for (int c = -1; c <= ncols; ++c)
+ {
+ ima.at(-1, c) = v;
+ ima.at(nrows, c) = v;
+ }
+ }
+
+ } // mln::border
+
+ namespace accu
+ {
+
+ template <typename I, typename L, typename A, typename V>
+ inline
+ void
+ compute(const Image<I>& input_,
+ const Image<L>& label_,
+ const Accumulator<A>&,
+ V& v)
+ {
+ trace::entering("accu::compute");
+
+ const I& input = exact(input_);
+ const L& label = exact(label_);
+
+ const unsigned n = v.size();
+ std::vector<A> a(n);
+
+ mln_piter(I) p(input.domain());
+ for_all(p)
+ a[label(p)].take(input(p));
+
+ for (unsigned l = 1; l < n; ++l)
+ v(l) = a[l].to_result();
+
+ trace::exiting("accu::compute");
+ }
+
+ } // mln::accu
+
+ namespace morpho
+ {
+
+ template <typename I, typename N>
+ mln_concrete(I)
+ gradient(const I& input, const N& nbh)
+ {
+ mln_concrete(I) output;
+ initialize(output, input);
+ accu::min_max<mln_value(I)> mm;
+
+ mln_piter(I) p(input.domain());
+ mln_niter(N) n(nbh, p);
+ for_all(p)
+ {
+ mm.init();
+ for_all(n) if (input.has(n))
+ mm.take(input(n));
+ output(p) = mm.second() - mm.first();
+ }
+ return output;
+ }
+
+ template <typename I, typename N>
+ mln_concrete(I)
+ dilation(const I& input, const N& nbh)
+ {
+ typedef mln_value(I) V;
+
+ mln_concrete(I) output;
+ initialize(output, input);
+
+ mln_piter(I) p(input.domain());
+ mln_niter(N) n(nbh, p);
+ for_all(p)
+ {
+ for_all(n)
+ if (input.has(n) && input(n) != value::rgb8(0,0,0))
+ output(p) = input(n);
+ }
+ return output;
+ }
+ } // mln::morpho
+
+
+ struct colorize : Function_v2v< colorize >
+ {
+ typedef value::rgb8 result;
+ colorize(unsigned max)
+ : lut(max + 1)
+ {
+ lut[0] = literal::black;
+ for (unsigned i = 1; i <= max; ++i)
+ lut[i] = result(100 + std::rand() % 150,
+ 100 + std::rand() % 150,
+ 100 + std::rand() % 150);
+ }
+ result operator()(unsigned i) const
+ {
+ return lut[i];
+ }
+ std::vector<result> lut;
+ };
+
+
+ template <typename I>
+ I display_edge(const I& ima, mln_value(I) bg, unsigned zoom)
+ {
+ unsigned nrows = ima.nrows() / 2 + 1;
+ unsigned ncols = ima.ncols() / 2 + 1;
+ I output(nrows * (zoom + 1) - 1,
+ ncols * (zoom + 1) - 1);
+ level::fill(output, bg);
+ mln_VAR( edge, ima | is_edge );
+ mln_piter(edge_t) p(edge.domain());
+ for_all(p)
+ if (p.row() % 2) // horizontal edge
+ {
+ unsigned row = (p.row() / 2 + 1) * (zoom + 1) - 1;
+ unsigned col = (p.col() / 2) * (zoom + 1);
+ for (unsigned i = 0; i < zoom; ++i)
+ output.at(row, col + i) = ima(p);
+ }
+ else // vertical edge
+ {
+ unsigned row = (p.row() / 2) * (zoom + 1);
+ unsigned col = (p.col() / 2 + 1) * (zoom + 1) - 1;
+ for (unsigned i = 0; i < zoom; ++i)
+ output.at(row + i, col) = ima(p);
+ }
+ return output;
+ }
+
+} // mln
+
+
+
+template <typename T>
+mln::image2d<T>
+image2cells(const mln::image2d<T>& input)
+{
+ mln::image2d<T> output(2 * input.nrows() - 1,
+ 2 * input.ncols() - 1);
+ for (int row = 0; row < input.nrows(); ++row)
+ for (int col = 0; col < input.ncols(); ++col)
+ output.at(2 * row, 2 * col) = input.at(row, col);
+ return output;
+}
+
+
+template <typename T>
+mln::image2d<T>
+cells2image(const mln::image2d<T>& input)
+{
+ mln::image2d<T> output((input.nrows() + 1) / 2,
+ (input.ncols() + 1) / 2);
+ for (int row = 0; row < input.nrows(); row += 2)
+ for (int col = 0; col < input.ncols(); col += 2)
+ output.at(row / 2, col / 2) = input.at(row, col);
+ return output;
+}
+
+
+
+
+template <typename I>
+mln_concrete(I)
+do_it(I& input, int lambda, unsigned& nbasins)
+{
+ using namespace mln;
+
+ /******************/
+ /* Initialisation */
+ /******************/
+
+ image2d<value::int_u8> ima = image2cells(convert_to_grey(input));
+
+ /**************************/
+ /* Neighborhood defintion */
+ /**************************/
+
+ // e2c
+ bool e2c_h[] = { 0, 1, 0,
+ 0, 0, 0,
+ 0, 1, 0 };
+ bool e2c_v[] = { 0, 0, 0,
+ 1, 0, 1,
+ 0, 0, 0 };
+ mln_VAR( e2c, make::double_neighb2d(is_row_odd, e2c_h, e2c_v) );
+
+ // e2e
+ bool e2e_h[] = { 0, 0, 1, 0, 0,
+ 0, 1, 0, 1, 0,
+ 0, 0, 0, 0, 0,
+ 0, 1, 0, 1, 0,
+ 0, 0, 1, 0, 0 };
+ bool e2e_v[] = { 0, 0, 0, 0, 0,
+ 0, 1, 0, 1, 0,
+ 1, 0, 0, 0, 1,
+ 0, 1, 0, 1, 0,
+ 0, 0, 0, 0, 0 };
+ mln_VAR( e2e, make::double_neighb2d(is_row_odd, e2e_h, e2e_v) );
+
+ // cell
+ mln_VAR(cell, ima | is_cell);
+
+ // edge
+ mln_VAR(edge, extend((ima | is_edge).rw(), pw::value(ima)));
+
+ // FIXME until laplacian is working use gradient / closing_area / wst
+
+ level::paste(morpho::gradient(edge, e2c), edge);
+ level::paste(morpho::closing_volume(edge, e2e, lambda), edge);
+ level::fill(edge, morpho::meyer_wst(edge, e2e, nbasins));
+
+ // Fill regions (with colorize) (won't work with laplacian...)
+
+ colorize colors(nbasins);
+
+ image2d<value::rgb8> cells(ima.domain());
+ level::fill(cells, literal::white);
+ level::paste(level::transform(edge, colors), cells);
+ io::ppm::save(display_edge(cells, literal::white, 3), "tmp_edge.ppm");
+
+ // Move the color of an edge which is non black in the cell
+ level::paste(morpho::dilation(cells, c4()), cells);
+
+ return cells2image(cells);
+}
+
+void usage(char* argv[])
+{
+ std::cerr << "usage: " << argv[0] << " input.pgm lambda output.ppm" << std::endl;
+ std::cerr << " lambda >= 0" << std::endl;
+ abort();
+}
+
+int main(int argc, char* argv[])
+{
+ using namespace mln;
+
+ if (argc != 4)
+ usage(argv);
+
+ int lambda = atoi(argv[2]);
+ if (lambda < 0)
+ usage(argv);
+
+ image2d<value::rgb8> ima;
+ io::ppm::load(ima, argv[1]);
+
+ unsigned nbasins;
+ image2d<value::rgb8> output = do_it(ima, lambda, nbasins);
+
+ io::ppm::save(output, argv[3]);
+}
Index: ballas/color/src/graph.hh
--- ballas/color/src/graph.hh (revision 0)
+++ ballas/color/src/graph.hh (revision 0)
@@ -0,0 +1,61 @@
+/*! \file src/graph.hh
+ *
+ */
+
+#ifndef SRC_GRAPH_HH
+# define SRC_GRAPH_HH
+
+# include <mln/value/int_u8.hh>
+# include <mln/value/int_u16.hh>
+# include <mln/value/rgb8.hh>
+
+# include <mln/level/fill.hh>
+
+# include <mln/core/image/image2d.hh>
+
+
+// Neighborhood functions
+inline
+bool is_row_odd(const mln::point2d& p)
+{
+ return p.row() % 2;
+}
+inline
+bool is_cell(const mln::point2d& p)
+{
+ return p.row() % 2 == 0 && p.col() % 2 == 0;
+}
+inline
+bool is_edge(const mln::point2d& p)
+{
+ return (p.row() % 2 + p.col() % 2) == 1;
+}
+inline
+bool is_point(const mln::point2d& p)
+{
+ return p.row() % 2 && p.col() % 2;
+}
+inline
+bool is_not_edge(const mln::point2d& p)
+{
+ return ! is_edge(p);
+}
+
+// Graph image creation function
+// FIXME: add exact conversion....
+// FIXME: check that the input image is in 2 dimension
+template <typename I>
+void
+create_graph(const I& ima, I& graph, mln_value(I) val)
+{
+ graph = I(ima.nrows() * 2 -1, ima.ncols() * 2 - 1);
+
+ mln::level::fill(graph, val);
+
+ mln_piter(I) p(ima.domain());
+ for_all(p)
+ graph.at(p.row() * 2, p.col() * 2) = ima(p);
+}
+
+
+#endif // !SRC_GRAPH_HH
Index: ballas/color/src/io.hh
--- ballas/color/src/io.hh (revision 0)
+++ ballas/color/src/io.hh (revision 0)
@@ -0,0 +1,57 @@
+/*! \file src/io.hh
+ *
+ * Contains various method to load/save an image
+ */
+
+#ifndef SRC_IO_HH
+# define SRC_IO_HH
+
+#include <mln/core/image/image2d.hh>
+
+#include <mln/value/int_u16.hh>
+#include <mln/value/int_s16.hh>
+
+# include <mln/io/pgm/load.hh>
+# include <mln/io/pgm/save.hh>
+# include <mln/io/ppm/load.hh>
+# include <mln/io/ppm/save.hh>
+
+namespace IO
+{
+
+ template <typename I>
+ void load(I& ima, const std::string& file)
+ {
+ mln::io::ppm::load(ima, file);
+ }
+
+ template <>
+ void load(mln::image2d<mln::value::int_u16>& ima,
+ const std::string& file)
+ {
+ mln::io::pgm::load(ima, file);
+ }
+
+ template <typename I>
+ void save(I& ima, const std::string& file)
+ {
+ mln::io::ppm::save(ima, file);
+ }
+
+ template <>
+ void save(mln::image2d<mln::value::int_u16>& ima,
+ const std::string& file)
+ {
+ mln::io::pgm::save(ima, file);
+ }
+
+ template <>
+ void save(mln::image2d<mln::value::int_s16>& ima,
+ const std::string& file)
+ {
+ mln::io::pgm::save(ima, file);
+ }
+
+} // !IO
+
+#endif // !SRC_IO_HH
Index: ballas/color/src/distance.hh
--- ballas/color/src/distance.hh (revision 0)
+++ ballas/color/src/distance.hh (revision 0)
@@ -0,0 +1,52 @@
+/*! \file src/distance.hh
+ *
+ */
+
+#ifndef SRC_DISTANCE_HH
+# define SRC_DISTANCE_HH
+
+# include "graph.hh"
+
+# include <cmath>
+
+
+/// Manhatan distance
+inline
+unsigned distance(const mln::value::rgb8& lhs,
+ const mln::value::rgb8& rhs)
+{
+ return abs(lhs.red() - rhs.red()) +
+ abs(lhs.green() - rhs.green()) +
+ abs(lhs.blue() - rhs.blue());
+}
+
+
+/// Store the distance between two points on edge
+/// FIXME documentation
+template <typename I, typename N, typename O>
+void distance(const I& rgb_graph,
+ const N& nbh,
+ O& gl_graph)
+{
+ mln_piter(I) p(rgb_graph.domain());
+ mln_niter(N) n(nbh, p);
+
+ for_all(p)
+ {
+ mln::value::rgb8 v1;
+ mln::value::rgb8 v2;
+
+ n.start();
+ assert(n.is_valid() && rgb_graph.has(n));
+ v1 = rgb_graph(n);
+ n.next();
+ assert(n.is_valid() && rgb_graph.has(n));
+ v2 = rgb_graph(n);
+
+ gl_graph(p) = distance(v1, v2);
+ }
+}
+
+
+
+#endif // !SRC_DISTANCE_HH
Index: ballas/color/src/convert.hh
--- ballas/color/src/convert.hh (revision 0)
+++ ballas/color/src/convert.hh (revision 0)
@@ -0,0 +1,37 @@
+/*! \file src/convert.hh
+ *
+ * Method that convert an rgb image 2d into gray level
+ */
+
+#ifndef SRC_CONVERT_HH
+# define SRC_CONVERT_HH
+
+# include <mln/value/int_u8.hh>
+# include <mln/value/int_u16.hh>
+# include <mln/value/int_s16.hh>
+# include <mln/value/rgb8.hh>
+
+# include <mln/core/image/image2d.hh>
+
+// Convert function
+void convert_to_gl(mln::image2d<mln::value::int_u16>& ima,
+ const mln::image2d<mln::value::rgb8>& data)
+{
+ mln_piter_(mln::image2d<mln::value::int_u16>) p(ima.domain());
+ for_all(p)
+ ima(p) = (int) (data(p).red() * 0.3 +
+ data(p).green() * 0.58 +
+ data(p).blue() * 0.12);
+}
+
+void convert_to_gl(mln::image2d<mln::value::int_s16>& ima,
+ const mln::image2d<mln::value::rgb8>& data)
+{
+ mln_piter_(mln::image2d<mln::value::int_s16>) p(ima.domain());
+ for_all(p)
+ ima(p) = (int) (data(p).red() * 0.3 +
+ data(p).green() * 0.58 +
+ data(p).blue() * 0.12);
+}
+
+#endif // !SRC_CONVERT_HH
Index: ballas/color/laplacien.cc
--- ballas/color/laplacien.cc (revision 0)
+++ ballas/color/laplacien.cc (revision 0)
@@ -0,0 +1,129 @@
+#include <mln/core/image/image2d.hh>
+#include <mln/core/image/image_if.hh>
+
+# include <mln/core/alias/neighb2d.hh>
+# include <mln/make/double_neighb2d.hh>
+
+#include <mln/value/rgb8.hh>
+#include <mln/value/int_u16.hh>
+#include <mln/value/int_s16.hh>
+
+#include <mln/linear/gaussian.hh>
+#include <mln/morpho/erosion.hh>
+
+#include <mln/core/var.hh>
+#include <mln/debug/println.hh>
+
+#include "src/io.hh"
+#include "src/graph.hh"
+#include "src/convert.hh"
+
+
+void usage(char* argv[])
+{
+ std::cerr << "usage: " << argv[0] << " input.ppm output.pgm" << std::endl;
+ abort();
+}
+
+/// FIXME Put these elsewhere
+typedef mln::image2d<mln::value::rgb8> Icolor;
+typedef mln::image2d<mln::value::int_u16> Igray;
+
+void process(Icolor& graph, const Icolor& input)
+{
+ using namespace mln;
+
+ // Neighborhood definition
+
+// bool e2c_h[] = { 0, 1, 0,
+// 0, 0, 0,
+// 0, 1, 0 };
+// bool e2c_v[] = { 0, 0, 0,
+// 1, 0, 1,
+// 0, 0, 0 };
+
+// bool e2e_h[] = { 0, 0, 1, 0, 0,
+// 0, 1, 0, 1, 0,
+// 0, 0, 0, 0, 0,
+// 0, 1, 0, 1, 0,
+// 0, 0, 1, 0, 0 };
+// bool e2e_v[] = { 0, 0, 0, 0, 0,
+// 0, 1, 0, 1, 0,
+// 1, 0, 0, 0, 1,
+// 0, 1, 0, 1, 0,
+// 0, 0, 0, 0, 0 };
+
+// bool e2p_h[] = { 0, 0, 1,
+// 0, 0, 0,
+// 1, 0, 0 };
+// bool e2p_v[] = { 1, 0, 0,
+// 0, 0, 0,
+// 0, 0, 1 };
+ //mln_VAR(e2c, make::double_neighb2d(is_row_odd, e2c_h, e2c_v));
+
+
+ // convert the image into grey level
+ Igray gray_graph(graph.bbox());
+ convert_to_gl(gray_graph, graph);
+
+ Igray gray_input(input.bbox());
+ convert_to_gl(gray_input, input);
+
+
+ //level::paste(morpho::dilation((gray_graph | is_cell), e2c.win()), gray_graph);
+// Igray save(gray_graph.bbox());
+// level::paste(gray_graph | is_cell, save);
+// IO::save(save, "tmp3.ppm");
+
+ // Create the laplacian image
+ //Igray laplacian(gray_input.bbox());
+ image2d<int> laplacian(gray_input.bbox());
+ linear::gaussian(gray_input, 2.0f, laplacian);
+ //IO::save(laplacian, "tmp1.ppm");
+ linear::laplacian(gray_input, 2.0f, laplacian);
+ debug::println(laplacian);
+ //IO::save(laplacian, "tmp2.ppm");
+
+ // Display edge on the output images
+// debug::println(laplacian);
+
+// mln_piter_(Igray) p(laplacian.domain());
+// for_all(p)
+// if (laplacian(p) == 0u)
+// {
+// std::cout << "I'm here" << std::endl;
+// graph.at(p.row() * 2, p.col() * 2) = value::rgb<8>(255, 0, 0);
+// }
+
+}
+
+
+
+int main(int argc, char* argv[])
+{
+ using namespace mln;
+
+ // Initialisation
+ if (argc != 3)
+ usage(argv);
+
+ std::string input_file(argv[1]);
+ std::string output_file(argv[2]);
+
+
+ Icolor input;
+
+ // Load the image
+ IO::load(input, input_file);
+
+ // create a graph image from the input
+ Icolor graph;
+ create_graph(input, graph);
+
+ // Process
+ process(graph, input);
+
+
+ // Save
+ IO::save(graph, output_file);
+}
1
0
From: Maxime van Noppen <yabo(a)lrde.epita.fr>
To: olena-patches(a)lrde.epita.fr
Subject: milena r2770: Fix an include problem in mln/morpho
URL: https://svn.lrde.epita.fr/svn/oln/branches/cleanup-2008/milena
ChangeLog:
2008-11-03 Maxime van Noppen <yabo(a)lrde.epita.fr>
Fix an include problem in mln/morpho.
* includes.hh: As erosion is used in dilation it must be included
* before. Same for complementation.
---
includes.hh | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
Index: branches/cleanup-2008/milena/mln/morpho/includes.hh
===================================================================
--- branches/cleanup-2008/milena/mln/morpho/includes.hh (revision 2769)
+++ branches/cleanup-2008/milena/mln/morpho/includes.hh (revision 2770)
@@ -66,11 +66,11 @@
# include <mln/win/diff.hh>
# include <mln/win/inter.hh>
-# include <mln/morpho/dilation.hh>
# include <mln/morpho/erosion.hh>
+# include <mln/morpho/complementation.hh>
+# include <mln/morpho/dilation.hh>
# include <mln/morpho/min.hh>
-# include <mln/morpho/complementation.hh>
# include <mln/morpho/minus.hh>
# include <mln/morpho/plus.hh>
1
0
03 Nov '08
https://svn.lrde.epita.fr/svn/oln/branches/cleanup-2008/milena
Index: ChangeLog
from Thierry Geraud <thierry.geraud(a)lrde.epita.fr>
Hack morphological dilation so it is practicable.
* tests/morpho/dilation.cc: Fix copyright.
(neighb/image.hh): Fix missing guards for this include.
* mln/morpho/dilation.hh (todo): New.
Fix copyright.
(impl): Remove all implementations; they were obsolete.
(dilation_as_erosion_dual): New implementation.
(dilation_tests, dilation_dispatch): New.
mln/morpho/dilation.hh | 251 ++++++-----------------------------------------
tests/morpho/dilation.cc | 9 +
2 files changed, 45 insertions(+), 215 deletions(-)
Index: tests/morpho/dilation.cc
--- tests/morpho/dilation.cc (revision 2768)
+++ tests/morpho/dilation.cc (working copy)
@@ -1,4 +1,5 @@
-// Copyright (C) 2007, 2008 EPITA Research and Development Laboratory (LRDE)
+// Copyright (C) 2007, 2008 EPITA Research and Development Laboratory
+// (LRDE)
//
// This file is part of the Olena Library. This library is free
// software; you can redistribute it and/or modify it under the terms
@@ -49,7 +50,13 @@
#include <mln/fun/ops.hh>
#include <mln/core/alias/neighb2d.hh>
+
+ /* FIXME: Re-enable these tests for Olena 1.1, when associated
+ neighborhoods (i.e., the neighb::image morpher) are supported and
+ shipped. */
+#if 0
#include <mln/neighb/image.hh>
+#endif
#include "tests/data.hh"
Index: mln/morpho/dilation.hh
--- mln/morpho/dilation.hh (revision 2768)
+++ mln/morpho/dilation.hh (working copy)
@@ -1,4 +1,5 @@
-// Copyright (C) 2007, 2008 EPITA Research and Development Laboratory (LRDE)
+// Copyright (C) 2007, 2008 EPITA Research and Development Laboratory
+// (LRDE)
//
// This file is part of the Olena Library. This library is free
// software; you can redistribute it and/or modify it under the terms
@@ -29,9 +30,10 @@
# define MLN_MORPHO_DILATION_HH
/// \file mln/morpho/dilation.hh
-/// \brief Morphological dilation.
///
-/// \todo Activate the FIXMEs (border::adjust).
+/// Morphological dilation.
+///
+/// \todo Revamp.
# include <mln/morpho/includes.hh>
@@ -42,28 +44,9 @@
namespace morpho
{
- /// Morphological dilation using the neighborhood bound to an image.
- ///
- /// \{
- /* FIXME: Re-enable this routine for Olena 1.1, when associated
- neighborhoods (i.e., the neighb::image morpher) are supported
- and shipped. */
-#if 0
- /// Perform a morphological dilation of \a input using its
- /// neighborhood and return the result.
- ///
- /// \pre \a input must be an image with a neighborhood.
- template <typename I>
- mln_concrete(I)
- dilation(const Image<I>& input);
- /// \}
-#endif
/// Morphological dilation using windows.
///
- /// \todo Overload dilation_wrt_win for hline and vline and for fast
- /// images.
- ///
/// \{
/// Perform a morphological dilation of \a input using \a win and
/// return the result.
@@ -74,223 +57,68 @@
-# ifndef MLN_INCLUDE_ONLY
-
- namespace impl
- {
-
- /*---------------.
- | Window-based. |
- `---------------*/
-
- // On function.
-
- template <typename I, typename W, typename O>
- inline
- void dilation_on_function(const Image<I>& input_, const Window<W>& win_,
- Image<O>& output_)
- {
- const I& input = exact(input_);
- const W& win = exact(win_);
- O& output = exact(output_);
- // FIXME: border::adjust(input, win.delta());
- extension::fill(input, mln_min(mln_value(I)));
+# ifndef MLN_INCLUDE_ONLY
- accu::max<mln_value(I)> max;
+ // Tests.
- mln_piter(I) p(input.domain());
- mln_qiter(W) q(win, p);
- for_all(p)
+ namespace internal
{
- max.init();
- for_all(q) if (input.has(q))
- max.take(input(q));
- output(p) = max.to_result();
- }
- }
- // On set.
-
- template <typename I, typename W, typename O>
- inline
- void dilation_on_set(const Image<I>& input_, const Window<W>& win_,
- Image<O>& output_)
+ template <typename I, typename W>
+ void
+ dilation_tests(const Image<I>& input_, const Window<W>& win_)
{
const I& input = exact(input_);
const W& win = exact(win_);
- O& output = exact(output_);
-
- // FIXME: border::adjust(input, win.delta());
- extension::fill(input, false);
-
- level::fill(output, input);
-
- mln_piter(I) p(input.domain());
- mln_qiter(W) q(win, p);
- for_all(p)
- if (!input(p))
- for_all(q) if (input.has(q))
- if (input(q))
- {
- output(p) = true;
- break;
+ mln_precondition(input.has_data());
+ // mln_precondition(win.is_valid());
+ (void) input;
+ (void) win;
}
- }
-
- // ------------- //
- // Dispatchers. //
- // ------------- //
+ } // end of namespace mln::morpho::internal
- // FIXME: Stage 3: dispatch w.r.t. speed property.
- // ...
+ // Implementations.
- /// Stage 2: dispatch w.r.t. the value kind.
- /// \{
-
- /// Binary => morphology on sets.
- template <typename I, typename W, typename O>
- inline
- void dilation_wrt_value(trait::image::kind::logic,
- const Image<I>& input, const Window<W>& win,
- Image<O>& output)
+ namespace impl
{
- return impl::dilation_on_set(exact(input), exact(win), output);
- }
- /// Otherwise => morphology on functions.
- template <typename I, typename W, typename O>
- inline
- void dilation_wrt_value(trait::image::kind::any,
- const Image<I>& input, const Window<W>& win,
- Image<O>& output)
+ template <typename I, typename W>
+ mln_concrete(I)
+ dilation_as_erosion_dual(const Image<I>& input, const Window<W>& win)
{
- return impl::dilation_on_function(exact(input), exact(win), output);
- }
-
- // End of stage 2.
-
- /// \}
+ trace::entering("morpho::impl::dilation_as_erosion_dual");
+ mln_concrete(I) output = morpho::erosion(morpho::complementation(input),
+ win);
+ morpho::complementation_inplace(output);
-
- /// Stage 1: dispatch w.r.t. the window type.
- /// \{
-
- /// Default case.
- template <typename I, typename W, typename O>
- inline
- void dilation_wrt_win(const Image<I>& input, const Window<W>& win,
- Image<O>& output)
- {
- // Perform stage 2: dispatch w.r.t. the value kind.
- dilation_wrt_value(mln_trait_image_kind(I)(), exact(input),
- exact(win), output);
+ trace::exiting("morpho::impl::dilation_as_erosion_dual");
+ return output;
}
-# ifdef MLN_CORE_WIN_RECTANGLE2D_HH
-
- /// Rectangle window.
- template <typename I, typename O>
- inline
- void dilation_wrt_win(const Image<I>& input, const win::rectangle2d& win,
- Image<O>& output)
- {
- O temp(exact(output).domain());
- morpho::dilation(input, win::hline2d(win.width()), temp);
- morpho::dilation(temp, win::vline2d(win.height()), output);
- }
+ } // end of namespace mln::morpho::impl
-# endif // MLN_CORE_WIN_RECTANGLE2D_HH
-# ifdef MLN_CORE_WIN_OCTAGON2D_HH
-# ifdef MLN_CORE_WIN_DIAG2D_HH
-# ifdef MLN_CORE_WIN_BACKDIAG2D_HH
+ // Dispatch.
- /// Octagon window.
- template <typename I, typename O>
- void dilation_wrt_win(const Image<I>& input, const win::octagon2d& win,
- Image<O>& output)
+ namespace internal
{
- const unsigned len = win.length() / 3 + 1;
-
- O temp1(exact(output).domain());
- O temp2(exact(output).domain());
- morpho::dilation(input, win::hline2d(len), temp1);
- morpho::dilation(temp1, win::vline2d(len), temp2);
- morpho::dilation(temp2, win::diag2d(len), temp1);
- morpho::dilation(temp1, win::backdiag2d(len), output);
- }
-
-# endif // MLN_CORE_WIN_BACKDIAG2D_HH
-# endif // MLN_CORE_WIN_DIAG2D_HH
-# endif // MLN_CORE_WIN_OCTAGON2D_HH
- // End of stage 1.
-
- /// \}
-
-
- /*---------------------.
- | Neighborhood-based. |
- `---------------------*/
-
- // FIXME: Disabled (see above).
-#if 0
- /* FIXME: We might want to move this function into the body of
- the facade (see at the bottom of the file. */
- // Sole case. Convert the neighborhood into a window, and
- // delegate to the window-based implementation.
- template <typename I, typename N, typename O>
- inline
- void dilation_wrt_nbh(const Image<I>& input, const Neighborhood<N>& nbh,
- Image<O>& output)
+ template <typename I, typename W>
+ mln_concrete(I)
+ dilation_dispatch(const Image<I>& input, const Window<W>& win)
{
- /* FIXME: The following comment applies to every algorithm
- having a neighborhood and a window flavor: move it
- elsewhere.
-
- We solely depend on the neighborhood-to-window conversion
- here. This means the conversion should be smart enough to
- produce a working window, even in the case of a non
- dpoint-set-based neighborhood. */
- dilation_wrt_win(input, nbh.win(), output);
+ return impl::dilation_as_erosion_dual(input, win);
}
-#endif
- } // end of namespace mln::morpho::impl
+ } // end of namespace mln::morpho::internal
- /*----------.
- | Facades. |
- `----------*/
-
- // ----------------------------------------------- //
- // Facade for neighborhood-based implementations. //
- // ----------------------------------------------- //
-
- // FIXME: Disabled (see above).
-#if 0
- template <typename I>
- mln_concrete(I)
- dilation(const Image<I>& input)
- {
- trace::entering("morpho::dilation");
-
- metal::has_neighborhood<I>::check();
- typedef mln_neighb(I) neighb;
-
- mln_concrete(I) output;
- initialize(output, input);
- impl::dilation_wrt_nbh(input, exact(input).neighborhood(), output);
- trace::exiting("morpho::dilation");
- return output;
- }
-#endif
// ----------------------------------------- //
// Facade for window-based implementations. //
@@ -301,15 +129,10 @@
dilation(const Image<I>& input, const Window<W>& win)
{
trace::entering("morpho::dilation");
- mln_precondition(exact(input).has_data());
- mln_precondition(! exact(win).is_empty());
- mln_concrete(I) output;
- initialize(output, input);
- impl::dilation_wrt_win(input, exact(win), output);
+ internal::dilation_tests(input, win);
+ mln_concrete(I) output = internal::dilation_dispatch(input, win);
- if (exact(win).is_centered())
- mln_postcondition(output >= input);
trace::exiting("morpho::dilation");
return output;
}
1
0
cleanup-2008 2768: Fix many little things in weighted window related stuff.
by Thierry Geraud 03 Nov '08
by Thierry Geraud 03 Nov '08
03 Nov '08
https://svn.lrde.epita.fr/svn/oln/branches/cleanup-2008/milena
Index: ChangeLog
from Thierry Geraud <thierry.geraud(a)lrde.epita.fr>
Fix many little things in weighted window related stuff.
* tests/core/alias/w_window1d_int.cc: Fix doc.
* tests/core/alias/window1d.cc: Likewise.
Remove echo on std::cout.
* tests/core/alias/w_window3d_int.cc: Likewise.
* tests/core/alias/window3d.cc: Likewise.
* tests/core/alias/w_window2d_int.cc: Fix doc.
* tests/core/alias/window2d.cc: Remove useless include.
* mln/trait/image_from_grid.hh: Update doc style.
* mln/core/concept/weighted_window.hh: Likewise.
(sym): Fix sig in doc.
(W): Rename this parameter as...
(E): ...this to remain consistent with othe concept files.
(is_empty, is_centered, delta): Move to...
* mln/core/internal/weighted_window_base.hh: ...this new file.
* mln/core/concept/object.hh: Include some more metal stuff.
* mln/core/concept/window.hh
(level::fill): Replace by plain code to supress dep.
(internal::bbox_): Move this hack into...
* mln/core/internal/geom_bbox.hh: ...this new file.
Update include.
* mln/core/w_window.hh (w_window): Update inheritance.
* mln/core/internal/window_base.hh: Fix copyright.
* mln/make/w_window2d.hh: Fix missing include.
* mln/convert/to_image.hh: Likewise.
(to_image): Fix sig in the Weighted_Window overload.
mln/convert/to_image.hh | 11 +
mln/core/concept/object.hh | 2
mln/core/concept/weighted_window.hh | 63 ++++------
mln/core/concept/window.hh | 39 +-----
mln/core/internal/geom_bbox.hh | 75 ++++++++++++
mln/core/internal/weighted_window_base.hh | 178 ++++++++++++++++++++++++++++++
mln/core/internal/window_base.hh | 2
mln/core/w_window.hh | 57 ++-------
mln/make/w_window2d.hh | 1
mln/trait/image_from_grid.hh | 12 +-
tests/core/alias/w_window1d_int.cc | 5
tests/core/alias/w_window2d_int.cc | 8 -
tests/core/alias/w_window3d_int.cc | 15 --
tests/core/alias/window1d.cc | 9 -
tests/core/alias/window2d.cc | 1
tests/core/alias/window3d.cc | 14 --
16 files changed, 339 insertions(+), 153 deletions(-)
Index: tests/core/alias/w_window1d_int.cc
--- tests/core/alias/w_window1d_int.cc (revision 2767)
+++ tests/core/alias/w_window1d_int.cc (working copy)
@@ -1,4 +1,5 @@
-// Copyright (C) 2007, 2008 EPITA Research and Development Laboratory (LRDE)
+// Copyright (C) 2007, 2008 EPITA Research and Development Laboratory
+// (LRDE)
//
// This file is part of the Olena Library. This library is free
// software; you can redistribute it and/or modify it under the terms
@@ -26,7 +27,7 @@
// Public License.
/// \file tests/core/alias/w_window1d_int.cc
-/// \brief Tests on mln::w_window1d_int.
+/// Tests on mln::w_window1d_int.
#include <mln/core/alias/w_window1d_int.hh>
#include <mln/win/segment1d.hh>
Index: tests/core/alias/window1d.cc
--- tests/core/alias/window1d.cc (revision 2767)
+++ tests/core/alias/window1d.cc (working copy)
@@ -1,4 +1,5 @@
-// Copyright (C) 2007 EPITA Research and Development Laboratory
+// Copyright (C) 2007, 2008 EPITA Research and Development Laboratory
+// (LRDE)
//
// This file is part of the Olena Library. This library is free
// software; you can redistribute it and/or modify it under the terms
@@ -31,9 +32,7 @@
*/
#include <mln/core/alias/window1d.hh>
-#include <mln/core/image/image1d.hh>
#include <mln/convert/to_image.hh>
-#include <mln/debug/println.hh>
@@ -46,14 +45,12 @@
mln_assertion(w.is_centered() == false);
mln_assertion(w.is_symmetric() == true);
- // FIXME: Does not compile as expected.
+ // As expected, the code below does not compile:
// w.insert(-1,0);
w.insert(-1);
w.insert( 1);
image1d<bool> ima = convert::to_image(w);
- debug::println(ima);
-
mln_assertion(w.delta() == 1);
}
Index: tests/core/alias/w_window2d_int.cc
--- tests/core/alias/w_window2d_int.cc (revision 2767)
+++ tests/core/alias/w_window2d_int.cc (working copy)
@@ -1,4 +1,5 @@
// Copyright (C) 2007, 2008 EPITA Research and Development Laboratory
+// (LRDE)
//
// This file is part of the Olena Library. This library is free
// software; you can redistribute it and/or modify it under the terms
@@ -25,10 +26,9 @@
// reasons why the executable file might be covered by the GNU General
// Public License.
-/*! \file tests/core/alias/w_window2d_int.cc
- *
- * \brief Tests on mln::w_window2d_int.
- */
+/// \file tests/core/alias/w_window2d_int.cc
+///
+/// Tests on mln::w_window2d_int.
#include <mln/core/alias/w_window2d_int.hh>
#include <mln/win/rectangle2d.hh>
Index: tests/core/alias/window2d.cc
--- tests/core/alias/window2d.cc (revision 2767)
+++ tests/core/alias/window2d.cc (working copy)
@@ -31,7 +31,6 @@
/// Tests on mln::window2d.
#include <mln/core/alias/window2d.hh>
-#include <mln/core/image/image2d.hh>
#include <mln/convert/to_image.hh>
Index: tests/core/alias/w_window3d_int.cc
--- tests/core/alias/w_window3d_int.cc (revision 2767)
+++ tests/core/alias/w_window3d_int.cc (working copy)
@@ -1,4 +1,5 @@
-// Copyright (C) 2007 EPITA Research and Development Laboratory
+// Copyright (C) 2007, 2008 EPITA Research and Development Laboratory
+// (LRDE)
//
// This file is part of the Olena Library. This library is free
// software; you can redistribute it and/or modify it under the terms
@@ -25,10 +26,9 @@
// reasons why the executable file might be covered by the GNU General
// Public License.
-/*! \file tests/core/alias/w_window3d_int.cc
- *
- * \brief Tests on mln::w_window3d_int.
- */
+/// tests/core/alias/w_window3d_int.cc
+///
+/// Tests on mln::w_window3d_int.
#include <mln/core/alias/w_window3d_int.hh>
#include <mln/win/cube3d.hh>
@@ -38,8 +38,6 @@
#include <mln/convert/to_fun.hh>
#include <mln/estim/sum.hh>
-#include <mln/debug/println.hh>
-
int f(mln::point3d p)
{
@@ -64,8 +62,6 @@
w_window3d_int w_win = make::w_window3d(ws);
image3d<int> ima = convert::to_image(w_win);
- debug::println(ima);
- std::cout << std::endl;
w_window3d_int w_win_2 = convert::to<w_window3d_int>(ima);
mln_assertion(w_win_2 == w_win);
}
@@ -77,7 +73,6 @@
// -2 -1 0 +1 +2
// -1 0 +1 +2 +3
image3d<int> ima = convert::to_image(w_win);
- debug::println(ima);
mln_assertion(estim::sum(ima) == 0);
}
Index: tests/core/alias/window3d.cc
--- tests/core/alias/window3d.cc (revision 2767)
+++ tests/core/alias/window3d.cc (working copy)
@@ -1,4 +1,5 @@
-// Copyright (C) 2007 EPITA Research and Development Laboratory
+// Copyright (C) 2007, 2008 EPITA Research and Development Laboratory
+// (LRDE)
//
// This file is part of the Olena Library. This library is free
// software; you can redistribute it and/or modify it under the terms
@@ -31,10 +32,7 @@
*/
#include <mln/core/alias/window3d.hh>
-#include <mln/core/image/image3d.hh>
#include <mln/convert/to_image.hh>
-#include <mln/debug/println.hh>
-
int main()
@@ -46,12 +44,10 @@
mln_assertion(w.is_centered() == false);
mln_assertion(w.is_symmetric() == true);
- w.insert(dpoint3d(-1, -1, -1));
- w.insert(dpoint3d( 1, 1, 1));
- w.insert(dpoint3d( 0, 0, 2));
+ w.insert(-1, -1, -1);
+ w.insert( 1, 1, 1);
+ w.insert( 0, 0, 2);
image3d<bool> ima = convert::to_image(w);
- debug::println(ima);
-
mln_assertion(w.delta() == 2);
}
Index: mln/trait/image_from_grid.hh
--- mln/trait/image_from_grid.hh (revision 2767)
+++ mln/trait/image_from_grid.hh (working copy)
@@ -1,4 +1,5 @@
// Copyright (C) 2007, 2008 EPITA Research and Development Laboratory
+// (LRDE)
//
// This file is part of the Olena Library. This library is free
// software; you can redistribute it and/or modify it under the terms
@@ -28,10 +29,13 @@
#ifndef MLN_TRAIT_IMAGE_FROM_MESH_HH
# define MLN_TRAIT_IMAGE_FROM_MESH_HH
-/*! \file mln/trait/image_from_grid.hh
- *
- * \brief Definition of the "image from mesh" trait.
- */
+/// \file mln/trait/image_from_grid.hh
+///
+/// Definition of the "image from mesh" trait.
+///
+/// This file shall not include the concrete image types it returns;
+/// otherwise we get circular dependencies. As a consequence the user
+/// shall include the appropriate image type she deals with.
# include <mln/core/grids.hh>
Index: mln/core/w_window.hh
--- mln/core/w_window.hh (revision 2767)
+++ mln/core/w_window.hh (working copy)
@@ -37,7 +37,7 @@
# include <map>
-# include <mln/core/concept/weighted_window.hh>
+# include <mln/core/internal/weighted_window_base.hh>
# include <mln/core/concept/image.hh>
# include <mln/core/site_set/box.hh>
# include <mln/core/window.hh>
@@ -45,7 +45,6 @@
# include <mln/value/ops.hh>
# include <mln/util/ord.hh>
-# include <mln/level/fill.hh>
# include <mln/geom/bbox.hh> // FIXME: We may have some dep trouble with this include.
# include <mln/literal/zero.hh>
# include <mln/convert/to.hh>
@@ -63,7 +62,7 @@
{
template <typename D, typename W>
- struct window_< mln::w_window<D,W> > : window_<W>
+ struct window_< mln::w_window<D,W> > : window_< mln::window<D> >
{
// Same traits as its corresponding window.
};
@@ -78,23 +77,15 @@
* the type of weights.
*/
template <typename D, typename W>
- struct w_window : public Weighted_Window< w_window<D,W> >
+ struct w_window : public internal::weighted_window_base< mln::window<D>,
+ w_window<D,W> >
{
/// Dpsite associated type.
typedef D dpsite;
- /// Psite associated type.
- typedef mln_psite(D) psite;
-
- /// Site associated type.
- typedef mln_site(D) site;
-
/// Weight associated type.
typedef W weight;
- /// Window associated type.
- typedef mln::window<D> window;
-
/// Site_Iterator type to browse (forward) the points of a generic w_window.
typedef with_w_< dpsites_fwd_piter< w_window<D, W> >, W > fwd_qiter;
@@ -118,12 +109,6 @@
const std::vector<W>& weights() const;
- // Give the \p i-th delta-point.
- const D& dp(unsigned i) const;
-
- /// Give the number of delta-points.
- unsigned size() const;
-
/// Give access to the vector of delta-points.
const std::vector<D>& std_vector() const;
@@ -237,25 +222,6 @@
template <typename D, typename W>
inline
- const D&
- w_window<D,W>::dp(unsigned i) const
- {
- mln_precondition(i < win_.size());
- mln_invariant(wei_.size() == win_.size());
- return win_.dp(i);
- }
-
- template <typename D, typename W>
- inline
- unsigned
- w_window<D,W>::size() const
- {
- mln_invariant(wei_.size() == win_.size());
- return win_.size();
- }
-
- template <typename D, typename W>
- inline
const std::vector<D>&
w_window<D,W>::std_vector() const
{
@@ -344,9 +310,10 @@
mlc_converts_to(mln_value(I), W)::check();
const I& ima = exact(from_);
to.clear();
+ mln_value(I) zero = literal::zero;
mln_piter(I) p(ima.domain());
for_all(p)
- if (ima(p) != literal::zero)
+ if (ima(p) != zero)
to.insert(ima(p), convert::to<D>(p));
}
@@ -363,8 +330,13 @@
// mln_precondition(w_win.is_valid());
ima.init_(geom::bbox(w_win));
- level::fill(ima, literal::zero);
-
+ {
+ // level::fill(ima, literal::zero) is:
+ mln_value(I) zero = literal::zero;
+ mln_piter(I) p(ima.domain());
+ for_all(p)
+ ima(p) = zero;
+ }
unsigned n = w_win.size();
for (unsigned i = 0; i < n; ++i)
ima(convert::to<P>(w_win.dp(i))) = w_win.w(i);
@@ -407,9 +379,10 @@
box<P> b(all_to(-s), all_to(+s));
mln_fwd_piter(box<P>) p(b);
unsigned i = 0;
+ V zero = literal::zero;
for_all(p)
{
- if (weight[i] != literal::zero)
+ if (weight[i] != zero)
to.insert(weight[i], convert::to<D>(p));
++i;
}
Index: mln/core/concept/weighted_window.hh
--- mln/core/concept/weighted_window.hh (revision 2767)
+++ mln/core/concept/weighted_window.hh (working copy)
@@ -29,10 +29,11 @@
#ifndef MLN_CORE_CONCEPT_WEIGHTED_WINDOW_HH
# define MLN_CORE_CONCEPT_WEIGHTED_WINDOW_HH
-/*! \file mln/core/concept/weighted_window.hh
- *
- * \brief Definition of the concept of mln::Weighted_Window.
- */
+/// \file mln/core/concept/weighted_window.hh
+///
+/// Definition of the concept of mln::Weighted_Window.
+///
+/// \todo Make ::sym() be optional.
# include <mln/core/concept/object.hh>
# include <mln/core/concept/iterator.hh>
@@ -42,8 +43,9 @@
namespace mln
{
- // Fwd decl.
- template <typename W> struct Weighted_Window;
+ // Forward declaration.
+ template <typename E> struct Weighted_Window;
+
// Weighted_Window category flag type.
template <>
@@ -59,8 +61,8 @@
* \see mln::doc::Weighted_Window for a complete documentation of
* this class contents.
*/
- template <typename W>
- struct Weighted_Window : public Object<W>
+ template <typename E>
+ struct Weighted_Window : public Object<E>
{
typedef Weighted_Window<void> category;
@@ -74,29 +76,9 @@
typedef weight;
typedef window;
- E& sym();
+ void sym();
*/
- /// Test if the weighted window is empty; final method.
- bool is_empty() const
- {
- return exact(this)->win().is_empty();
- }
-
- /// Test if the weighted window is centered; final method.
- bool is_centered() const
- {
- return exact(this)->win().is_centered();
- }
-
- // FIXME: Remove because too ambiguous: bool is_symmetric() const
-
- /// Give the maximum coordinate gap.
- unsigned delta() const
- {
- return exact(this)->win().delta();
- }
-
protected:
Weighted_Window();
};
@@ -110,24 +92,27 @@
W operator-(const Weighted_Window<W>& rhs);
+
# ifndef MLN_INCLUDE_ONLY
- template <typename W>
+ template <typename E>
inline
- Weighted_Window<W>::Weighted_Window()
+ Weighted_Window<E>::Weighted_Window()
{
- //typedef mln_psite(E) point;
- //typedef mln_dpsite(E) dpoint;
- typedef mln_weight(W) weight;
- typedef mln_window(W) window;
+ typedef mln_site(E) site;
+ typedef mln_psite(E) psite;
+ typedef mln_dpsite(E) dpsite;
+
+ typedef mln_weight(E) weight;
+ typedef mln_window(E) window;
- typedef mln_fwd_qiter(W) fwd_qiter;
- typedef mln_bkd_qiter(W) bkd_qiter;
+ typedef mln_fwd_qiter(E) fwd_qiter;
+ typedef mln_bkd_qiter(E) bkd_qiter;
- void (W::*m1)() = & W::sym;
+ void (E::*m1)() = & E::sym;
m1 = 0;
- const window& (W::*m2)() const = & W::win;
+ const window& (E::*m2)() const = & E::win;
m2 = 0;
}
Index: mln/core/concept/object.hh
--- mln/core/concept/object.hh (revision 2767)
+++ mln/core/concept/object.hh (working copy)
@@ -47,6 +47,8 @@
# include <mln/metal/is_a.hh>
# include <mln/metal/is.hh>
# include <mln/metal/is_not.hh>
+# include <mln/metal/equal.hh>
+# include <mln/metal/not_equal.hh>
# include <mln/metal/converts_to.hh>
# include <mln/metal/ret.hh>
# include <mln/metal/unqualif.hh>
Index: mln/core/concept/window.hh
--- mln/core/concept/window.hh (revision 2767)
+++ mln/core/concept/window.hh (working copy)
@@ -38,8 +38,6 @@
*
* \todo The is_centered() method could also exist when the window is
* not regular...
- *
- * \todo Remove hack.
*/
# include <mln/core/concept/object.hh>
@@ -47,10 +45,7 @@
# include <mln/trait/windows.hh>
# include <mln/core/site_set/p_array.hh>
-
-# include <mln/accu/bbox.hh>
-# include <mln/literal/origin.hh>
-# include <mln/level/fill.hh>
+# include <mln/core/internal/geom_bbox.hh> // For use in convert::from_to.
# include <mln/convert/from_to.hxx>
@@ -307,27 +302,6 @@
namespace convert
{
- namespace internal
- {
-
-
- // FIXME: Hack to avoid including geom::bbox (circular
- // dependency).
-
- template <typename W>
- box<mln_psite(W)> bbox_(const Window<W>& win)
- {
- typedef mln_psite(W) P;
- accu::bbox<P> b;
- P O = literal::origin;
- mln_qiter(W) q(exact(win), O);
- for_all(q)
- b.take(q);
- return b;
- }
-
- }
-
template <typename W, typename I>
void
from_to(const Window<W>& win_, Image<I>& ima_)
@@ -343,9 +317,14 @@
// mln_precondition(win.is_valid());
mln_precondition(! ima.has_data());
- ima.init_(internal::bbox_(win)); // geom::bbox(win));
- level::fill(ima, false);
-
+ // Hack (below) to avoid circular dependency.
+ ima.init_(mln::internal::geom_bbox(win));
+ {
+ // level::fill(ima, false) is:
+ mln_piter(I) p(ima.domain());
+ for_all(p)
+ ima(p) = false;
+ }
unsigned n = win.size();
for (unsigned i = 0; i < n; ++i)
ima(convert::to<P>(win.dp(i))) = true;
Index: mln/core/internal/geom_bbox.hh
--- mln/core/internal/geom_bbox.hh (revision 0)
+++ mln/core/internal/geom_bbox.hh (revision 0)
@@ -0,0 +1,75 @@
+// Copyright (C) 2008 EPITA Research and Development Laboratory (LRDE)
+//
+// This file is part of the Olena Library. This library is free
+// software; you can redistribute it and/or modify it under the terms
+// of the GNU General Public License version 2 as published by the
+// Free Software Foundation.
+//
+// This library 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 this library; see the file COPYING. If not, write to
+// the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+// Boston, MA 02111-1307, USA.
+//
+// As a special exception, you may use this file as part of a free
+// software library without restriction. Specifically, if other files
+// instantiate templates or use macros or inline functions from this
+// file, or you compile this file and link it with other files to
+// produce an executable, this file does not by itself cause the
+// resulting executable to be covered by the GNU General Public
+// License. This exception does not however invalidate any other
+// reasons why the executable file might be covered by the GNU General
+// Public License.
+
+#ifndef MLN_CORE_INTERNAL_GEOM_BBOX_HH
+# define MLN_CORE_INTERNAL_GEOM_BBOX_HH
+
+/// \file mln/core/internal/geom_bbox.hh
+///
+/// \internal Routine that computes a bounding box from a window.
+/// This file is included in mln/core/concept/window.hh and avoid
+/// circular dependency since mln/geom/bbox.hh cannot be included in
+/// concept files.
+
+# include <mln/accu/bbox.hh>
+# include <mln/literal/origin.hh>
+
+
+namespace mln
+{
+
+ namespace internal
+ {
+
+ template <typename W>
+ box<mln_psite(W)>
+ geom_bbox(const W& win);
+
+
+# ifndef MLN_INCLUDE_ONLY
+
+ template <typename W>
+ box<mln_psite(W)>
+ geom_bbox(const W& win)
+ {
+ typedef mln_psite(W) P;
+ accu::bbox<P> b;
+ P O = literal::origin;
+ mln_qiter(W) q(exact(win), O);
+ for_all(q)
+ b.take(q);
+ return b;
+ }
+
+# endif // ! MLN_INCLUDE_ONLY
+
+ } // end of namespace mln::internal
+
+} // end of namespace mln
+
+
+#endif // ! MLN_CORE_INTERNAL_GEOM_BBOX_HH
Index: mln/core/internal/weighted_window_base.hh
--- mln/core/internal/weighted_window_base.hh (revision 0)
+++ mln/core/internal/weighted_window_base.hh (revision 0)
@@ -0,0 +1,178 @@
+// Copyright (C) 2008 EPITA Research and Development Laboratory (LRDE)
+//
+// This file is part of the Olena Library. This library is free
+// software; you can redistribute it and/or modify it under the terms
+// of the GNU General Public License version 2 as published by the
+// Free Software Foundation.
+//
+// This library 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 this library; see the file COPYING. If not, write to
+// the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+// Boston, MA 02111-1307, USA.
+//
+// As a special exception, you may use this file as part of a free
+// software library without restriction. Specifically, if other files
+// instantiate templates or use macros or inline functions from this
+// file, or you compile this file and link it with other files to
+// produce an executable, this file does not by itself cause the
+// resulting executable to be covered by the GNU General Public
+// License. This exception does not however invalidate any other
+// reasons why the executable file might be covered by the GNU General
+// Public License.
+
+#ifndef MLN_CORE_INTERNAL_WEIGHTED_WINDOW_BASE_HH
+# define MLN_CORE_INTERNAL_WEIGHTED_WINDOW_BASE_HH
+
+/// \file mln/core/internal/weighted_window_base.hh
+///
+/// Definition of a base class for weighted window classes.
+
+# include <mln/core/concept/weighted_window.hh>
+
+
+namespace mln
+{
+
+ namespace internal
+ {
+
+ /// Base class for weighted window classes.
+ ///
+ /// \p W is the corresponding window type.
+
+ template <typename W, typename E>
+ struct weighted_window_base : public Weighted_Window<E>
+ {
+
+ /// Window associated type.
+ typedef W window;
+
+ /// Dpsite associated type.
+ typedef mln_dpsite(W) dpsite;
+
+ /// Psite associated type.
+ typedef mln_psite(W) psite;
+
+ /// Site associated type.
+ typedef mln_site(W) site;
+
+ /// Test if the weighted window is empty; final method.
+ bool is_empty() const;
+
+ /// Give the window size; final method. This method is valid
+ /// iff the underlying window has a fixed size.
+ unsigned size() const;
+
+ /// Test if the weighted window is centered; final method. This
+ /// method is valid iff the support is regular and the
+ /// definition is not varying.
+ bool is_centered() const;
+
+ /// Give the maximum coordinate gap; final method. This method
+ /// is valid iff the support is regular and the definition is
+ /// not varying.
+ unsigned delta() const;
+
+ /// Give the \p i-th delta-point; final method. This method is
+ /// valid iff the support is regular and the definition is
+ /// unique.
+ const mln_dpsite(W)& dp(unsigned i) const;
+
+ /// Test if the \p dp delta-point is in this window; final
+ /// method. This method is valid iff the support is regular and
+ /// the definition is unique.
+ bool has(const mln_dpsite(W)& dp) const;
+
+ protected:
+ weighted_window_base();
+ };
+
+
+# ifndef MLN_INCLUDE_ONLY
+
+ template <typename W, typename E>
+ inline
+ weighted_window_base<W,E>::weighted_window_base()
+ {
+ }
+
+ template <typename W, typename E>
+ inline
+ bool
+ weighted_window_base<W,E>::is_empty() const
+ {
+ return exact(this)->win().is_empty();
+ }
+
+ template <typename W, typename E>
+ inline
+ unsigned
+ weighted_window_base<W,E>::size() const
+ {
+ mlc_equal(mln_trait_window_size(W),
+ trait::window::size::fixed)::check();
+ return exact(this)->win().size();
+ }
+
+ template <typename W, typename E>
+ inline
+ bool
+ weighted_window_base<W,E>::is_centered() const
+ {
+ mlc_equal(mln_trait_window_support(W),
+ trait::window::support::regular)::check();
+ mlc_not_equal(mln_trait_window_definition(W),
+ trait::window::definition::varying)::check();
+ return exact(this)->win().is_centered();
+ }
+
+ template <typename W, typename E>
+ inline
+ unsigned
+ weighted_window_base<W,E>::delta() const
+ {
+ mlc_equal(mln_trait_window_support(W),
+ trait::window::support::regular)::check();
+ mlc_not_equal(mln_trait_window_definition(W),
+ trait::window::definition::varying)::check();
+ return exact(this)->win().delta();
+ }
+
+ template <typename W, typename E>
+ inline
+ const mln_dpsite(W)&
+ weighted_window_base<W,E>::dp(unsigned i) const
+ {
+ mlc_equal(mln_trait_window_support(W),
+ trait::window::support::regular)::check();
+ mlc_equal(mln_trait_window_definition(W),
+ trait::window::definition::unique)::check();
+ mln_precondition(i < this->size());
+ return exact(this)->win().dp(i);
+ }
+
+ template <typename W, typename E>
+ inline
+ bool
+ weighted_window_base<W,E>::has(const mln_dpsite(W)& dp) const
+ {
+ mlc_equal(mln_trait_window_support(W),
+ trait::window::support::regular)::check();
+ mlc_equal(mln_trait_window_definition(W),
+ trait::window::definition::unique)::check();
+ return exact(this)->win().has(dp);
+ }
+
+# endif // ! MLN_INCLUDE_ONLY
+
+ } // end of namespace mln::internal
+
+} // end of namespace mln
+
+
+#endif // ! MLN_CORE_INTERNAL_WEIGHTED_WINDOW_BASE_HH
Index: mln/core/internal/window_base.hh
--- mln/core/internal/window_base.hh (revision 2767)
+++ mln/core/internal/window_base.hh (working copy)
@@ -1,4 +1,4 @@
-// Copyright (C) 2008 EPITA Research and Development Laboratory
+// Copyright (C) 2008 EPITA Research and Development Laboratory (LRDE)
//
// This file is part of the Olena Library. This library is free
// software; you can redistribute it and/or modify it under the terms
Index: mln/make/w_window2d.hh
--- mln/make/w_window2d.hh (revision 2767)
+++ mln/make/w_window2d.hh (working copy)
@@ -36,6 +36,7 @@
# include <mln/core/w_window.hh>
# include <mln/core/alias/dpoint2d.hh>
+# include <mln/convert/from_to.hxx>
namespace mln
Index: mln/convert/to_image.hh
--- mln/convert/to_image.hh (revision 2767)
+++ mln/convert/to_image.hh (working copy)
@@ -40,12 +40,13 @@
# include <mln/core/concept/weighted_window.hh>
# include <mln/core/concept/neighborhood.hh>
-# include <mln/literal/zero.hh>
# include <mln/geom/bbox.hh>
# include <mln/level/fill.hh>
# include <mln/histo/data.hh>
-
+# include <mln/core/image/image1d.hh>
+# include <mln/core/image/image2d.hh>
+# include <mln/core/image/image3d.hh>
namespace mln
@@ -68,7 +69,7 @@
/// Convert a weighted window \p w_win into an image.
template <typename W>
- mln_image_from_grid(mln_site(W)::grid, bool)
+ mln_image_from_grid(mln_site(W)::grid, mln_weight(W))
to_image(const Weighted_Window<W>& w_win);
/// Convert a neighborhood \p nbh into a binary image.
@@ -114,12 +115,12 @@
template <typename W>
inline
- mln_image_from_grid(mln_site(W)::grid, bool)
+ mln_image_from_grid(mln_site(W)::grid, mln_weight(W))
to_image(const Weighted_Window<W>& w_win)
{
mln_is_simple_window(W)::check();
// mln_precondition(exact(w_win).is_valid());
- mln_image_from_grid(mln_site(W)::grid, bool) tmp;
+ mln_image_from_grid(mln_site(W)::grid, mln_weight(W)) tmp;
convert::from_to(exact(w_win), tmp);
return tmp;
}
1
0
https://svn.lrde.epita.fr/svn/oln/branches/cleanup-2008
Index: ChangeLog
from Ugo Jardonnet <ugo.jardonnet(a)lrde.epita.fr>
Remove a std::size_t.
In my opinion nelements must not return size_t.
* milena/mln/core/site_set/p_image.hh,
* milena/mln/core/image/image3d.hh: Here.
image/image3d.hh | 4 ++--
site_set/p_image.hh | 6 +++---
2 files changed, 5 insertions(+), 5 deletions(-)
Index: milena/mln/core/site_set/p_image.hh
--- milena/mln/core/site_set/p_image.hh (revision 2766)
+++ milena/mln/core/site_set/p_image.hh (working copy)
@@ -115,7 +115,7 @@
bool is_valid() const;
/// Give the number of sites.
- std::size_t nsites() const;
+ unsigned nsites() const;
/// Insertion element associated type.
@@ -149,7 +149,7 @@
private:
I ima_;
- std::size_t nsites_;
+ unsigned nsites_;
};
@@ -198,7 +198,7 @@
template <typename I>
inline
- std::size_t
+ unsigned
p_image<I>::nsites() const
{
return nsites_;
Index: milena/mln/core/image/image3d.hh
--- milena/mln/core/image/image3d.hh (revision 2766)
+++ milena/mln/core/image/image3d.hh (working copy)
@@ -184,7 +184,7 @@
unsigned border() const;
/// Give the number of cells (points including border ones).
- std::size_t nelements() const;
+ unsigned nelements() const;
/// Read-only access to the image value located at point \p p.
const T& operator()(const point3d& p) const;
@@ -426,7 +426,7 @@
template <typename T>
inline
- std::size_t
+ unsigned
image3d<T>::nelements() const
{
mln_precondition(this->has_data());
1
0
https://svn.lrde.epita.fr/svn/oln/branches/cleanup-2008/milena/sandbox
Index: ChangeLog
from Alexandre Abraham <abraham(a)lrde.epita.fr>
Fix projection.
* nature/proj.cc: .
proj.cc | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
Index: nature/proj.cc
--- nature/proj.cc (revision 2765)
+++ nature/proj.cc (working copy)
@@ -56,10 +56,11 @@
std::string name(argv[i]);
name.erase(name.length() - 4);
image1d<float> toto = proj_nat(ima);
- image2d<float> tata (toto.nelements(), 1);
+ image2d<int_u8> tata (toto.nelements(), 1);
mln_piter_(image1d<float>) p(toto.domain());
for (unsigned u = 0; u < toto.nelements(); ++u)
- tata(point2d(u, 1)) = toto(point1d(u));
+ if (toto.domain().has(point1d(u)))
+ tata(point2d(u, 1)) = (int_u8) ceil(toto(point1d(u)));
io::pgm::save(cast_image <int_u8>(tata), name.append("_proj.pgm"));
}
}
1
0
https://svn.lrde.epita.fr/svn/oln/branches/cleanup-2008/milena/sandbox
Index: ChangeLog
from Alexandre Abraham <abraham(a)lrde.epita.fr>
Fix 1d to 2d problem.
* nature/proj.cc: use an unsigned to iterate over the image.
proj.cc | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
Index: nature/proj.cc
--- nature/proj.cc (revision 2764)
+++ nature/proj.cc (working copy)
@@ -58,8 +58,8 @@
image1d<float> toto = proj_nat(ima);
image2d<float> tata (toto.nelements(), 1);
mln_piter_(image1d<float>) p(toto.domain());
- for_all(p)
- tata(point2d((p.row(), 1))) = toto(p);
- io::pgm::save(cast_image <int>(tata), name.append("_proj.pgm"));
+ for (unsigned u = 0; u < toto.nelements(); ++u)
+ tata(point2d(u, 1)) = toto(point1d(u));
+ io::pgm::save(cast_image <int_u8>(tata), name.append("_proj.pgm"));
}
}
1
0
https://svn.lrde.epita.fr/svn/oln/branches/cleanup-2008/milena/sandbox
Index: ChangeLog
from Alexandre Abraham <abraham(a)lrde.epita.fr>
Add closing, udate filter parameters.
* nature/closing.cc: New.
* nature/gradient.cc: .
* nature/histo_hsi.cc: .
* nature/hom.cc: .
* nature/opening.cc: .
closing.cc | 59 +++++++++++++++++++++++++++++++
gradient.cc | 5 +-
histo_hsi.cc | 39 +++++++++++---------
hom.cc | 110 ++++++++++++++++++++++++++++++++++++++++++++++++-----------
opening.cc | 10 ++---
5 files changed, 180 insertions(+), 43 deletions(-)
Index: nature/closing.cc
--- nature/closing.cc (revision 0)
+++ nature/closing.cc (revision 0)
@@ -0,0 +1,59 @@
+// Copyright (C) 2007, 2008 EPITA Research and Development Laboratory (LRDE)
+//
+// This file is part of the Olena Library. This library is free
+// software; you can redistribute it and/or modify it under the terms
+// of the GNU General Public License version 2 as published by the
+// Free Software Foundation.
+//
+// This library 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 this library; see the file COPYING. If not, write to
+// the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+// Boston, MA 02111-1307, USA.
+//
+// As a special exception, you may use this file as part of a free
+// software library without restriction. Specifically, if other files
+// instantiate templates or use macros or inline functions from this
+// file, or you compile this file and link it with other files to
+// produce an executable, this file does not by itself cause the
+// resulting executable to be covered by the GNU General Public
+// License. This exception does not however invalidate any other
+// reasons why the executable file might be covered by the GNU General
+// Public License.
+
+#include <mln/core/image/image2d.hh>
+#include <mln/win/rectangle2d.hh>
+
+#include <mln/io/pgm/load.hh>
+#include <mln/io/pgm/save.hh>
+
+#include <mln/value/int_u8.hh>
+#include <mln/morpho/closing.hh>
+
+int main(int argc, const char * argv[])
+{
+ using namespace mln;
+ using value::int_u8;
+
+ if (argc < 2) {
+ std::cerr << "usage: " << argv[0] << " in.pgm [other_files.pgm]" << std::endl;
+ return 1;
+ }
+
+ for (int i = 1; i < argc; ++i)
+ {
+ image2d<int_u8> ima;
+ io::pgm::load(ima, argv[i]);
+
+ win::rectangle2d rect(5, 5);
+ border::thickness = 11;
+
+ std::string name(argv[i]);
+ name.erase(name.length() - 4);
+ io::pgm::save(morpho::closing(ima, rect), name.append("_opened.pgm"));
+ }
+}
Index: nature/gradient.cc
--- nature/gradient.cc (revision 2760)
+++ nature/gradient.cc (working copy)
@@ -53,7 +53,8 @@
win::rectangle2d rect(5, 5);
border::thickness = 5;
- io::pgm::save( morpho::gradient(ima, rect),
- "out.pgm" );
+ std::string name(argv[i]);
+ name.erase(name.length() - 4);
+ io::pgm::save(morpho::gradient(ima, rect), name.append("_grad.pgm"));
}
}
Index: nature/histo_hsi.cc
--- nature/histo_hsi.cc (revision 2760)
+++ nature/histo_hsi.cc (working copy)
@@ -61,24 +61,28 @@
template <typename I>
-void save_histo(Image<I> &i, std::string &name, unsigned width, unsigned height, unsigned npoints)
+void save_histo(Image<I> &i, std::string &name)
{
I& ima = exact(i);
- histo::data<u_t> h = histo::compute(ima);
+ histo::data<float01_8> h = histo::compute(ima);
- double norm = (double) npoints / (double) height;
+ // Compute histo max
+ size_t max = 0;
+ mln_viter(mln::value::set<float01_8>) v(h.vset());
- image2d<bool> output(height + 1, width + 1, 0);
+ for_all(v)
+ if (h(v) > max)
+ max = h(v);
+
+ image2d<bool> output(max, mln_card(float01_8), 0);
level::fill(output, true);
- mln_viter(mln::value::set<u_t>) v(h.vset());
for_all(v)
- if (h(v) > 0)
- for (u_t i = 0; i < h(v)/norm; ++i)
+ for (size_t i = 0; i < h(v); ++i)
{
// std::cout << height - i << ", " << (u_t)v << std::endl;
- output(point2d(height - i, (u_t)v)) = false;
+ output(point2d(max - i - 1, (float01_8)v)) = false;
}
io::pbm::save(output, name);
@@ -91,21 +95,19 @@
return 1;
}
- for (unsigned i = 1; i < argc; ++i)
+ for (int i = 1; i < argc; ++i)
{
image2d<rgb8> input;
io::ppm::load(input, argv[i]);
- unsigned npoints = input.ncols() * input.nrows();
-
image2d<hsi_f> hsi = level::transform(input, fun::v2v::f_rgb_to_hsi_f);
thru<mln::meta::hue<hsi_f>, image2d<hsi_f> > h(hsi);
- cast_image_<u_t, thru<mln::meta::hue<hsi_f>, image2d<hsi_f> > > hue(h);
+ cast_image_<float01_8, thru<mln::meta::hue<hsi_f>, image2d<hsi_f> > > hue(h);
std::string n(argv[i]);
n.erase(n.length() - 4);
- io::pgm::save(hue, n.append("_hue.pgm"));
+ // io::pgm::save(hue, n.append("_hue.pgm"));
image2d<hsi_f>::piter p(hsi.domain());
float m = 0;
@@ -120,8 +122,10 @@
std::string name(argv[i]);
name.erase(name.length() - 4);
- save_histo(hue, name.append("_hue.pbm"), 256, 360, npoints);
+ save_histo(hue, name.append("_hue.pbm"));
+
+ /*
thru<mln::meta::sat<hsi_f>, image2d<hsi_f> > s(hsi);
cast_image_<u_t, thru<mln::meta::sat<hsi_f>, image2d<hsi_f> > > sat(s);
@@ -162,5 +166,6 @@
name = argv[i];
name.erase(name.length() - 4);
save_histo(inty, name.append("_inty.pbm"), 256, 256, npoints);
+ */
}
}
Index: nature/hom.cc
--- nature/hom.cc (revision 2760)
+++ nature/hom.cc (working copy)
@@ -34,6 +34,7 @@
#include <mln/io/pbm/save.hh>
#include <mln/value/int_u8.hh>
+#include <mln/value/int_u16.hh>
# include <mln/core/alias/window2d.hh>
@@ -56,39 +57,110 @@
for (int i = 1; i < argc; ++i)
{
- image2d<int_u8> ima;
+ typedef int_u8 int_t;
+
+ image2d<int_t> ima;
io::pgm::load(ima, argv[i]);
// Compute the mean
- int_u8 mean = estim::mean(ima);
+ int_t mean = estim::mean(ima);
+
+ image2d<bool> imab = binarization::threshold(ima, mean);
+
+ border::thickness = 10;
window2d winout;
window2d winin;
- static const bool matout [] = {0, 0, 0, 0, 0, 0, 0,
- 1, 0, 0, 0, 0, 0, 0,
- 1, 0, 0, 0, 0, 0, 0,
- 1, 0, 0, 0, 0, 0, 0,
- 1, 0, 0, 0, 0, 0, 0,
- 1, 0, 0, 0, 0, 0, 0,
- 1, 1, 1, 1, 1, 1, 0};
-
- convert::from_to(matout, winout);
-
- static const bool matin [] = {0, 1, 0, 0, 0, 0, 0,
- 0, 1, 0, 0, 0, 0, 0,
- 0, 1, 0, 0, 0, 0, 0,
- 0, 1, 0, 0, 0, 0, 0,
- 0, 1, 0, 0, 0, 0, 0,
- 0, 1, 1, 1, 1, 1, 1,
- 0, 0, 0, 0, 0, 0, 0};
+// static const bool matout [] = {0, 0, 0, 0, 0, 0, 0,
+// 0, 0, 1, 0, 0, 0, 0,
+// 0, 0, 1, 0, 0, 0, 0,
+// 0, 0, 1, 0, 0, 0, 0,
+// 0, 0, 1, 1, 1, 1, 0,
+// 0, 0, 0, 0, 0, 0, 0,
+// 0, 0, 0, 0, 0, 0, 0};
+
+// static const bool matout [] = {0, 0, 0, 0, 0,
+// 0, 1, 0, 0, 0,
+// 0, 1, 0, 0, 0,
+// 0, 1, 1, 1, 0,
+// 0, 0, 0, 0, 0};
+
+ static const bool blmatout [] = {0, 0, 0,
+ 1, 0, 0,
+ 1, 1, 0};
+
+
+ convert::from_to(blmatout, winout);
+
+// static const bool matin [] = {0, 0, 0, 1, 0, 0, 0,
+// 0, 0, 0, 1, 0, 0, 0,
+// 0, 0, 0, 1, 0, 0, 0,
+// 0, 0, 0, 1, 1, 1, 1,
+// 0, 0, 0, 0, 0, 0, 0,
+// 0, 0, 0, 0, 0, 0, 0,
+// 0, 0, 0, 0, 0, 0, 0};
+
+// static const bool matin [] = {0, 0, 1, 0, 0,
+// 0, 0, 1, 0, 0,
+// 0, 0, 1, 1, 1,
+// 0, 0, 0, 0, 0,
+// 0, 0, 0, 0, 0};
+
+ static const bool blmatin [] = {0, 1, 0,
+ 0, 1, 1,
+ 0, 0, 0};
+
+ convert::from_to(blmatin, winin);
+ image2d<bool> bottom_left = morpho::hit_or_miss(imab, winout, winin);
+
+
+ static const bool brmatout [] = {0, 0, 0,
+ 0, 0, 1,
+ 0, 1, 1};
+
+ static const bool brmatin [] = {0, 1, 0,
+ 1, 1, 0,
+ 0, 0, 0};
+
+ convert::from_to(brmatout, winout);
+ convert::from_to(brmatin, winin);
+ image2d<bool> bottom_right = morpho::hit_or_miss(imab, winout, winin);
+
+ static const bool urmatout [] = {0, 1, 1,
+ 0, 0, 1,
+ 0, 0, 0};
+
+ static const bool urmatin [] = {0, 0, 0,
+ 1, 1, 0,
+ 0, 1, 0};
+
+ convert::from_to(urmatout, winout);
+ convert::from_to(urmatin, winin);
+ image2d<bool> up_right = morpho::hit_or_miss(imab, winout, winin);
+
+
+ static const bool ulmatout [] = {1, 1, 0,
+ 1, 0, 0,
+ 0, 0, 0};
+
+ static const bool ulmatin [] = {0, 0, 0,
+ 0, 1, 1,
+ 0, 1, 0};
+
+ convert::from_to(ulmatout, winout);
+ convert::from_to(ulmatin, winin);
+ image2d<bool> up_left = morpho::hit_or_miss(imab, winout, winin);
- convert::from_to(matin, winin);
std::string name(argv[i]);
name.erase(name.length() - 4);
- io::pbm::save( morpho::hit_or_miss(binarization::threshold(ima, mean), winout, winin),
+ io::pbm::save( imab, name.append("_bin.pbm"));
+
+ name = argv[i];
+ name.erase(name.length() - 4);
+ io::pbm::save( up_left + up_right + bottom_right + bottom_left,
name.append("_hom.pbm"));
}
}
Index: nature/opening.cc
--- nature/opening.cc (revision 2760)
+++ nature/opening.cc (working copy)
@@ -34,7 +34,6 @@
#include <mln/value/int_u8.hh>
#include <mln/morpho/opening.hh>
-
int main(int argc, const char * argv[])
{
using namespace mln;
@@ -50,10 +49,11 @@
image2d<int_u8> ima;
io::pgm::load(ima, argv[i]);
- win::rectangle2d rect(1, 51);
- border::thickness = 100;
+ win::rectangle2d rect(5, 5);
+ border::thickness = 11;
- io::pgm::save( morpho::opening(ima, rect),
- "out.pgm" );
+ std::string name(argv[i]);
+ name.erase(name.length() - 4);
+ io::pgm::save(morpho::opening(ima, rect), name.append("_opened.pgm"));
}
}
1
0
https://svn.lrde.epita.fr/svn/oln/branches/cleanup-2008/milena/sandbox
Index: ChangeLog
from Alexandre Abraham <abraham(a)lrde.epita.fr>
Updating filters.
* nature/erosion.cc: More user friendly.
* nature/gradient.cc: .
* nature/hom.cc: Now working.
* nature/mco.cc: Now working on every images.
* nature/opening.cc: .
erosion.cc | 7 ++++---
gradient.cc | 2 +-
hom.cc | 40 +++++++++++++++++++++++++++++++---------
mco.cc | 53 ++++++++++++++++++++++++++---------------------------
opening.cc | 2 +-
5 files changed, 63 insertions(+), 41 deletions(-)
Index: nature/erosion.cc
--- nature/erosion.cc (revision 2754)
+++ nature/erosion.cc (working copy)
@@ -45,7 +45,7 @@
return 1;
}
- for (unsigned i = 1; i < argc; ++i)
+ for (int i = 1; i < argc; ++i)
{
image2d<int_u8> ima;
io::pgm::load(ima, argv[i]);
@@ -53,7 +53,8 @@
win::hline2d f(31);
border::thickness = 16;
- io::pgm::save( morpho::erosion(ima, f),
- "out.pgm" );
+ std::string name(argv[i]);
+ name.erase(name.length() - 4);
+ io::pgm::save(morpho::erosion(ima, f), name.append("_eroded.pgm"));
}
}
Index: nature/gradient.cc
--- nature/gradient.cc (revision 2754)
+++ nature/gradient.cc (working copy)
@@ -45,7 +45,7 @@
return 1;
}
- for (unsigned i = 1; i < argc; ++i)
+ for (int i = 1; i < argc; ++i)
{
image2d<int_u8> ima;
io::pgm::load(ima, argv[i]);
Index: nature/hom.cc
--- nature/hom.cc (revision 2754)
+++ nature/hom.cc (working copy)
@@ -35,7 +35,7 @@
#include <mln/value/int_u8.hh>
-
+# include <mln/core/alias/window2d.hh>
#include <mln/level/transform.hh>
#include <mln/binarization/threshold.hh>
@@ -47,26 +47,48 @@
int main(int argc, const char * argv[])
{
using namespace mln;
- using value::int_u16;
+ using namespace value;
if (argc < 2) {
std::cerr << "usage: " << argv[0] << " in.pgm [other_files.pgm]" << std::endl;
return 1;
}
- for (unsigned i = 1; i < argc; ++i)
+ for (int i = 1; i < argc; ++i)
{
- image2d<int_u16> ima;
+ image2d<int_u8> ima;
io::pgm::load(ima, argv[i]);
// Compute the mean
- int_u16 mean = estim::mean(ima);
-
- win::rectangle2d rectout(3, 3);
- win::rectangle2d rectin(5, 5);
+ int_u8 mean = estim::mean(ima);
+ window2d winout;
+ window2d winin;
- io::pbm::save( morpho::hit_or_miss(binarization::threshold(ima, mean), rectout, rectin),
- "out.pbm" );
+ static const bool matout [] = {0, 0, 0, 0, 0, 0, 0,
+ 1, 0, 0, 0, 0, 0, 0,
+ 1, 0, 0, 0, 0, 0, 0,
+ 1, 0, 0, 0, 0, 0, 0,
+ 1, 0, 0, 0, 0, 0, 0,
+ 1, 0, 0, 0, 0, 0, 0,
+ 1, 1, 1, 1, 1, 1, 0};
+
+ convert::from_to(matout, winout);
+
+ static const bool matin [] = {0, 1, 0, 0, 0, 0, 0,
+ 0, 1, 0, 0, 0, 0, 0,
+ 0, 1, 0, 0, 0, 0, 0,
+ 0, 1, 0, 0, 0, 0, 0,
+ 0, 1, 0, 0, 0, 0, 0,
+ 0, 1, 1, 1, 1, 1, 1,
+ 0, 0, 0, 0, 0, 0, 0};
+
+ convert::from_to(matin, winin);
+
+
+ std::string name(argv[i]);
+ name.erase(name.length() - 4);
+ io::pbm::save( morpho::hit_or_miss(binarization::threshold(ima, mean), winout, winin),
+ name.append("_hom.pbm"));
}
}
Index: nature/mco.cc
--- nature/mco.cc (revision 2754)
+++ nature/mco.cc (working copy)
@@ -1,45 +1,44 @@
#include <iostream>
-#include <mln/value/int_u.hh>
#include <mln/core/image/image2d.hh>
+#include <mln/core/image/cast_image.hh>
+#include <mln/value/int_u16.hh>
#include "co_occurence.hh"
#include <mln/core/alias/dpoint2d.hh>
+#include <mln/io/pgm/save.hh>
+#include <mln/io/pgm/load.hh>
+#include <mln/estim/min_max.hh>
-int main ()
+int main(int argc, const char * argv[])
{
using namespace mln;
using namespace value;
- typedef image2d< int_u<3> > I;
-
- int_u<3> vs[6][5] = {
-
- { 3, 3, 4, 4, 4 },
- { 2, 1, 1, 1, 1 },
- { 1, 4, 4, 4, 6 },
- { 1, 4, 3, 4, 1 },
- { 7, 4, 5, 3, 1 },
- { 7, 7, 1, 1, 0 }
-
- };
-
- I ima(make::image2d(vs));
+ if (argc < 2) {
+ std::cerr << "usage: " << argv[0] << " in.pgm [other_files.pgm]" << std::endl;
+ return 1;
+ }
dpoint2d d(0, 1);
- // std::cout << co_occurence(ima, d) << std::endl;
+ for (int i = 1; i < argc; ++i)
+ {
+ image2d<int_u8> ima;
+ io::pgm::load(ima, argv[i]);
+
+ std::string name(argv[i]);
+ name.erase(name.length() - 4);
image2d<unsigned> co(co_occurence(ima, d));
- unsigned cpt = 0;
-
+ // Normalization
+ unsigned m, M;
+ estim::min_max(co, m, M);
+ double norm = 255./M;
mln_piter_(image2d<unsigned>) p(co.domain());
for_all(p)
- {
- std::cout << p << " = " << co(p) << std::endl;
- cpt += co(p);
- }
-
- std::cout << cpt << std::endl;
+ co(p) *= norm;
+ io::pgm::save(cast_image<int_u<8> >(co), name.append("_mco.pgm"));
+ }
}
Index: nature/opening.cc
--- nature/opening.cc (revision 2754)
+++ nature/opening.cc (working copy)
@@ -45,7 +45,7 @@
return 1;
}
- for (unsigned i = 1; i < argc; ++i)
+ for (int i = 1; i < argc; ++i)
{
image2d<int_u8> ima;
io::pgm::load(ima, argv[i]);
1
0
URL: https://svn.lrde.epita.fr/svn/oln/branches/cleanup-2008/milena/sandbox
ChangeLog:
2008-11-02 Jimmy Ma <jimmy.ma(a)lrde.epita.fr>
Cleanup OCR preprocessing.
* garrigues/ocr/ocr_with_preprocess.cc,
garrigues/ocr/ocr_without_preprocess.cc: Cleanup the call to
tesseract.
* garrigues/ocr/tesseract_wrap.hh: New.
Provide a Tesseract wrapper which is also able to compute an
approximate confidence value of the recognition based on tesseract
internal data.
---
ocr_with_preprocess.cc | 64 ++++++++++++++++++--------------
ocr_without_preprocess.cc | 24 ++++--------
tesseract_wrap.hh | 91 ++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 135 insertions(+), 44 deletions(-)
Index: branches/cleanup-2008/milena/sandbox/garrigues/ocr/ocr_without_preprocess.cc
===================================================================
--- branches/cleanup-2008/milena/sandbox/garrigues/ocr/ocr_without_preprocess.cc (revision 2759)
+++ branches/cleanup-2008/milena/sandbox/garrigues/ocr/ocr_without_preprocess.cc (revision 2760)
@@ -53,7 +53,7 @@
#include <mln/labeling/regional_maxima.hh>
#include <mln/morpho/dilation.hh>
-#include <tesseract/baseapi.h>
+#include "tesseract_wrap.hh"
// _COMPILATION_
// g++ -DNDEBUG -O3 -I../../.. ocr.cc -L/usr/lib -ltesseract_full -lpthread
@@ -61,19 +61,6 @@
// Call tesseract
// lang: expected language
-template <typename T>
-char* tesseract(const char* lang, const mln::image2d<T>& input)
-{
- TessBaseAPI::InitWithLanguage(NULL, NULL, lang, NULL, false, 0, NULL);
- char* s = TessBaseAPI::TesseractRect(
- (unsigned char*) input.buffer(),
- sizeof (T),
- input.ncols() * sizeof (T),
- 0, 0,
- input.ncols(),
- input.nrows());
- return s;
-}
int main(int argc, char** argv)
{
@@ -92,7 +79,12 @@
io::pbm::load(input, argv[1]);
- char* s = tesseract("fra", input);
+ {
+ image2d<int_u8> tmp = clone(cast_image<int_u8>(input));
+ float score = 0;
+ char* s = tesseract("fra", tmp, &score);
+ std::cerr << "Tesseract result: (score " << score << ")" << std::endl;
std::cout << s;
- free(s);
+ delete[] s;
+ }
}
Index: branches/cleanup-2008/milena/sandbox/garrigues/ocr/tesseract_wrap.hh
===================================================================
--- branches/cleanup-2008/milena/sandbox/garrigues/ocr/tesseract_wrap.hh (revision 0)
+++ branches/cleanup-2008/milena/sandbox/garrigues/ocr/tesseract_wrap.hh (revision 2760)
@@ -0,0 +1,91 @@
+#ifndef TESSERACT_WRAP_HH
+# define TESSERACT_WRAP_HH
+
+# include <cassert>
+
+# include <mln/core/image/image2d.hh>
+
+# include <tesseract/baseapi.h>
+
+struct TessWrap : public TessBaseAPI
+{
+ static int InitWithLanguage(const char* datapath, const char* outputbase,
+ const char* language, const char* configfile,
+ bool numeric_mode, int argc, char* argv[])
+ {
+ return TessBaseAPI::InitWithLanguage(datapath, outputbase,
+ language, configfile,
+ numeric_mode, argc, argv);
+ }
+
+ static char* TesseractRect(const unsigned char* imagedata,
+ int bytes_per_pixel,
+ int bytes_per_line,
+ int left, int top, int width, int height,
+ float* score)
+ {
+ int kMinRectSize = 10; // Quick and dirty... (value imported from tesseract src)
+ if (width < kMinRectSize || height < kMinRectSize)
+ return NULL; // Nothing worth doing.
+
+ // Copy/Threshold the image to the tesseract global page_image.
+ CopyImageToTesseract(imagedata, bytes_per_pixel, bytes_per_line,
+ left, top, width, height);
+
+ { // RecognizeToString
+ BLOCK_LIST* block_list = FindLinesCreateBlockList();
+ PAGE_RES* page_res = Recognize(block_list, NULL);
+
+ char* string;
+ int* lengths;
+ float* costs;
+ int* x0;
+ int* y0;
+ int* x1;
+ int* y1;
+ // Retrieve Tesseract internal data to compute the quality score.
+ int n = TessBaseAPI::TesseractExtractResult(&string, &lengths, &costs, &x0, &y0, &x1, &y1, page_res);
+
+ float average_uncertainty = 0.f;
+ for (int i = 0; i < n; ++i)
+ average_uncertainty += costs[i];
+
+ if (n)
+ *score = average_uncertainty / n;
+ else
+ *score = -1;
+
+ // Some memory cleanup
+ delete[] string;
+ delete[] lengths;
+ delete[] costs;
+ delete[] x0;
+ delete[] y0;
+ delete[] x1;
+ delete[] y1;
+
+ return TesseractToText(page_res);
+ }
+ }
+};
+
+// Call tesseract
+// lang: expected language
+template <typename T>
+char* tesseract(const char* lang, const mln::image2d<T>& input, float* score)
+{
+ assert(!score);
+ TessWrap::InitWithLanguage(NULL, NULL, lang, NULL, false, 0, NULL);
+ char* s = TessWrap::TesseractRect(
+ (unsigned char*) input.buffer(),
+ sizeof (T),
+ input.ncols() * sizeof (T),
+ 0, 0,
+ input.ncols(),
+ input.nrows(),
+ score);
+ return s;
+}
+
+
+#endif // ! TESSERACT_WRAP_HH
Index: branches/cleanup-2008/milena/sandbox/garrigues/ocr/ocr_with_preprocess.cc
===================================================================
--- branches/cleanup-2008/milena/sandbox/garrigues/ocr/ocr_with_preprocess.cc (revision 2759)
+++ branches/cleanup-2008/milena/sandbox/garrigues/ocr/ocr_with_preprocess.cc (revision 2760)
@@ -57,28 +57,24 @@
#include <mln/io/pbm/save.hh>
#include <mln/logical/not.hh>
-#include <tesseract/baseapi.h>
-
+#include "tesseract_wrap.hh"
// _COMPILATION_
// g++ -DNDEBUG -O3 -I../../.. ocr.cc -L/usr/lib -ltesseract_full -lpthread
-
-// Call tesseract
-// lang: expected language
-template <typename T>
-char* tesseract(const char* lang, const mln::image2d<T>& input)
-{
- TessBaseAPI::InitWithLanguage(NULL, NULL, lang, NULL, false, 0, NULL);
- char* s = TessBaseAPI::TesseractRect(
- (unsigned char*) input.buffer(),
- sizeof (T),
- input.ncols() * sizeof (T),
- 0, 0,
- input.ncols(),
- input.nrows());
- return s;
+#if 0
+# define TEST(Var) \
+ { \
+ image2d<int_u8> tmp = clone(cast_image<int_u8>(Var)); \
+ float score = 0.f; \
+ char* s = tesseract("fra", tmp, &score); \
+ std::cerr << #Var << ": " << score << std::endl << s; \
+ delete[] s; \
}
+#else
+# define TEST(Var)
+#endif
+
int main(int argc, char** argv)
{
@@ -96,20 +92,23 @@
mln::border::thickness = 0;
io::pbm::load(input, argv[1]);
+ TEST(input);
// Resize
- std::cerr << "Enlarge the image" << std::endl;
+ //std::cerr << "Enlarge the image" << std::endl;
image2d<int_u8> enlarged = enlarge(logical::not_(input), 2);
//image2d<bool> enlarged = geom::resize(logical::not_(input), 4);
- io::pgm::save(enlarged, "1_enlage.pgm");
+ io::pgm::save(enlarged, "1_enlarge.pgm");
+ TEST(enlarged);
// Blur.
- std::cerr << "Blur the enlarged image" << std::endl;
+ //std::cerr << "Blur the enlarged image" << std::endl;
// image2d<int_u8> blur = linear::gaussian(fun::p2v::ternary(pw::value(enlarged), pw::cst(int_u8(255)), pw::cst(int_u8(0))) | enlarged.domain(),
// 4);
- image2d<int_u8> blur = linear::gaussian(enlarged, 1);
+ image2d<int_u8> blur = linear::gaussian(clone(enlarged), 1);
io::pgm::save(blur, "2_gaussian.pgm");
+ TEST(blur);
// Crest.
// image2d<bool> c = crest(enlarged, blur, c4());
@@ -120,7 +119,7 @@
// Threshold
image2d<bool> binary;
{
- std::cerr << "Threshold the blur image" << std::endl;
+ //std::cerr << "Threshold the blur image" << std::endl;
// // Compute the histogram.
// histo::data<int_u8> h = histo::compute(blur);
@@ -141,26 +140,35 @@
binary(p) = blur(p) > 100;
io::pbm::save(binary, "3_threshold.pbm");
+ TEST(binary);
}
// Skeleton
- std::cerr << "Compute the skeleton" << std::endl;
+ //std::cerr << "Compute the skeleton" << std::endl;
image2d<bool> skel = skeleton(binary, 4);
io::pbm::save(skel, "4_skeleton.pbm");
+ TEST(skel);
// Dilation
- std::cerr << "Dilate the skeleton" << std::endl;
+ //std::cerr << "Dilate the skeleton" << std::endl;
win::octagon2d oct(7);
for (unsigned i = 0; i < 1; i++)
skel = morpho::dilation(skel, oct);
io::pbm::save(skel, "5_dilation.pbm");
+ TEST(skel);
io::pbm::save(skel, argv[2]);
- std::cerr << "Text recognition" << std::endl;
- char* s = tesseract("fra", clone(logical::not_(skel)));
- std::cerr << "Tesseract result:"<< std::endl;
+ //std::cerr << "Text recognition" << std::endl;
+ //char* s = tesseract("fra", clone(logical::not_(skel)));
+ {
+ image2d<int_u8> tmp = clone(cast_image<int_u8>(skel));
+ float score = 0;
+ char* s = tesseract("fra", tmp, &score);
+ std::cerr << "Tesseract result: (score " << score << ")" << std::endl;
std::cout << s;
- free(s);
+ delete[] s;
+ }
+
}
1
0