https://svn.lrde.epita.fr/svn/oln/branches/cleanup-2008/milena/sandbox
Index: ChangeLog
from Ugo Jardonnet <ugo.jardonnet(a)lrde.epita.fr>
Fix Update and Revamp Classif.
* inim/classif/max_tree.hh,
* inim/classif/display.hh,
* inim/classif/iccvg04.cc,
* inim/classif/proj.hh,
* inim/classif/v2.cc: Moved and update...
* inim/classif/src/max_tree.hh,
* inim/classif/src/display.hh,
* inim/classif/src/iccvg04.cc,
* inim/classif/src/proj.hh,
* inim/classif/src/v2.cc: ...here.
* inim/classif/Makefile: .
Makefile | 11 +--
src/iccvg04.cc | 175 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
src/proj.hh | 83 +++++++++++++++++++++++++++
3 files changed, 263 insertions(+), 6 deletions(-)
Index: inim/classif/src/iccvg04.cc
--- inim/classif/src/iccvg04.cc (revision 0)
+++ inim/classif/src/iccvg04.cc (revision 0)
@@ -0,0 +1,175 @@
+#include <iostream>
+
+#include <mln/core/image/image2d.hh>
+#include <mln/core/image/image3d.hh>
+#include <mln/histo/data.hh>
+#include <mln/value/all.hh>
+
+#include <mln/level/fill.hh>
+
+#include <mln/morpho/closing_volume.hh>
+#include <mln/morpho/closing_area.hh>
+#include <mln/morpho/opening_volume.hh>
+#include <mln/morpho/opening_area.hh>
+
+#include <mln/arith/revert.hh>
+#include <mln/morpho/meyer_wst.hh>
+#include <mln/core/alias/neighb3d.hh>
+#include <mln/util/array.hh>
+#include <mln/labeling/compute.hh>
+
+#include <mln/geom/nrows.hh>
+#include <mln/geom/ncols.hh>
+#include <mln/geom/nslis.hh>
+
+#include <mln/io/ppm/load.hh>
+#include <mln/io/pgm/save.hh>
+#include <mln/io/ppm/save.hh>
+
+#include <mln/estim/min_max.hh>
+#include <mln/algebra/vec.hh>
+#include <mln/algebra/vec.hh>
+
+#include <mln/literal/all.hh>
+
+#include <mln/level/stretch.hh>
+
+#include <sys/stat.h>
+#include <sstream>
+#include <string.h>
+#include <stdlib.h>
+
+#include "proj.hh"
+#include "display.hh"
+
+using namespace mln;
+
+unsigned max = 0;
+
+template <typename I>
+mln::image3d<unsigned>
+fill_histo(const I& ima, int f)
+{
+ const value::int_u8 v = 255 / f; // FIXME
+ image3d<unsigned> histo(v,v,v);
+ level::fill(histo, 0);
+ unsigned i = 0;
+
+ mln_piter(I) p(ima.domain());
+ for_all(p)
+ {
+ point3d p3(ima(p).red() / f, ima(p).green() / f, ima(p).blue() / f);
+ histo(p3)++;
+ }
+ return histo;
+}
+
+template <typename I>
+void gplot(const I& ima)
+{
+ mln_piter(I) p(ima.domain());
+ for_all(p)
+ {
+ if (ima(p) != 0)
+ std::cout << p[0] << ' ' << p[1] << ' '
<< p[2] << std::endl;
+ }
+}
+
+template <typename I, typename J, typename K>
+void
+classify_image(const I& ima, const J& histo, const K& ws, int nbasins, int
f)
+{
+ unsigned border = 0;
+ unsigned count[nbasins + 1];
+ memset(count, 0, (nbasins + 1) * sizeof (unsigned));
+
+ algebra::vec<3, double> sum[nbasins + 1];
+ for (int i = 0; i < nbasins + 1; ++i)
+ sum[i] = literal::zero;
+
+ // Compute representatives of every class
+ mln_piter(I) p(ima.domain());
+ for_all(p)
+ {
+ point3d p3(ima(p).red() / f, ima(p).green() / f, ima(p).blue() / f);
+ int w = ws(p3);
+
+ //check if we are not on a border of the WS
+ if (w == 0)
+ border++;
+ count[w] += histo(p3);
+ sum[w] += histo(p3) * convert::to< algebra::vec<3, value::int_u8> >(p3);
+
+ std::cerr << "p3 : " << p3 << " == " <<
+ convert::to<algebra::vec<3, value::int_u8> >(p3) << std::endl;
+ }
+
+ std::cout << border << std::endl;
+ for (int i = 0; i < nbasins + 1; ++i)
+ {
+ std::cout << "sum[" << i << "] = " <<
sum[i] * f << " / " << count[i] << " == ";
+ sum[i] = (sum[i] * f) / count[i];
+ std::cout << sum[i] << std::endl;
+ }
+
+ // Make an output image where colors are replaced by their representatives.
+ mln_piter(I) pi(ima.domain());
+ I out(ima.domain());
+ for_all(pi)
+ {
+ //retrieve color class
+ value::rgb8 coul = ima(pi);
+ int w = ws(point3d(coul.red() / f, coul.green() / f, coul.blue() / f));
+
+ //if w == 0, out(pi) = 0 ie is part of a border of the watershed
+ if (w == 0)
+ out(pi) = literal::red;
+ else
+ out(pi) = convert::to<value::rgb8>(sum[w]);
+
+ std::cerr << "out(" << pi << ") = sum["
<< w << "]; //"
+ << sum[w] << " : rgb8(" << sum[w] <<
")" << std::endl;
+ }
+
+ io::ppm::save(out, "out.ppm");
+ save_class(histo, ws, sum, "palette.ppm");
+}
+
+bool usage(int argc, char ** argv)
+{
+ if (argc != 4)
+ {
+ std::cout << "usage: " << argv[0] << " image
div_factor lambda" << std::endl;
+ return false;
+ }
+ return true;
+}
+
+
+int main(int argc, char **argv)
+{
+ if (not usage(argc, argv))
+ return 1;
+ const int div_factor = atoi(argv[2]);
+ const int lambda = atoi(argv[3]);
+
+ image2d<value::rgb8> ima;
+ ima = io::ppm::load<value::rgb8>(argv[1]);
+
+ //make histo
+ image3d<unsigned> histo = fill_histo(ima,div_factor);
+
+ //compute opening_volume of histo
+ image3d<unsigned> histo_filtered(histo.domain());
+ morpho::opening_volume(histo, c6(), lambda, histo_filtered);
+
+
+ //watershed over histo_closure
+ unsigned nbasins = 0;
+ image3d<unsigned> ws = morpho::meyer_wst(arith::revert(histo_filtered),
+ c6(), nbasins);
+ std::cout << "nbassins : " << nbasins << std::endl;
+
+ //classify image
+ classify_image(ima, histo, ws, nbasins, div_factor);
+}
Index: inim/classif/src/proj.hh
--- inim/classif/src/proj.hh (revision 0)
+++ inim/classif/src/proj.hh (revision 0)
@@ -0,0 +1,83 @@
+// Copyright (C) 2008 EPITA Research and Development Laboratory
+//
+// 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_PROJ_HH
+# define MLN_PROJ_HH
+
+#include <mln/io/ppm/save.hh>
+#include <mln/io/pgm/save.hh>
+#include <mln/level/paste.hh>
+#include <mln/accu/mean.hh>
+#include <mln/accu/maj_h.hh>
+#include <mln/literal/white.hh>
+
+namespace mln
+{
+
+ template <typename T, typename A>
+ image2d<mln_result(A)>
+ proj(const image3d<T>& input, A a)
+ {
+ image2d<A> acc(geom::nslis(input), geom::nrows(input));
+ mln_piter(image3d<T>) p(input.domain());
+ for_all(p) // 3d
+ if (input(p) != literal::zero)
+ acc.at(p.sli(), p.row()).take(input(p));
+
+ image2d<mln_result(A)> output(acc.domain());
+ level::paste(acc, output);
+ return output;
+ }
+
+ template <typename T, typename U, typename K>
+ void
+ save_class(const image3d<T>& histo, const image3d<U>& ws,
+ K mean, const char * fn)
+ {
+ accu::maj_h<value::int_u8> max_1;
+ image2d<value::int_u8> hproj = proj(histo, max_1);
+
+ accu::maj_h<value::int_u8> max_2;
+ image2d<value::int_u8> proj_class = proj(ws, max_2);
+
+ //std::cout << histo;
+
+ image2d<value::rgb8> out(proj_class.domain());
+
+ level::fill(out, literal::white);
+ mln_piter(image2d<value::int_u8>) p(hproj.domain());
+ for_all(p)
+ if (hproj(p) > 0)
+ out(p) = convert::to<value::rgb8>(mean[proj_class(p)]);
+
+ io::ppm::save(out, fn);
+ }
+
+} // end of namespace mln
+
+#endif /* MLN_PROJ_HH */
+
Index: inim/classif/Makefile
--- inim/classif/Makefile (revision 2824)
+++ inim/classif/Makefile (working copy)
@@ -2,7 +2,7 @@
# ICCVG
ICCVG_INCLUDES=-I../../..
-ICCVG_SRC=iccvg04.cc
+ICCVG_SRC=src/iccvg04.cc
ICCVG=iccvg
ICCVG_DBG=iccvg_dbg
@@ -10,10 +10,9 @@
DIV?=4
LAMBDA?=10
-
# V2
V2_INCLUDES=-I../../..
-V2_SRC=v2.cc max_tree.hh
+V2_SRC=src/v2.cc src/max_tree.hh
V2=v2
V2_DBG=v2_dbg
@@ -44,7 +43,7 @@
rm -f $(V2)
rm -f $(V2_DBG)
rm -f *.log
- rm -f .dep_iccvg .dep_v2
+ rm -f .dep_iccvg .dep_v2 .dep_iccvgr .dep_v2r
check-debug: $(ICCVG_DBG)
./iccvg_dbg $(IMG) $(DIV) $(LAMBDA) $(LOG)
@@ -74,12 +73,12 @@
.dep_iccvg:
- g++ $(ICCVG_INCLUDES) -MM iccvg04.cc > $@
+ g++ $(ICCVG_INCLUDES) -MM src/iccvg04.cc > $@
@sed -ir s/iccvg04.cc// .dep_iccvg
@sed -ir s/iccvg04.o/iccvg/ .dep_iccvg
.dep_v2:
- g++ $(V2_INCLUDES) -MM v2.cc > $@
+ g++ $(V2_INCLUDES) -MM src/v2.cc > $@
@sed -ir s/v2.cc// .dep_v2
@sed -ir s/v2.o/v2/ .dep_v2