Commit e48f7d72 authored by Baptiste Esteban's avatar Baptiste Esteban
Browse files

Add a cut algorithm

parent a118f79a
Pipeline #27754 canceled with stages
#pragma once
#include <mln/morpho/component_tree.hpp>
#include <vector>
namespace mln::morpho
{
template <typename V, typename Nodemap, typename L>
auto horizontal_cut_labelization_from(const component_tree<V>& t, Nodemap nm, V th, const std::vector<L>& vals);
/******************
* Implementation *
******************/
template <typename V, typename Nodemap, typename L>
auto horizontal_cut_labelization_from(const component_tree<V>& t, Nodemap nm, V th, const std::vector<L>& vals)
{
static_assert(mln::is_a_v<Nodemap, mln::details::Image>);
static_assert(std::is_same_v<image_value_t<Nodemap>, int>);
assert(vals.size() == t.parent.size());
image_ch_value_t<Nodemap, L> lbl = imchvalue<L>(nm);
image_concrete_t<Nodemap> cut_nm = imconcretize(nm);
mln_foreach(auto p, nm.domain())
{
cut_nm(p) = nm(p);
while (t.parent[cut_nm(p)] != cut_nm(p) && t.values[t.parent[cut_nm(p)]] <= th)
cut_nm(p) = t.parent[cut_nm(p)];
}
mln_foreach(auto px, cut_nm.pixels())
lbl(px.point()) = vals[px.val()];
return lbl;
}
} // namespace mln::morpho
\ No newline at end of file
link_libraries(${FreeImage_LIBRARIES})
add_core_test(${test_prefix}alphatree alphatree.cpp)
#include <mln/morpho/alphatree.hpp>
#include <mln/morpho/cut.hpp>
#include <mln/accu/accumulators/mean.hpp>
#include <mln/core/neighborhood/c4.hpp>
#include <mln/core/image/ndimage.hpp>
#include <fixtures/ImageCompare/image_compare.hpp>
#include <gtest/gtest.h>
#include <mln/io/imprint.hpp>
TEST(Morpho, AlphaTreeCutMeanLabelization)
{
mln::image2d<std::uint8_t> ima = {
{1, 1, 5},
{2, 5, 6},
{0, 1, 4}
};
mln::image2d<std::uint8_t> cut_1 = {
{1, 1, 5},
{1, 5, 5},
{0, 0, 4}
};
mln::image2d<std::uint8_t> cut_2 = {
{1, 1, 5},
{1, 5, 5},
{1, 1, 5}
};
mln::image2d<std::uint8_t> cut_3 = {
{2, 2, 2},
{2, 2, 2},
{2, 2, 2}
};
auto [t, nm] = mln::morpho::alphatree(ima, mln::c4);
auto val = t.compute_attribute_on_values(nm, ima, mln::accu::accumulators::mean<std::uint8_t, std::uint8_t>());
ASSERT_IMAGES_EQ_EXP(mln::morpho::horizontal_cut_labelization_from(t, nm, 0, val), ima);
ASSERT_IMAGES_EQ_EXP(mln::morpho::horizontal_cut_labelization_from(t, nm, 1, val), cut_1);
ASSERT_IMAGES_EQ_EXP(mln::morpho::horizontal_cut_labelization_from(t, nm, 2, val), cut_2);
ASSERT_IMAGES_EQ_EXP(mln::morpho::horizontal_cut_labelization_from(t, nm, 3, val), cut_3);
}
TEST(Morpho, AlphaTreeCutRangeLabelization)
{
mln::image2d<std::uint8_t> ima = {
{1, 1, 5},
{2, 5, 6},
{0, 1, 4}
};
mln::image2d<std::uint8_t> cut_1 = {
{1, 1, 5},
{1, 5, 5},
{0, 0, 4}
};
mln::image2d<std::uint8_t> cut_2 = {
{1, 1, 5},
{1, 5, 5},
{1, 1, 5}
};
mln::image2d<std::uint8_t> cut_3 = {
{2, 2, 2},
{2, 2, 2},
{2, 2, 2}
};
auto [t, nm] = mln::morpho::alphatree(ima, mln::c4);
auto val = t.compute_attribute_on_values(nm, ima, mln::accu::accumulators::mean<std::uint8_t, std::uint8_t>());
ASSERT_IMAGES_EQ_EXP(mln::morpho::horizontal_cut_labelization_from(t, nm, 0, val), ima);
ASSERT_IMAGES_EQ_EXP(mln::morpho::horizontal_cut_labelization_from(t, nm, 1, val), cut_1);
ASSERT_IMAGES_EQ_EXP(mln::morpho::horizontal_cut_labelization_from(t, nm, 2, val), cut_2);
ASSERT_IMAGES_EQ_EXP(mln::morpho::horizontal_cut_labelization_from(t, nm, 3, val), cut_3);
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment