Commit dc84877e authored by Thierry Geraud's avatar Thierry Geraud
Browse files

Add weighted windows and related tools + a generic convolution.

	* tests/convolve.cc: New.
	* tests/w_window2d_int.cc: New.
	* mln/core/w_window.hh: New.
	* mln/core/concept/weighted_window.hh: New.
	* mln/core/w_window2d_int.hh: New.
	* mln/math/round.hh: New.
	* mln/linear: New.
	* mln/linear/convolve.hh: New.
	* mln/make/w_window2d.hh: New.
	* mln/make/w_window2d_int.hh: New.
	* mln/core/dpoints_piter.hh: Change accessibility.
	* mln/core/internal/pixel_impl.hh
	(address_): Remove preconditions.
	* mln/core/image2d_b.hh: Update include.


git-svn-id: https://svn.lrde.epita.fr/svn/oln/trunk@1044 4aad255d-cdde-0310-9447-f3009e2ae8c0
parent 9096ba3e
2007-07-27 Thierry Geraud <thierry.geraud@lrde.epita.fr>
Add weighted windows and related tools + a generic convolution.
* tests/convolve.cc: New.
* tests/w_window2d_int.cc: New.
* mln/core/w_window.hh: New.
* mln/core/concept/weighted_window.hh: New.
* mln/core/w_window2d_int.hh: New.
* mln/math/round.hh: New.
* mln/linear: New.
* mln/linear/convolve.hh: New.
* mln/make/w_window2d.hh: New.
* mln/make/w_window2d_int.hh: New.
* mln/core/dpoints_piter.hh: Change accessibility.
* mln/core/internal/pixel_impl.hh
(address_): Remove preconditions.
* mln/core/image2d_b.hh: Update include.
2007-07-26 Thierry Geraud <thierry.geraud@lrde.epita.fr>
Add a queue of points.
......
// Copyright (C) 2007 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_CORE_CONCEPT_WEIGHTED_WINDOW_HH
# define MLN_CORE_CONCEPT_WEIGHTED_WINDOW_HH
/*! \file mln/core/concept/weighted_window.hh
*
* \brief Definition of the concept of mln::Weighted_Window.
*/
# include <mln/core/concept/object.hh>
# include <mln/core/concept/iterator.hh>
namespace mln
{
/*! \brief Base class for implementation classes that are
* weighted_windows.
*
* \see mln::doc::Weighted_Window for a complete documentation of
* this class contents.
*/
template <typename E>
struct Weighted_Window : public Object<E>
{
/*
typedef point;
typedef dpoint;
typedef fwd_qiter;
typedef bkd_qiter;
E sym_() const;
*/
bool is_empty() const
{
return exact(this)->window().is_empty();
}
bool is_centered() const
{
return exact(this)->window().is_centered();
}
bool is_symmetric() const
{
return exact(this)->window().is_symmetric();
}
unsigned delta() const
{
return exact(this)->window().delta();
}
protected:
Weighted_Window();
};
/*! \brief Compute the symmetrical weighted_window of \p rhs.
*
* \relates mln::Weighted_Window
*/
template <typename W>
W operator-(const Weighted_Window<W>& rhs);
# ifndef MLN_INCLUDE_ONLY
template <typename E>
Weighted_Window<E>::Weighted_Window()
{
typedef mln_point(E) point;
typedef mln_dpoint(E) dpoint;
typedef mln_fwd_qiter(E) fwd_qiter;
typedef mln_bkd_qiter(E) bkd_qiter;
E (E::*m_)() const = & E::sym_;
m_ = 0;
}
template <typename W>
W operator-(const Weighted_Window<W>& rhs)
{
return exact(rhs).sym_();
}
# endif // ! MLN_INCLUDE_ONLY
} // end of namespace mln
#endif // ! MLN_CORE_CONCEPT_WEIGHTED_WINDOW_HH
......@@ -102,7 +102,8 @@ namespace mln
/// account that its center point may have moved.
void update();
private:
protected:
const std::vector<D>& dps_;
const point& p_ref_; // reference point (or "center point")
......
......@@ -482,6 +482,7 @@ namespace mln
# include <mln/core/trait/pixter.hh>
# include <mln/core/dpoints_pixter.hh>
# include <mln/core/pixter2d_b.hh>
# include <mln/core/w_window.hh>
namespace mln
......
......@@ -173,7 +173,6 @@ namespace mln
mln_value(I) **
pixel_impl_<I, E>::address_() const
{
mln_precondition(is_valid_());
return (value**)(& this->value_ptr_);
}
......@@ -206,7 +205,6 @@ namespace mln
const mln_value(I) **
pixel_impl_<const I, E>::address_() const
{
mln_precondition(is_valid_());
return (const value**)(& this->value_ptr_);
}
......
// Copyright (C) 2007 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_CORE_W_WINDOW_HH
# define MLN_CORE_W_WINDOW_HH
/*! \file mln/core/w_window.hh
*
* \brief Definition of the generic weighted window class mln::w_window_.
*/
# include <map>
# include <mln/core/concept/weighted_window.hh>
# include <mln/core/window.hh>
namespace mln
{
// Fwd decl.
template <typename It, typename W> struct with_w_;
/*! \brief Generic w_window class.
*
* This type of w_window is just like a set of delta-points. The
* parameter \c D is the type of delta-points; the parameter \c W is
* the type of weights.
*/
template <typename D, typename W>
struct w_window_ : public Weighted_Window< w_window_<D,W> >
{
/// Point associated type.
typedef mln_point(D) point;
/// Dpoint associated type.
typedef D dpoint;
/// Point_Iterator type to browse the points of a generic w_window.
typedef with_w_< dpoints_fwd_piter<D>, W > fwd_qiter;
/// Point_Iterator type to browse the points of a generic w_window.
typedef with_w_< dpoints_bkd_piter<D>, W > bkd_qiter;
/// Constructor without argument.
w_window_();
/// Give the corresponding window.
const window_<D>& window() const;
/// Give the symmetrical w_window.
w_window_<D,W> sym_() const;
/// Give the \p i-th weight.
W weight(unsigned i) const;
/// Give access to the vector of weights.
const std::vector<W>& weights() const;
/// Give access to the vector of delta-points.
const std::vector<D>& vect() const;
/// Insert a delta-point \p d and its associated weight \p w.
w_window_<D,W>& insert(const D& d, const W& w);
/// Give the number of elements (pairs of delta-point/weight).
unsigned nelements() const;
// FIXME: Rename as dpoint(i); same in mln/core/dpoints_pixter.hh:174.
const D& element(unsigned i) const;
protected:
window_<D> win_;
std::vector<W> wei_;
};
/// Print a weighted window \p wwin into an output stream \p ostr.
template <typename D, typename W>
std::ostream& operator<<(std::ostream& ostr, const w_window_<D,W>& wwin);
/// Decorator to handle weights for iterators on window points.
template <typename It, typename W>
struct with_w_ : public It
{
template <typename Ds, typename P>
with_w_(const Ds& ds, const P& p);
W weight() const;
protected:
const std::vector<W> wei_;
};
# ifndef MLN_INCLUDE_ONLY
// with_w_<It,W>
template <typename It, typename W>
template <typename Ds, typename P>
with_w_<It,W>::with_w_(const Ds& ds,
const P& p)
: It(ds, p),
wei_(ds.weights())
{
}
template <typename It, typename W>
W
with_w_<It,W>::weight() const
{
mln_precondition(this->i_ < wei_.size());
return wei_[this->i_];
}
// w_window_<D,W>
template <typename D, typename W>
std::ostream& operator<<(std::ostream& ostr, const w_window_<D,W>& wwin)
{
ostr << '[';
for (unsigned i = 0; i < wwin.window().nelements(); ++i)
ostr << wwin.vect()[i] << ':' << wwin.weight(i) << ' ';
return ostr << ']';
}
template <typename D, typename W>
w_window_<D,W>::w_window_()
{
}
template <typename D, typename W>
const window_<D>&
w_window_<D,W>::window() const
{
return win_;
}
template <typename D, typename W>
const std::vector<D>&
w_window_<D,W>::vect() const
{
return win_.vect();
}
template <typename D, typename W>
const std::vector<W>&
w_window_<D,W>::weights() const
{
return wei_;
}
template <typename D, typename W>
unsigned
w_window_<D,W>::nelements() const
{
mln_invariant(wei_.size() == win_.nelements());
return win_.nelements();
}
template <typename D, typename W>
const D&
w_window_<D,W>::element(unsigned i) const
{
mln_precondition(i < wei_.size());
mln_invariant(wei_.size() == win_.nelements());
return win_.element(i);
}
template <typename D, typename W>
W
w_window_<D,W>::weight(unsigned i) const
{
mln_precondition(i < wei_.size());
mln_invariant(wei_.size() == win_.nelements());
return wei_[i];
}
template <typename D, typename W>
w_window_<D,W>&
w_window_<D,W>::insert(const D& d, const W& w)
{
if (win_.has(d))
// no-op
return *this;
// memorization: d_i -> w_i
std::map<D, W> memo_wei_;
for (unsigned i = 0; i < win_.nelements(); ++i)
memo_wei_[win_.element(i)] = wei_[i];
// insertion of d and w:
win_.insert(d);
memo_wei_[d] = w;
// re-mapping: w_i <- d_i
wei_.resize(win_.nelements());
for (unsigned i = 0; i < win_.nelements(); ++i)
wei_[i] = memo_wei_[win_.element(i)];
return *this;
}
template <typename D, typename W>
w_window_<D,W>
w_window_<D,W>::sym_() const
{
// FIXME
return *this;
}
# endif // ! MLN_INCLUDE_ONLY
} // end of namespace mln
#endif // ! MLN_CORE_W_WINDOW_HH
// Copyright (C) 2007 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_CORE_W_WINDOW2D_INT_HH
# define MLN_CORE_W_WINDOW2D_INT_HH
/*! \file mln/core/w_window2d_int.hh
*
* \brief Definition of the mln::w_window2d_int alias.
*/
# include <mln/core/w_window.hh>
# include <mln/core/dpoint2d.hh>
namespace mln
{
/*! \brief Type alias for a w_window with arbitrary shape, defined
* on the 2D square grid (with integer coordinates) and whose
* weights are integers.
*/
typedef w_window_<dpoint2d, int> w_window2d_int;
} // end of namespace mln
# include <mln/make/w_window2d_int.hh>
#endif // ! MLN_CORE_W_WINDOW2D_INT_HH
// Copyright (C) 2007 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_LINEAR_CONVOLVE_HH
# define MLN_LINEAR_CONVOLVE_HH
/*! \file mln/linear/convolve.hh
*
* \brief Convolution.
*/
# include <mln/core/concept/image.hh>
# include <mln/core/concept/weighted_window.hh>
namespace mln
{
namespace linear
{
/*! Convolution of image \p input by the weighted window \p win.
*
* \pre output.domain = input.domain
*/
template <typename I, typename W, typename O>
void convolve(const Image<I>& input, const Weighted_Window<W>& win,
Image<O>& output);
# ifndef MLN_INCLUDE_ONLY
namespace impl
{
template <typename I, typename W, typename O>
void convolve(const Image<I>& input_, const Weighted_Window<W>& win_,
Image<O>& output_)
{
const I& input = exact(input_);
const W& win = exact(win_);
O& output = exact(output_);
mln_piter(I) p(input.domain());
mln_qiter(W) q(win, p);
for_all(p)
{
mln_value(O) v = 0;
for_all(q) if (input.has(q))
v += input(q) * q.weight();
output(p) = v;
}
}
template <typename I, typename W, typename O>
void convolve(const Fast_Image<I>& input_, const Weighted_Window<W>& win_,
Fast_Image<O>& output_)
{
const I& input = exact(input_);
const W& win = exact(win_);
O& output = exact(output_);
mln_pixter(O) p_out(output);
mln_pixter(const I) p(input);
mln_qixter(const I, W) q(input, win, p);
for_all_2(p, p_out)
{
mln_value(O) v = 0;
unsigned i = 0;
for_all(q)
v += win.weight(i++) * *q;
*p_out = v;
}
}
} // end of namespace mln::linear::impl
template <typename I, typename W, typename O>
void convolve(const Image<I>& input, const Weighted_Window<W>& win,
Image<O>& output)
{
mln_precondition(exact(output).domain() == exact(input).domain());
impl::convolve(exact(input), exact(win), exact(output));
}
# endif // ! MLN_INCLUDE_ONLY
} // end of namespace mln::linear
} // end of namespace mln
#endif // ! MLN_LINEAR_CONVOLVE_HH
// Copyright (C) 2007 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