38 template <
typename TCost>
39 class TLSDImageCost:
public TCost {
45 class CRefPrepare :
public mia::TFilter<void> {
47 CRefPrepare(std::vector<double>& QtQinv, std::vector<int>& Q_mappping):
49 m_Q_mappping(Q_mappping)
52 template <
typename DataTempl>
53 void operator()(
const DataTempl& ref);
55 std::vector<double>& m_QtQinv;
56 std::vector<int>& m_Q_mappping;
59 class RunCost : mia::TFilter<double> {
62 RunCost(
const std::vector<double>& QtQinv,
const std::vector<int>& Q_mappping);
64 template <
typename DataTempl>
65 double operator()(
const DataTempl& ref)
const;
67 template <
typename DataTempl>
68 double operator()(
const DataTempl& ref, Force& force)
const;
70 const std::vector<double>& m_QtQinv;
71 const std::vector<int>& m_Q_mappping;
76 virtual double do_value(
const Data& a,
const Data& b)
const;
78 virtual double do_evaluate_force(
const Data& a,
const Data& , Force& force)
const;
80 virtual void post_set_reference(
const Data& ref);
82 std::vector<double> m_QtQinv;
83 std::vector<int> m_Q_mappping;
87 inline bool operator < (
const valpos& a,
const valpos& b)
92 template <
typename CP,
typename C>
93 class TLSDImageCostPlugin:
public CP {
95 TLSDImageCostPlugin();
96 virtual TLSDImageCost<C> *do_create()
const;
97 const std::string do_get_descr()
const;
100 template <
typename TCost>
101 template <
typename DataTempl>
102 void TLSDImageCost<TCost>::CRefPrepare::operator()(
const DataTempl& ref)
104 size_t npixels = ref.get_size().x * ref.get_size().y;
105 std::vector<valpos> buffer(npixels);
106 m_Q_mappping.resize(npixels);
107 m_QtQinv.resize(npixels);
110 for(
auto x = ref.begin(); x != ref.end(); ++x, ++idx) {
112 valpos vp = {value, idx};
115 std::sort(buffer.begin(), buffer.end());
118 auto x = buffer.begin();
119 double oldv = x->val;
124 m_Q_mappping[x->pos] = idx;
126 while ( x != buffer.end() ) {
127 if (x->val != oldv) {
132 m_Q_mappping[x->pos] = idx;
136 m_QtQinv.resize(idx);
137 std::transform(m_QtQinv.begin(), m_QtQinv.end(), m_QtQinv.begin(),
138 [](
double x){
return 1.0 / x; });
141 template <
typename TCost>
142 void TLSDImageCost<TCost>::post_set_reference(
const Data& ref)
144 CRefPrepare rp(m_QtQinv, m_Q_mappping);
149 template <
typename TCost>
150 double TLSDImageCost<TCost>::do_value(
const Data& a,
const Data& )
const 152 RunCost rf(m_QtQinv, m_Q_mappping);
156 template <
typename TCost>
157 double TLSDImageCost<TCost>::do_evaluate_force(
const Data& a,
const Data& , Force& force)
const 159 RunCost rf(m_QtQinv, m_Q_mappping);
163 template <
typename TCost>
164 TLSDImageCost<TCost>::RunCost::RunCost(
const std::vector<double>& QtQinv,
const std::vector<int>& Q_mappping):
166 m_Q_mappping(Q_mappping)
170 template <
typename TCost>
171 template <
typename DataTempl>
172 double TLSDImageCost<TCost>::RunCost::operator()(
const DataTempl& a)
const 176 std::vector<double> sums(m_QtQinv.size(), 0.0);
178 auto idx = m_Q_mappping.begin();
179 for (
auto ia = a.begin(); ia != a.end(); ++ia, ++idx) {
185 for(
size_t i = 0; i < sums.size(); ++i)
186 val2 += sums[i] * sums[i] * m_QtQinv[i];
188 return 0.5 * (val1 - val2);
191 template <
typename TCost>
192 template <
typename DataTempl>
193 double TLSDImageCost<TCost>::RunCost::operator()(
const DataTempl& a, Force& force)
const 196 std::vector<double> sums(m_QtQinv.size(), 0.0);
198 auto idx = m_Q_mappping.begin();
199 for (
auto ia = a.begin(); ia != a.end(); ++ia, ++idx) {
204 auto s = sums.begin();
205 for (
auto q = m_QtQinv.begin(); q != m_QtQinv.end(); ++q, ++s) {
206 value -= *q * *s * *s;
211 auto iforce = force.begin();
212 auto igrad = gradient.begin();
213 idx = m_Q_mappping.begin();
214 for (
auto ia = a.begin(); ia != a.end(); ++ia, ++iforce, ++igrad, ++idx)
215 *iforce = *igrad * (*ia - sums[*idx]);
221 template <
typename CP,
typename C>
222 TLSDImageCostPlugin<CP,C>::TLSDImageCostPlugin():
227 template <
typename CP,
typename C>
228 TLSDImageCost<C> *TLSDImageCostPlugin<CP,C>::do_create()
const 230 return new TLSDImageCost<C>();
233 template <
typename CP,
typename C>
234 const std::string TLSDImageCostPlugin<CP,C>::do_get_descr()
const 236 return "Least-Squares Distance measure";
static F::result_type filter_and_output(const F &f, const B &a, O &b)
The generic cost function interface.
#define NS_BEGIN(NS)
conveniance define to start a namespace
T Data
typedef for generic programming: The data type used by the cost function
bool operator<(const T2DVector< T > &a, const T2DVector< S > &b)
static F::result_type filter(const F &f, const B &b)
EXPORT_2D C2DFVectorfield get_gradient(const C2DImage &image)
R result_type
defines the return type of the filter function
static F::result_type accumulate(F &f, const B &data)
#define NS_END
conveniance define to end a namespace
V Force
typedef for generic programming: The gradient forca type create by the cost function ...