https://svn.lrde.epita.fr/svn/oln/branches/cleanup-2008/milena/sandbox
Index: ChangeLog
from Thierry Geraud <thierry.geraud(a)lrde.epita.fr>
Add demo code for Laurent N.
* geraud/toto: Remove.
* geraud/laurent: New.
* geraud/laurent/wst2d.cc: New.
* geraud/laurent/wst3d.cc: New.
* geraud/laurent/classif.cc: New.
* geraud/laurent/segmentation.hh: New.
classif.cc | 115 +++++++++++++++++++++++++++++++++++++++++
segmentation.hh | 14 +++++
wst2d.cc | 154 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
wst3d.cc | 79 ++++++++++++++++++++++++++++
4 files changed, 362 insertions(+)
Index: geraud/laurent/wst2d.cc
--- geraud/laurent/wst2d.cc (revision 0)
+++ geraud/laurent/wst2d.cc (revision 0)
@@ -0,0 +1,154 @@
+# include <cstdlib>
+
+# include <mln/value/int_u8.hh>
+# include <mln/value/rgb8.hh>
+
+# include <mln/io/pgm/load.hh>
+# include <mln/io/ppm/save.hh>
+
+# include <mln/core/var.hh>
+# include <mln/core/image/image2d.hh>
+# include <mln/core/alias/neighb2d.hh>
+# include <mln/make/double_neighb2d.hh>
+
+# include <mln/level/transform.hh>
+# include <mln/literal/black.hh>
+# include <mln/debug/println.hh>
+
+# include <mln/morpho/closing_area.hh>
+# include <mln/morpho/gradient.hh>
+# include <mln/accu/min_max.hh>
+# include <mln/morpho/meyer_wst.hh>
+
+
+using namespace mln;
+using value::int_u8;
+
+
+template <typename I, typename N>
+mln_concrete(I)
+morpho_gradient(const Image<I>& input_, const Neighborhood<N>& nbh_)
+{
+ const I& input = exact(input_);
+ const N& nbh = exact(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.take_as_init(input(p));
+ for_all(n)
+ mm.take(input(n));
+ output(p) = mm.second() - mm.first();
+ }
+
+ return output;
+}
+
+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;
+};
+
+
+bool is_chess(const point2d& p)
+{
+ return p.col() % 2 == p.row() % 2;
+}
+
+
+void usage(char* argv[])
+{
+ std::cerr << "usage: " << argv[0] << " input.pgm nbh
lambda output.ppm" << std::endl;
+ std::cerr << " nbh in {4, 6, 8}; lambda >= 0" << std::endl;
+ abort();
+}
+
+
+template <typename I, typename N>
+void do_it(const I& ima,
+ const N& nbh,
+ int lambda,
+ const std::string& filename)
+{
+ I grad = morpho_gradient(ima, nbh);
+
+ I clo;
+ if (lambda > 1)
+ clo = morpho::closing_area(grad, nbh, lambda);
+ else
+ clo = grad;
+
+ unsigned l;
+ mln_ch_value(I, unsigned) wst = morpho::meyer_wst(clo, nbh, l);
+
+ std::cout << "nbasins = " << l << std::endl;
+// debug::println(labeling::regional_minima(clo, nbh, l));
+// debug::println(wst);
+
+ io::ppm::save(level::transform(wst, colorize(l)), filename);
+}
+
+
+int main(int argc, char* argv[])
+{
+ if (argc != 5)
+ usage(argv);
+
+ image2d<int_u8> ima;
+
+ io::pgm::load(ima, argv[1]);
+
+ int nbh_ = atoi(argv[2]);
+ if (! (nbh_ == 4 || nbh_ == 6 || nbh_ == 8))
+ usage(argv);
+
+ int lambda = atoi(argv[3]);
+ if (lambda < 0)
+ usage(argv);
+
+ std::string filename(argv[4]);
+
+ if (nbh_ == 4)
+ {
+ mln_VAR(nbh, c4());
+ do_it(ima, nbh, lambda, filename);
+ }
+ else if (nbh_ == 8)
+ {
+ mln_VAR(nbh, c8());
+ do_it(ima, nbh, lambda, filename);
+ }
+ else if (nbh_ == 6)
+ {
+ bool vert[] = { 1, 1, 0,
+ 1, 0, 1,
+ 0, 1, 1 };
+
+ bool hori[] = { 0, 1, 1,
+ 1, 0, 1,
+ 1, 1, 0 };
+
+ mln_VAR(nbh, make::double_neighb2d(is_chess, vert, hori));
+ do_it(ima, nbh, lambda, filename);
+ }
+}
Index: geraud/laurent/wst3d.cc
--- geraud/laurent/wst3d.cc (revision 0)
+++ geraud/laurent/wst3d.cc (revision 0)
@@ -0,0 +1,79 @@
+# include <cstdlib>
+# include <cstdio>
+
+# include <mln/value/int_u16.hh>
+# include <mln/value/int_u8.hh>
+# include <mln/level/stretch.hh>
+
+# include <mln/io/pgm/load.hh>
+# include <mln/io/ppm/save.hh>
+
+# include <mln/core/var.hh>
+# include <mln/core/image/image3d.hh>
+# include <mln/core/alias/neighb3d.hh>
+
+# include <mln/morpho/meyer_wst.hh>
+# include <mln/morpho/closing_area.hh>
+# include <mln/morpho/closing_volume.hh>
+
+
+
+using namespace mln;
+using value::int_u8;
+
+
+void usage(char* argv[])
+{
+ std::cerr << "usage: " << argv[0] << " input.raw
lambda output.ppm" << std::endl;
+ std::cerr << " lambda >= 0" << std::endl;
+ abort();
+}
+
+
+
+int main(int argc, char* argv[])
+{
+ using namespace mln;
+ using value::int_u8;
+ using value::int_u16;
+
+ if (argc != 3)
+ usage(argv);
+
+ trace::quiet = false;
+
+ image3d<int_u16> ima16(256, 256, 256, 0);
+ int size = 256 * 256 * 256 * 2;
+
+ std::FILE* file = std::fopen(argv[1], "r");
+ std::fread(ima16.buffer(), size, 1, file);
+
+// typedef image3d<int_u16> I;
+// mln_piter_(I) p(ima16.domain());
+// for_all(p)
+// if (ima16(p) != 0)
+// std::cout << ima16(p) << std::endl;
+
+// abort();
+
+ int_u16 min_, max_;
+ estim::min_max(ima16, min_, max_);
+ std::cout << min_ << ' ' << max_ << std::endl;
+
+ abort();
+
+ image3d<int_u8> ima(ima16.domain());
+ level::stretch(ima16, ima);
+ ima16.destroy();
+
+ int lambda = atoi(argv[2]);
+ if (lambda < 0)
+ usage(argv);
+
+ image3d<int_u8> clo(ima.domain());
+ morpho::closing_volume(ima, c6(), lambda, clo);
+
+ unsigned nbasins;
+ image3d<unsigned> wst = morpho::meyer_wst(clo, c6(), nbasins);
+ std::cout << "nbasins = " << nbasins << std::endl;
+}
Index: geraud/laurent/classif.cc
--- geraud/laurent/classif.cc (revision 0)
+++ geraud/laurent/classif.cc (revision 0)
@@ -0,0 +1,115 @@
+#include <vector>
+#include <mln/core/image/image2d.hh>
+#include <mln/core/routine/clone.hh>
+#include <mln/core/alias/neighb2d.hh>
+#include <mln/core/site_set/p_queue_fast.hh>
+#include <mln/labeling/blobs.hh>
+#include <mln/io/pbm/load.hh>
+#include <mln/debug/println.hh>
+
+
+namespace mln
+{
+
+ template <typename I, typename N>
+ mln_concrete(I)
+ influence_zones(const I& input, const N& nbh)
+ {
+ mln_concrete(I) output = clone(input);
+
+ p_queue_fast<mln_site(I)> q;
+
+ // Init.
+ {
+ mln_piter(I) p(input.domain());
+ mln_niter(N) n(nbh, p);
+ for_all(p)
+ if (input(p) != 0)
+ for_all(n) if (input.has(n))
+ if (input(n) == 0)
+ {
+ q.push(p);
+ break;
+ }
+ }
+
+ // Body.
+ {
+ mln_site(I) p;
+ mln_niter(N) n(nbh, p);
+ while (! q.is_empty())
+ {
+ p = q.pop_front();
+ mln_invariant(output(p) != 0);
+ for_all(n) if (input.has(n))
+ if (output(n) == 0)
+ {
+ output(n) = output(p);
+ q.push(n);
+ }
+ }
+ }
+
+ return output;
+
+ } // IZ
+
+
+ template <typename I>
+ void
+ mk_graph(const I& iz, unsigned nlabels)
+ {
+ std::vector< std::vector<bool> > adj(nlabels + 1);
+ for (unsigned l = 1; l <= nlabels; ++l)
+ adj[l].resize(l, false);
+
+ mln_piter(I) p(iz.domain());
+ for_all(p)
+ {
+ mln_site(I) r = p + right, b = p + down;
+ if (iz.has(r) && iz(p) != iz(r))
+ {
+ if (iz(p) < iz(r))
+ adj[iz(p)][iz(r)] = true;
+ else
+ adj[iz(r)][iz(p)] = true;
+ }
+ if (iz.has(b) && iz(p) != iz(b))
+ {
+ if (iz(p) < iz(b))
+ adj[iz(p)][iz(b)] = true;
+ else
+ adj[iz(b)][iz(p)] = true;
+ }
+ }
+
+ for (unsigned l1 = 1; l1 <= nlabels; ++l1)
+ for (unsigned l2 = 1; l2 <= nlabels; ++l2)
+ if (adj[l1][l2])
+ std::cout << l1 << ' ' << l2 << std::endl;
+ }
+
+}
+
+
+
+int main()
+{
+ using namespace mln;
+
+ border::thickness = 0;
+
+ image2d<bool> seeds;
+ io::pbm::load(seeds, "+seeds.pbm");
+
+ debug::println(seeds);
+
+ unsigned n;
+ image2d<unsigned> label = labeling::blobs(seeds, c4(), n);
+ debug::println(label);
+
+ image2d<unsigned> iz = influence_zones(label, c4());
+ debug::println(iz);
+
+ mk_graph(iz, n);
+}
Index: geraud/laurent/segmentation.hh
--- geraud/laurent/segmentation.hh (revision 0)
+++ geraud/laurent/segmentation.hh (revision 0)
@@ -0,0 +1,14 @@
+
+
+template <typename I, typename N>
+mln_ch_value(I, rgb8) segmentation(const I& ima,
+ const N& nbh,
+ unsigned area)
+{
+ mln_concrete(I) filtered = morpho::closing_area(ima, nbh, area);
+
+ unsigned nbasins;
+ mln_ch_value(I, unsigned) wst = morpho::wst_meyer(filtered, nbh, nbasins);
+
+ return level::transform(wst, colorize(nbasins));
+}