r3338: Add test file for IGR

URL: https://svn.lrde.epita.fr/svn/oln/trunk/milena/sandbox ChangeLog: 2009-02-10 Fabien Freling <fabien.freling@lrde.epita.fr> Add test file for IGR. * fabien/igr/dump2ppm.cc: New tool to convert dump file to ppm. * fabien/igr/igr.cc: New test file for IRM dump. --- dump2ppm.cc | 35 +++++++++++++ igr.cc | 153 ++++++++++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 153 insertions(+), 35 deletions(-) Index: trunk/milena/sandbox/fabien/igr/igr.cc =================================================================== --- trunk/milena/sandbox/fabien/igr/igr.cc (revision 3337) +++ trunk/milena/sandbox/fabien/igr/igr.cc (revision 3338) @@ -26,12 +26,27 @@ // reasons why the executable file might be covered by the GNU General // Public License. +#include <mln/core/concept/image.hh> +#include <mln/core/concept/neighborhood.hh> + #include <mln/core/image/image2d.hh> +#include <mln/core/image/image3d.hh> + #include <mln/value/int_u8.hh> +#include <mln/value/rgb8.hh> + #include <mln/io/pgm/all.hh> #include <mln/io/pbm/all.hh> +#include <mln/io/ppm/all.hh> +#include <mln/io/cloud/all.hh> + #include <mln/core/alias/neighb2d.hh> +#include <mln/core/alias/neighb3d.hh> #include <mln/labeling/flat_zones.hh> +#include <mln/literal/colors.hh> +#include <mln/norm/l1.hh> + +#include <mln/io/dump/all.hh> #include <mln/labeling/blobs.hh> #include <mln/labeling/compute.hh> @@ -39,64 +54,132 @@ #include <mln/fun/v2b/threshold.hh> #include <mln/level/transform.hh> #include <mln/accu/count.hh> -#include <mln/value/label_8.hh> +#include <mln/accu/center.hh> +#include <mln/set/compute.hh> +#include <mln/value/label_16.hh> #include <mln/data/fill.hh> #include <mln/pw/all.hh> +#include <mln/morpho/elementary/gradient_internal.hh> //FIXME: remove #include <mln/essential/2d.hh> +#include <iostream> +#include <mln/debug/println.hh> -//struct threshold : Function_v2v<threshold> -//{ -// typedef bool result; -// bool operator() (int_u8 val) const {if (val < 25) return false; return true;} -//}; -int main() +template <typename I, typename N, typename L> +mln_ch_value(I, mln::value::rgb8) +igr(const mln::Image<I>& input_, const mln::Neighborhood<N>& nbh_, L& nlabels) { using namespace mln; using value::int_u8; - using value::label_8; + using value::rgb8; - trace::quiet = false; + const I& input = exact(input_); + const N& nbh = exact(nbh_); - image2d<int_u8> src; - io::pgm::load(src, "img/slice_7.pgm"); + // Threshold. - image2d<bool> threshold = level::transform(src, fun::v2b::threshold<int_u8>(25)); + mln_ch_value(I, bool) threshold = level::transform(input, fun::v2b::threshold<int_u8>(25)); - label_8 n; - image2d<label_8> labels = labeling::flat_zones(threshold, c4(), n); + // Labeling. + + mln_ch_value(I, L) labels = labeling::flat_zones(threshold, nbh, nlabels); accu::count<int_u8> a_; - util::array<unsigned> a = labeling::compute(a_, src, labels, n); + util::array<unsigned> a = labeling::compute(a_, threshold, labels, nlabels); + + // We keep the third and second biggest object. - mln_ch_value_(image2d<int_u8>, bool) biggest; - initialize(biggest, src); - data::fill(biggest, false); - unsigned x = 0; - unsigned y = 0; - unsigned z = 0; + mln_ch_value(I, bool) big_second; + initialize(big_second, input); + data::fill(big_second, false); + unsigned big_third_count = 0; + unsigned big_third_lbl = 0; + unsigned big_second_count = 0; + unsigned big_second_lbl = 0; + unsigned big_first_count = 0; + unsigned big_first_lbl = 0; for (int i = 0; i < a.nelements(); ++i) { - if (a[i] > x) - x = a[i]; - if (x > y) + if (a[i] > big_third_count) { - int swap = y; - y = x; - x = swap; + big_third_count = a[i]; + big_third_lbl = i; } - if (y > z) + if (big_third_count > big_second_count) + { + int swap = big_second_count; + int swap_lbl = big_second_lbl; + big_second_count = big_third_count; + big_second_lbl = big_third_lbl; + big_third_count = swap; + big_third_lbl = swap_lbl; + } + if (big_second_count > big_first_count) + { + int swap = big_first_count; + int swap_lbl = big_first_lbl; + big_first_count = big_second_count; + big_first_lbl = big_second_lbl; + big_second_count = swap; + big_second_lbl = swap_lbl; + } + } + mln_VAR(big_third, threshold | pw::value(labels) == big_third_lbl); + + data::fill((big_second | pw::value(labels) == big_second_lbl).rw(), true); + + // Gradient. + + mln_ch_value(I, bool) gradient = morpho::elementary::gradient_internal(big_second, nbh); + mln_VAR(gradient_map, gradient | pw::value(gradient) == true); + + mln_ch_value(I, rgb8) result = level::convert(rgb8(), input); + data::fill((result | gradient_map.domain()).rw(), literal::red); + + // Center. + + accu::center<mln_site(I)> center_; + mln_site(I) center = set::compute(center_, big_third.domain()); + result(center) = literal::red; + + // Distance. + + mln_fwd_piter(gradient_map_t) p(gradient_map.domain()); + p_array<mln_site(I)> arr; + for_all(p) + { + if (mln::norm::l1_distance(p.to_site().to_vec(), center.to_vec()) < 200) { - int swap = z; - z = y; - y = swap; + result(p) = literal::green; + + arr.append(p); + } } + + // Save the cloud in a file. + + std::cout << "Nbr sites = " << arr.nsites() << std::endl; + io::cloud::save(arr, "cloud.txt"); + + return result; } - data::fill((biggest | pw::value(labels) == x).rw(), true); - data::fill((biggest | pw::value(labels) == y).rw(), true); - // gradient_internal, center +int main() +{ + using namespace mln; + using value::int_u8; + using value::label_16; + + trace::quiet = false; + +// image2d<int_u8> src; +// io::pgm::load(src, "img/slice_7.pgm"); + image3d<int_u8> vol; + io::dump::load(vol, "img/IRM.dump"); + + label_16 nlabels; - io::pbm::save(biggest, "out.pgm"); +// io::ppm::save(igr(src, c4(), nlabels), "out.ppm"); + io::dump::save(igr(vol, c6(), nlabels), "out.raw"); } Index: trunk/milena/sandbox/fabien/igr/dump2ppm.cc =================================================================== --- trunk/milena/sandbox/fabien/igr/dump2ppm.cc (revision 0) +++ trunk/milena/sandbox/fabien/igr/dump2ppm.cc (revision 3338) @@ -0,0 +1,35 @@ + +#include <mln/core/image/image2d.hh> +#include <mln/make/image3d.hh> +#include <mln/debug/slices_2d.hh> + +#include <mln/value/rgb8.hh> +#include <mln/io/dump/load.hh> +#include <mln/io/ppm/save.hh> + +#include <mln/literal/colors.hh> + + +void usage(char* argv[]) +{ + std::cerr << "usage: " << argv[0] << " input.dump output.ppm" << std::endl; + abort(); +} + + + +int main(int argc, char* argv[]) +{ + using namespace mln; + using value::rgb8; + + if (argc != 3) + usage(argv); + + image3d<rgb8> vol; + io::dump::load(vol, argv[1]); + + rgb8 bg = literal::black; + image2d<rgb8> ima = debug::slices_2d(vol, 1.f, bg); + io::ppm::save(ima, argv[2]); +}
participants (1)
-
Fabien Freling