/usr/include/mia-2.0/mia/template/ssd.hh is in libmia-2.0-dev 2.0.13-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | /* -*- mia-c++ -*-
*
* This file is part of MIA - a toolbox for medical image analysis
* Copyright (c) Leipzig, Madrid 1999-2013 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/>.
*
*/
#include <mia/core/filter.hh>
#include <mia/core/msgstream.hh>
#include <mia/core/parameter.hh>
#include <mia/core/property_flags.hh>
#include <numeric>
#include <limits>
NS_BEGIN(NS)
/// @cond DOC_PLUGINS
/**
@ingroup registation
*/
template <typename TCost>
class TSSDCost: public TCost {
public:
typedef typename TCost::Data Data;
typedef typename TCost::Force Force;
TSSDCost();
TSSDCost(bool normalize);
private:
virtual double do_value(const Data& a, const Data& b) const;
virtual double do_evaluate_force(const Data& a, const Data& b, Force& force) const;
bool m_normalize;
};
struct FEvalSSD : public mia::TFilter<double> {
FEvalSSD(bool normalize):m_normalize(normalize){}
template <typename T, typename R>
struct SQD {
double operator ()(T a, R b) const {
double d = (double)a - (double)b;
return d * d;
}
};
template <typename T, typename R>
FEvalSSD::result_type operator () (const T& a, const R& b) const {
double scale = m_normalize ? 0.5 / a.size() : 0.5;
return scale * inner_product(a.begin(), a.end(), b.begin(), 0.0, ::std::plus<double>(),
SQD<typename T::value_type , typename R::value_type >());
}
bool m_normalize;
};
template <typename TCost>
TSSDCost<TCost>::TSSDCost():
m_normalize(true)
{
this->add(::mia::property_gradient);
}
template <typename TCost>
TSSDCost<TCost>::TSSDCost(bool normalize):
m_normalize(normalize)
{
this->add(::mia::property_gradient);
}
template <typename TCost>
double TSSDCost<TCost>::do_value(const Data& a, const Data& b) const
{
FEvalSSD essd(m_normalize);
return filter(essd, a, b);
}
template <typename Force>
struct FEvalForce: public mia::TFilter<float> {
FEvalForce(Force& force, bool normalize):
m_force(force),
m_normalize(normalize)
{
}
template <typename T, typename R>
float operator ()( const T& a, const R& b) const {
Force gradient = get_gradient(a);
float cost = 0.0;
auto ai = a.begin();
auto bi = b.begin();
auto fi = m_force.begin();
auto gi = gradient.begin();
float scale = m_normalize ? 1.0 / a.size() : 1.0;
for (size_t i = 0; i < a.size(); ++i, ++ai, ++bi, ++fi, ++gi) {
float delta = float(*ai) - float(*bi);
*fi = *gi * delta * scale;
cost += delta * delta * scale;
}
return 0.5 * cost;
}
private:
Force& m_force;
bool m_normalize;
};
/**
This is the force evaluation routine of the cost function
*/
template <typename TCost>
double TSSDCost<TCost>::do_evaluate_force(const Data& a, const Data& b, Force& force) const
{
assert(a.get_size() == b.get_size());
assert(a.get_size() == force.get_size());
FEvalForce<Force> ef(force, m_normalize);
return filter(ef, a, b);
}
/**
This is the plug-in declaration - the actual plugin needs to define the
cost plugin type and the data type (this could be unified)
do_test and do_get_descr need to be implemented
*/
template <typename CP, typename C>
class TSSDCostPlugin: public CP {
public:
TSSDCostPlugin();
C *do_create()const;
private:
bool m_normalize;
};
/**
This plugin will alwasy be "ssd"
*/
template <typename CP, typename C>
TSSDCostPlugin<CP,C>::TSSDCostPlugin():
CP("ssd"),
m_normalize(false)
{
TRACE("TSSDCostPlugin<CP,C>::TSSDCostPlugin()");
this->add_property(::mia::property_gradient);
this->add_parameter("norm", new mia::CBoolParameter(m_normalize, false,
"Set whether the metric should be normalized by the number of image pixels"));
}
/**
The creator routine is also generic
*/
template <typename CP, typename C>
C *TSSDCostPlugin<CP,C>::do_create() const
{
return new TSSDCost<C>(m_normalize);
}
/// @endcond
NS_END
|