core/filter.hh
Go to the documentation of this file.
/* -*- mia-c++ -*-
*
* This file is part of MIA - a toolbox for medical image analysis
* Copyright (c) Leipzig, Madrid 1999-2017 Gert Wollny
*
* MIA is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 MIA; if not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef mia_core_filter_hh
#define mia_core_filter_hh
#include <cassert>
#include <stdexcept>
#include <mia/core/pixeltype.hh>
#include <mia/core/product_base.hh>
#include <mia/core/factory.hh>
#include <mia/core/import_handler.hh>
#include <cinttypes>
NS_MIA_BEGIN
struct EXPORT_CORE combiner_type {
static const char *type_descr;
};
struct EXPORT_CORE filter_type {
static const char *type_descr;
};
#define DC(T, D) dynamic_cast<const T&>(D)
#define DV(T, D) dynamic_cast<T&>(D)
template <typename R>
struct TFilter {
typedef R result_type;
};
template <class D>
class EXPORT_HANDLER TDataFilter: public TFilter< std::shared_ptr<D >>, public CProductBase
{
public:
typedef D plugin_data;
typedef filter_type plugin_type;
typedef D Image;
typedef std::shared_ptr<TDataFilter<D>> Pointer;
typedef typename TFilter< std::shared_ptr<D >>::result_type result_type;
virtual ~TDataFilter();
result_type filter(const Image& image) const;
result_type filter(std::shared_ptr<D> pimage) const;
std::set<EPixelType> test_pixeltype_conversion(const std::set<EPixelType>& in_types) const;
private:
virtual result_type do_filter(const Image& image) const = 0;
virtual result_type do_filter(std::shared_ptr<D> image) const;
virtual std::set<EPixelType> do_test_pixeltype_conversion(const std::set<EPixelType>& in_type) const;
};
template <class D>
class EXPORT_HANDLER TDataFilterChained: public TDataFilter<D>
{
typedef typename TDataFilter<D>::result_type result_type;
public:
typedef typename TDataFilter<D>::Pointer Pointer;
void push_back(Pointer f)
{
m_chain.push_back(f);
}
private:
virtual result_type do_filter(const D& image) const
{
assert(m_chain.size() > 0);
cvdebug() << "Run chained filter '" << m_chain[0]->get_init_string() << "'\n";
result_type result = m_chain[0]->filter(image);
for (auto f = m_chain.begin() + 1; f != m_chain.end(); ++f) {
cvdebug() << "Run chained filter '" << (*f)->get_init_string() << "'\n";
result = (*f)->filter(*result);
}
return result;
}
std::set<EPixelType> do_test_pixeltype_conversion(const std::set<EPixelType>& in_type) const
{
std::set<EPixelType> result = in_type;
for (auto f : m_chain) {
result = f->test_pixeltype_conversion(result);
}
return result;
}
std::vector<Pointer> m_chain;
};
template <class Image>
class EXPORT_HANDLER TDataFilterPlugin: public TFactory<TDataFilter<Image>>
{
public:
typedef typename TFactory<TDataFilter<Image>>::Product Product;
TDataFilterPlugin(char const *const name):
TFactory<TDataFilter<Image>>(name)
{}
};
template <typename D>
struct plugin_can_chain<TDataFilterPlugin<D>> {
static constexpr bool value = true;
typedef TDataFilterChained<D> Chained;
};
template <template <class> class D>
struct __bind_all {
typedef D<bool> Dbool;
typedef D<int8_t> Dsc;
typedef D<uint8_t> Duc;
typedef D<int16_t> Dss;
typedef D<uint16_t> Dus;
typedef D<int32_t> Dsi;
typedef D<uint32_t> Dui;
typedef D<int64_t> Dsl;
typedef D<uint64_t> Dul;
typedef D<float> Dfloat;
typedef D<double> Ddouble;
};
template <typename B>
struct Binder {
};
template <typename F, typename B>
static typename F::result_type filter(const F& f, const B& b)
{
typedef typename Binder<B>::Derived D;
switch (b.get_pixel_type()) {
case it_bit:
return f(DC(typename D::Dbool, b));
case it_sbyte:
return f(DC(typename D::Dsc, b));
case it_ubyte:
return f(DC(typename D::Duc, b));
case it_sshort:
return f(DC(typename D::Dss, b));
case it_ushort:
return f(DC(typename D::Dus, b));
case it_sint:
return f(DC(typename D::Dsi, b));
case it_uint:
return f(DC(typename D::Dui, b));
case it_slong:
return f(DC(typename D::Dsl, b));
case it_ulong:
return f(DC(typename D::Dul, b));
case it_float:
return f(DC(typename D::Dfloat, b));
case it_double:
return f(DC(typename D::Ddouble, b));
default:
assert(!"unsupported pixel type in image");
throw std::invalid_argument("mia::filter: unsupported pixel type in image");
}
}
template <typename F, typename B>
static typename F::result_type filter_inplace(const F& f, B& b)
{
typedef typename Binder<B>::Derived D;
switch (b.get_pixel_type()) {
case it_bit:
return f(DV(typename D::Dbool, b));
case it_sbyte:
return f(DV(typename D::Dsc, b));
case it_ubyte:
return f(DV(typename D::Duc, b));
case it_sshort:
return f(DV(typename D::Dss, b));
case it_ushort:
return f(DV(typename D::Dus, b));
case it_sint:
return f(DV(typename D::Dsi, b));
case it_uint:
return f(DV(typename D::Dui, b));
case it_slong:
return f(DV(typename D::Dsl, b));
case it_ulong:
return f(DV(typename D::Dul, b));
case it_float:
return f(DV(typename D::Dfloat, b));
case it_double:
return f(DV(typename D::Ddouble, b));
default:
assert(!"unsupported pixel type in image");
throw std::invalid_argument("mia::filter: unsupported pixel type in image");
}
}
template <typename F, typename B>
static typename F::result_type accumulate(F& f, const B& data)
{
typedef typename Binder<B>::Derived D;
switch (data.get_pixel_type()) {
case it_bit:
return f(DC(typename D::Dbool, data));
case it_sbyte:
return f(DC(typename D::Dsc, data));
case it_ubyte:
return f(DC(typename D::Duc, data));
case it_sshort:
return f(DC(typename D::Dss, data));
case it_ushort:
return f(DC(typename D::Dus, data));
case it_sint:
return f(DC(typename D::Dsi, data));
case it_uint:
return f(DC(typename D::Dui, data));
case it_slong:
return f(DC(typename D::Dsl, data));
case it_ulong:
return f(DC(typename D::Dul, data));
case it_float:
return f(DC(typename D::Dfloat, data));
case it_double:
return f(DC(typename D::Ddouble, data));
default:
assert(!"unsupported pixel type in image");
throw std::invalid_argument("mia::filter: unsupported pixel type in image");
}
}
template <typename F, typename INOUT, typename IN>
static typename F::result_type _combine_inplace(const F& f, INOUT& inout, const IN& in)
{
typedef typename Binder<IN>::Derived D;
switch (in.get_pixel_type()) {
case it_bit:
return f(inout, DC(typename D::Dbool, in));
case it_sbyte:
return f(inout, DC(typename D::Dsc, in));
case it_ubyte:
return f(inout, DC(typename D::Duc, in));
case it_sshort:
return f(inout, DC(typename D::Dss, in));
case it_ushort:
return f(inout, DC(typename D::Dus, in));
case it_sint:
return f(inout, DC(typename D::Dsi, in));
case it_uint:
return f(inout, DC(typename D::Dui, in));
case it_slong:
return f(inout, DC(typename D::Dsl, in));
case it_ulong:
return f(inout, DC(typename D::Dul, in));
case it_float:
return f(inout, DC(typename D::Dfloat, in));
case it_double:
return f(inout, DC(typename D::Ddouble, in));
default:
assert(!"unsupported pixel type in image");
throw std::invalid_argument("mia::filter: unsupported pixel type in image");
}
}
template <typename F, typename INOUT, typename IN>
static typename F::result_type combine_inplace(const F& f, INOUT& inout, const IN& in)
{
typedef typename Binder<INOUT
>::Derived D;
switch (inout.get_pixel_type()) {
case it_bit:
return _combine_inplace(f, DV(typename D::Dbool, inout), in);
case it_sbyte:
return _combine_inplace(f, DV(typename D::Dsc, inout), in);
case it_ubyte:
return _combine_inplace(f, DV(typename D::Duc, inout), in);
case it_sshort:
return _combine_inplace(f, DV(typename D::Dss, inout), in);
case it_ushort:
return _combine_inplace(f, DV(typename D::Dus, inout), in);
case it_sint:
return _combine_inplace(f, DV(typename D::Dsi, inout), in);
case it_uint:
return _combine_inplace(f, DV(typename D::Dui, inout), in);
case it_slong:
return _combine_inplace(f, DV(typename D::Dsl, inout), in);
case it_ulong:
return _combine_inplace(f, DV(typename D::Dul, inout), in);
case it_float:
return _combine_inplace(f, DV(typename D::Dfloat, inout), in);
case it_double:
return _combine_inplace(f, DV(typename D::Ddouble, inout), in);
default:
assert(!"unsupported pixel type in image");
throw std::invalid_argument("mia::filter: unsupported pixel type in image");
}
}
template <typename F, typename B>
static typename F::result_type filter_equal(const F& f, const B& a, const B& b)
{
assert(a.get_pixel_type() == b.get_pixel_type());
typedef typename Binder<B>::Derived D;
switch (a.get_pixel_type()) {
case it_bit:
return f(DC(typename D::Dbool, a), DC(typename D::Dbool, b));
case it_sbyte:
return f( DC(typename D::Dsc, a), DC(typename D::Dsc, b));
case it_ubyte:
return f( DC(typename D::Duc, a), DC(typename D::Duc, b));
case it_sshort:
return f( DC(typename D::Dss, a), DC(typename D::Dss, b));
case it_ushort:
return f( DC(typename D::Dus, a), DC(typename D::Dus, b));
case it_sint:
return f( DC(typename D::Dsi, a), DC(typename D::Dsi, b));
case it_uint:
return f( DC(typename D::Dui, a), DC(typename D::Dui, b));
case it_slong:
return f( DC(typename D::Dsl, a), DC(typename D::Dsl, b));
case it_ulong:
return f( DC(typename D::Dul, a), DC(typename D::Dul, b));
case it_float:
return f( DC(typename D::Dfloat, a), DC(typename D::Dfloat, b));
case it_double:
return f( DC(typename D::Ddouble, a), DC(typename D::Ddouble, b));
default:
assert(!"unsupported pixel type in image");
throw std::invalid_argument("mia::filter: unsupported pixel type in image");
}
}
template <typename F, typename B>
static void filter_equal_inplace(const F& f, const B& a, B& b)
{
assert(a.get_pixel_type() == b.get_pixel_type());
typedef typename Binder<B>::Derived D;
switch (a.get_pixel_type()) {
case it_bit:
f(DC(typename D::Dbool, a), DV(typename D::Dbool, b));
break;
case it_sbyte:
f( DC(typename D::Dsc, a), DV(typename D::Dsc, b));
break;
case it_ubyte:
f( DC(typename D::Duc, a), DV(typename D::Duc, b));
break;
case it_sshort:
f( DC(typename D::Dss, a), DV(typename D::Dss, b));
break;
case it_ushort:
f( DC(typename D::Dus, a), DV(typename D::Dus, b));
break;
case it_sint:
f( DC(typename D::Dsi, a), DV(typename D::Dsi, b));
break;
case it_uint:
f( DC(typename D::Dui, a), DV(typename D::Dui, b));
break;
case it_slong:
f( DC(typename D::Dsl, a), DV(typename D::Dsl, b));
break;
case it_ulong:
f( DC(typename D::Dul, a), DV(typename D::Dul, b));
break;
case it_float:
f( DC(typename D::Dfloat, a), DV(typename D::Dfloat, b));
break;
case it_double:
f( DC(typename D::Ddouble, a), DV(typename D::Ddouble, b));
break;
default:
assert(!"unsupported pixel type in image");
throw std::invalid_argument("mia::filter: unsupported pixel type in image");
}
}
template <typename F, typename B, typename O>
static typename F::result_type filter_and_output(const F& f, const B& a, O& b)
{
typedef typename Binder<B>::Derived D;
switch (a.get_pixel_type()) {
case it_bit:
return f(DC(typename D::Dbool, a), b);
break;
case it_sbyte:
return f(DC(typename D::Dsc, a), b);
break;
case it_ubyte:
return f(DC(typename D::Duc, a), b);
break;
case it_sshort:
return f(DC(typename D::Dss, a), b);
break;
case it_ushort:
return f(DC(typename D::Dus, a), b);
break;
case it_sint:
return f(DC(typename D::Dsi, a), b);
break;
case it_uint:
return f(DC(typename D::Dui, a), b);
break;
case it_slong:
return f(DC(typename D::Dsl, a), b);
break;
case it_ulong:
return f(DC(typename D::Dul, a), b);
break;
case it_float:
return f(DC(typename D::Dfloat, a), b);
break;
case it_double:
return f(DC(typename D::Ddouble, a), b);
break;
default:
assert(!"unsupported pixel type in image");
throw std::invalid_argument("mia::filter_and_output: unsupported pixel type in image");
}
}
template <typename F, typename A, typename B>
static typename F::result_type _filter(const F& f, const A& a, const B& b)
{
typedef typename Binder<A>::Derived D;
switch (a.get_pixel_type()) {
case it_bit:
return f(DC(typename D::Dbool, a), b);
case it_sbyte:
return f(DC(typename D::Dsc, a), b);
case it_ubyte:
return f(DC(typename D::Duc, a), b);
case it_sshort:
return f(DC(typename D::Dss, a), b);
case it_ushort:
return f(DC(typename D::Dus, a), b);
case it_sint:
return f(DC(typename D::Dsi, a), b);
case it_uint:
return f(DC(typename D::Dui, a), b);
case it_slong:
return f(DC(typename D::Dsl, a), b);
case it_ulong:
return f(DC(typename D::Dul, a), b);
case it_float:
return f(DC(typename D::Dfloat, a), b);
case it_double:
return f(DC(typename D::Ddouble, a), b);
default:
assert(!"unsupported pixel type in image");
throw std::invalid_argument("mia::filter: unsupported pixel type in image");
}
}
template <typename F, typename A, typename B>
static typename F::result_type filter(const F& f, const A& a, const B& b)
{
typedef typename Binder<B>::Derived D;
switch (b.get_pixel_type()) {
case it_bit:
return _filter(f, a, DC(typename D::Dbool, b));
case it_sbyte:
return _filter(f, a, DC(typename D::Dsc, b));
case it_ubyte:
return _filter(f, a, DC(typename D::Duc, b));
case it_sshort:
return _filter(f, a, DC(typename D::Dss, b));
case it_ushort:
return _filter(f, a, DC(typename D::Dus, b));
case it_sint:
return _filter(f, a, DC(typename D::Dsi, b));
case it_uint:
return _filter(f, a, DC(typename D::Dui, b));
case it_slong:
return _filter(f, a, DC(typename D::Dsl, b));
case it_ulong:
return _filter(f, a, DC(typename D::Dul, b));
case it_float:
return _filter(f, a, DC(typename D::Dfloat, b));
case it_double:
return _filter(f, a, DC(typename D::Ddouble, b));
default:
assert(!"unsupported pixel type in image");
throw std::invalid_argument("mia::filter: unsupported pixel type in image");
}
}
template <typename F, typename A, typename B>
static typename F::result_type _accumulate(F& f, const A& a, const B& b)
{
typedef typename Binder<A>::Derived D;
switch (a.get_pixel_type()) {
case it_bit:
return f(DC(typename D::Dbool, a), b);
case it_sbyte:
return f(DC(typename D::Dsc, a), b);
case it_ubyte:
return f(DC(typename D::Duc, a), b);
case it_sshort:
return f(DC(typename D::Dss, a), b);
case it_ushort:
return f(DC(typename D::Dus, a), b);
case it_sint:
return f(DC(typename D::Dsi, a), b);
case it_uint:
return f(DC(typename D::Dui, a), b);
case it_slong:
return f(DC(typename D::Dsl, a), b);
case it_ulong:
return f(DC(typename D::Dul, a), b);
case it_float:
return f(DC(typename D::Dfloat, a), b);
case it_double:
return f(DC(typename D::Ddouble, a), b);
default:
assert(!"unsupported pixel type in image");
throw std::invalid_argument("mia::filter: unsupported pixel type in image");
}
}
template <typename F, typename A, typename B>
static typename F::result_type accumulate(F& f, const A& a, const B& b)
{
typedef typename Binder<B>::Derived D;
switch (b.get_pixel_type()) {
case it_bit:
return _accumulate(f, a, DC(typename D::Dbool, b));
case it_sbyte:
return _accumulate(f, a, DC(typename D::Dsc, b));
case it_ubyte:
return _accumulate(f, a, DC(typename D::Duc, b));
case it_sshort:
return _accumulate(f, a, DC(typename D::Dss, b));
case it_ushort:
return _accumulate(f, a, DC(typename D::Dus, b));
case it_sint:
return _accumulate(f, a, DC(typename D::Dsi, b));
case it_uint:
return _accumulate(f, a, DC(typename D::Dui, b));
case it_slong:
return _accumulate(f, a, DC(typename D::Dsl, b));
case it_ulong:
return _accumulate(f, a, DC(typename D::Dul, b));
case it_float:
return _accumulate(f, a, DC(typename D::Dfloat, b));
case it_double:
return _accumulate(f, a, DC(typename D::Ddouble, b));
default:
assert(!"unsupported pixel type in image");
throw std::invalid_argument("mia::accumulate: unsupported pixel type in image");
}
}
#undef DC
#undef DV
template <class D>
TDataFilter<D>::~TDataFilter()
{
}
template <class D>
typename TDataFilter<D>::result_type
TDataFilter<D>::filter(const D& image) const
{
return do_filter(image);
}
template <class D>
typename TDataFilter<D>::result_type
TDataFilter<D>::filter(std::shared_ptr<D> pimage) const
{
return do_filter(pimage);
}
template <class D>
typename TDataFilter<D>::result_type
TDataFilter<D>::do_filter(std::shared_ptr<D> pimage) const
{
return do_filter(*pimage);
}
template <class D>
std::set<EPixelType>
TDataFilter<D>::test_pixeltype_conversion(const std::set<EPixelType>& in_types) const
{
return do_test_pixeltype_conversion(in_types);
}
template <class D>
std::set<EPixelType>
TDataFilter<D>::do_test_pixeltype_conversion(const std::set<EPixelType>& in_types) const
{
return in_types;
}
NS_MIA_END
#endif
CProductBase
The base class for all plug-in created object.
Definition product_base.hh:41
TDataFilterChained
Definition core/filter.hh:133
TDataFilterChained::push_back
void push_back(Pointer f)
Definition core/filter.hh:138
TDataFilterChained::Pointer
TDataFilter< D >::Pointer Pointer
Definition core/filter.hh:136
TDataFilterPlugin
Generic image filter plugin base.
Definition core/filter.hh:186
TDataFilterPlugin::TDataFilterPlugin
TDataFilterPlugin(char const *const name)
Constructor that sets the plug-in name.
Definition core/filter.hh:190
TDataFilterPlugin::Product
TFactory< TDataFilter< Image > >::Product Product
Definition core/filter.hh:188
TDataFilter
Generic interface class to data filters.
Definition core/filter.hh:87
TDataFilter::filter
result_type filter(const Image &image) const
Definition core/filter.hh:925
TDataFilter::~TDataFilter
virtual ~TDataFilter()
Definition core/filter.hh:919
TDataFilter::filter
result_type filter(std::shared_ptr< D > pimage) const
Definition core/filter.hh:932
TDataFilter::plugin_type
filter_type plugin_type
plugin handler helper type
Definition core/filter.hh:93
TDataFilter::plugin_data
D plugin_data
plugin handler helper type
Definition core/filter.hh:91
TDataFilter::Pointer
std::shared_ptr< TDataFilter< D > > Pointer
pointer type of the data filtered by this filter
Definition core/filter.hh:99
TDataFilter::test_pixeltype_conversion
std::set< EPixelType > test_pixeltype_conversion(const std::set< EPixelType > &in_types) const
Definition core/filter.hh:947
TDataFilter::result_type
TFilter< std::shared_ptr< D > >::result_type result_type
result type of this filter
Definition core/filter.hh:102
TDataFilter::Image
D Image
defines the image type handled by the image filter
Definition core/filter.hh:96
TFactory< TDataFilter< Image > >::TFactory
TFactory(char const *const name)
Definition factory.hh:173
EXPORT_HANDLER
#define EXPORT_HANDLER
Definition core/cost.hh:32
_combine_inplace
static F::result_type _combine_inplace(const F &f, INOUT &inout, const IN &in)
Definition core/filter.hh:416
combine_inplace
static F::result_type combine_inplace(const F &f, INOUT &inout, const IN &in)
Definition core/filter.hh:461
NS_MIA_BEGIN
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition defines.hh:33
EXPORT_CORE
#define EXPORT_CORE
Macro to manage Visual C++ style dllimport/dllexport.
Definition defines.hh:101
NS_MIA_END
#define NS_MIA_END
conveniance define to end the mia namespace
Definition defines.hh:36
factory.hh
filter_equal_inplace
static void filter_equal_inplace(const F &f, const B &a, B &b)
Definition core/filter.hh:578
filter_and_output
static F::result_type filter_and_output(const F &f, const B &a, O &b)
Definition core/filter.hh:647
filter
static F::result_type filter(const F &f, const B &b)
Definition core/filter.hh:258
filter_equal
static F::result_type filter_equal(const F &f, const B &a, const B &b)
Definition core/filter.hh:520
filter_inplace
static F::result_type filter_inplace(const F &f, B &b)
Definition core/filter.hh:315
accumulate
static F::result_type accumulate(F &f, const B &data)
Definition core/filter.hh:371
import_handler.hh
cvdebug
CDebugSink & cvdebug()
Definition msgstream.hh:226
pixeltype.hh
it_slong
@ it_slong
Definition pixeltype.hh:40
it_ushort
@ it_ushort
Definition pixeltype.hh:37
it_float
@ it_float
Definition pixeltype.hh:42
it_ubyte
@ it_ubyte
Definition pixeltype.hh:35
it_uint
@ it_uint
Definition pixeltype.hh:39
it_double
@ it_double
Definition pixeltype.hh:43
it_ulong
@ it_ulong
Definition pixeltype.hh:41
it_sbyte
@ it_sbyte
Definition pixeltype.hh:34
it_sshort
@ it_sshort
Definition pixeltype.hh:36
it_sint
@ it_sint
Definition pixeltype.hh:38
it_bit
@ it_bit
Definition pixeltype.hh:33
product_base.hh
type_descr
const char *const TSparseSolver< F >::type_descr
Definition sparse_solver.hh:141
TFilter
base class for all filer type functors.
Definition core/filter.hh:70
TFilter::result_type
R result_type
defines the return type of the filter function
Definition core/filter.hh:72
plugin_can_chain< TDataFilterPlugin< D > >::value
static constexpr bool value
Definition core/filter.hh:197
plugin_can_chain< TDataFilterPlugin< D > >::Chained
TDataFilterChained< D > Chained
Definition core/filter.hh:198
plugin_can_chain
Definition plugin_base.hh:210