r4226: Add some utils to handle objects detection with mean color attribute

URL: https://svn.lrde.epita.fr/svn/oln/trunk/milena/sandbox ChangeLog: 2009-07-03 Edwin Carlinet <carlinet@lrde.epita.fr> Add some utils to handle objects detection with mean color attribute. * edwin/exec/give_me_the_curve.cc, * edwin/exec/plot.conf: Util to trace attribute evolution along the branch. * edwin/exec/mask_colorize.cc: Color labeling from a mask image. * edwin/exec/max_delta_colormean_obj.cc: Retrieve components that maximes the distance between the mean color of components. * edwin/mln/morpho/tree/propagate.hh: Minor fixes. --- exec/give_me_the_curve.cc | 92 ++++++++++++++++++++++++++++++++++++++++ exec/mask_colorize.cc | 78 +++++++++++++++++++++++++++++++++ exec/max_delta_colormean_obj.cc | 18 ++++++- exec/plot.conf | 3 + mln/morpho/tree/propagate.hh | 5 +- 5 files changed, 192 insertions(+), 4 deletions(-) Index: trunk/milena/sandbox/edwin/exec/plot.conf =================================================================== --- trunk/milena/sandbox/edwin/exec/plot.conf (revision 0) +++ trunk/milena/sandbox/edwin/exec/plot.conf (revision 4226) @@ -0,0 +1,3 @@ +set terminal png; +set output "plot.png" +plot "data.txt" Index: trunk/milena/sandbox/edwin/exec/give_me_the_curve.cc =================================================================== --- trunk/milena/sandbox/edwin/exec/give_me_the_curve.cc (revision 0) +++ trunk/milena/sandbox/edwin/exec/give_me_the_curve.cc (revision 4226) @@ -0,0 +1,92 @@ +#include <mln/core/image/image2d.hh> +#include <mln/core/alias/neighb2d.hh> + +#include <mln/morpho/tree/data.hh> +#include <mln/data/sort_psites.hh> + +#include <mln/io/pgm/load.hh> + +#include <iostream> +#include <fstream> +#include <cstdlib> +#include <cstring> +#include <unistd.h> + +void usage(char** argv) +{ + + std::cout << "Usage: " << argv[0] << " in:source.pgm in:attribute.pgm [out:data.txt]" + << std::endl + << "Compute the tree from 'source' and display the values of" + << " 'attribute' along the branch." + << std::endl; + abort(); +} + + +int main(int argc, char** argv) +{ + using namespace mln; + using value::int_u8; + + if (argc < 3) + usage(argv); + + const char * finput = argv[1]; + const char * fattribute = argv[2]; + const char * fdata = (argc >= 4) ? argv[3] : "data.txt"; + + // Image loadin'. + typedef image2d<value::int_u8> I; + I input, a; + io::pgm::load(input, finput); + io::pgm::load(a, fattribute); + + // Tree construction. + typedef p_array<mln_psite_(I)> S; + typedef morpho::tree::data<I, S> T; + + S s = data::sort_psites_decreasing(input); + T tree(input, s, c4()); + + // Display branches + + while (true) + { + unsigned x, y; + + std::cout << "x y: "; + std::cout.flush(); + std::cin >> x; + std::cin >> y; + + if (std::cin.fail()) + { + std::cout << "Parse error." + << std::endl; + continue; + } + + mln_psite_(T) p(x, y); + std::ofstream data(fdata); + + while (!tree.is_root(p)) + { + data << a(p) << std::endl; + p = tree.parent(p); + } + data.close(); + + system("gnuplot plot.conf"); + if (fork() == 0) + { + setsid(); + //umask(0); + execlp("display", "display", "plot.png", 0); + exit(0); + } + } +} + + + Index: trunk/milena/sandbox/edwin/exec/mask_colorize.cc =================================================================== --- trunk/milena/sandbox/edwin/exec/mask_colorize.cc (revision 0) +++ trunk/milena/sandbox/edwin/exec/mask_colorize.cc (revision 4226) @@ -0,0 +1,78 @@ +#include <mln/core/image/image2d.hh> +#include <mln/core/alias/neighb2d.hh> + +#include <mln/value/int_u8.hh> +#include <mln/value/int_u16.hh> +#include <mln/value/rgb8.hh> + +#include <mln/io/pbm/load.hh> +#include <mln/io/pgm/load.hh> +#include <mln/io/ppm/load.hh> +#include <mln/io/ppm/save.hh> + +#include <mln/labeling/colorize.hh> +#include <mln/labeling/blobs.hh> + +using namespace mln; +using value::int_u16; +using value::int_u8; +using value::rgb8; + +bool check_extension(const char* filename, const char* extension) +{ + return strncmp(filename + strlen(filename) - strlen(extension), + extension, strlen(extension)) == 0; +} + +void usage(char** argv) +{ + std::cout << "Usage: " << argv[0] << " in:mask.pgm in:source.(pgm|ppm) [out:colorize.ppm]" + << std::endl; + abort(); +} + +template <typename M, typename I> +void +process(const Image<M>& mask, const Image<I>& ref, const char* foutput) +{ + int_u16 nlabels; + + mln_ch_value(I, int_u16) labels = labeling::blobs(mask, c4(), nlabels); + + typedef mln_ch_value(I, rgb8) O; + O out = labeling::colorize(rgb8(), labels, nlabels); + + io::ppm::save(out, foutput); +} + + +int main(int argc, char** argv) +{ + if (argc < 3) + usage(argv); + + const char* finput = argv[1]; + const char* freference = argv[2]; + const char* foutput = (argc >= 4) ? argv[3] : "colorize.ppm"; + + image2d<bool> input; + io::pbm::load(input, finput); + + if (check_extension(freference, ".pgm")) + { + image2d<int_u8> ref; + io::pgm::load(ref, freference); + process(input, ref, foutput); + } + else if (check_extension(freference, ".ppm")) + { + image2d<rgb8> ref; + io::ppm::load(ref, freference); + process(input, ref, foutput); + } + else + usage(argv); +} + + + Index: trunk/milena/sandbox/edwin/exec/max_delta_colormean_obj.cc =================================================================== --- trunk/milena/sandbox/edwin/exec/max_delta_colormean_obj.cc (revision 4225) +++ trunk/milena/sandbox/edwin/exec/max_delta_colormean_obj.cc (revision 4226) @@ -33,6 +33,7 @@ #include <mln/morpho/tree/compute_attribute_image.hh> #include <mln/morpho/tree/components.hh> #include <mln/morpho/tree/propagate.hh> +#include <mln/morpho/tree/propagate_representative.hh> #include <mln/accu/stat/mean.hh> @@ -42,7 +43,10 @@ #include <mln/io/pgm/load.hh> #include <mln/io/ppm/load.hh> #include <mln/io/pbm/save.hh> +#include <mln/io/pgm/save.hh> +#include <mln/io/ppm/save.hh> +#include <mln/math/diff_abs.hh> #include "color_distance.hh" #include <iostream> @@ -81,13 +85,18 @@ O out = morpho::tree::compute_attribute_image_from(accu::stat::mean<mln_value(S)>(), tree, source); + io::pgm::save(data::convert(value::int_u8(), out), "mean.pgm"); + // Compute delta image. mln_ch_value(typename T::function, int_u8) dist; initialize(dist, tree.f()); mln_up_node_piter(T) n(tree); for_all(n) - dist(n) = out(tree.parent(n)) - out(n); + { + mln_assertion(tree.f(tree.parent(n)) > tree.f(n)); + dist(n) = math::diff_abs(out(tree.parent(n)), out(n)); + } return dist; } @@ -102,6 +111,8 @@ O out = morpho::tree::compute_attribute_image_from(accu::stat::mean<mln_value(S)>(), tree, source); + io::ppm::save(data::convert(value::rgb8(), out), "mean.ppm"); + // Compute delta image. mln_ch_value(typename T::function, int_u8) dist; initialize(dist, tree.f()); @@ -147,7 +158,7 @@ typedef p_array<mln_psite_(I)> S; typedef morpho::tree::data<I, S> T; - S s = data::sort_psites_increasing(input); + S s = data::sort_psites_decreasing(input); T tree(input, s, c4()); // Attribute computation. @@ -169,6 +180,9 @@ else usage(argv); // Type not handled. + morpho::tree::propagate_representative(tree, delta); + io::pgm::save(delta, "distance.pgm"); + // Get the max components of the delta image. p_array<mln_psite_(I)> obj = morpho::tree::get_components(tree, delta); Index: trunk/milena/sandbox/edwin/mln/morpho/tree/propagate.hh =================================================================== --- trunk/milena/sandbox/edwin/mln/morpho/tree/propagate.hh (revision 4225) +++ trunk/milena/sandbox/edwin/mln/morpho/tree/propagate.hh (revision 4226) @@ -56,13 +56,14 @@ */ template <typename A, typename T> inline - A propagate_components(const Image<A>& attr_image, + mln_concrete(A) + propagate_components(const Image<A>& attr_image, const T& tree, const p_array< mln_psite(A) >& component_list, const mln_value(A)& null) { const A& a = exact(attr_image); - A out; + mln_concrete(A) out; initialize(out, a); mln::data::fill(out, null);
participants (1)
-
Edwin Carlinet