37template <
typename TCost>
38class TSSDCost:
public TCost
45 TSSDCost(
bool normalize,
float automask_thresh);
47 virtual double do_value(
const Data& a,
const Data& b)
const;
48 virtual double do_evaluate_force(
const Data& a,
const Data& b, Force& force)
const;
50 float m_automask_thresh;
54struct FEvalSSD :
public mia::TFilter<double> {
55 FEvalSSD(
bool normalize,
float automask_thresh):
56 m_normalize(
normalize), m_automask_thresh(automask_thresh) {}
58 template <
typename T,
typename R>
60 double operator ()(T a, R b)
const
62 double d = (double)a - (
double)b;
67 template <
typename T,
typename R>
68 FEvalSSD::result_type operator () (
const T& a,
const R& b)
const
70 double scale = m_normalize ? 0.5 / a.size() : 0.5;
72 if (m_automask_thresh == 0.0f)
73 return scale * inner_product(a.begin(), a.end(), b.begin(), 0.0, ::std::plus<double>(),
74 SQD<typename T::value_type, typename R::value_type >());
83 if (*ia > m_automask_thresh) {
84 double d = (double) * ia - (
double) * ib;
94 return n > 0 ? 0.5 * sum / n : std::numeric_limits<float>::max();
98 float m_automask_thresh;
102template <
typename TCost>
103TSSDCost<TCost>::TSSDCost():
105 m_automask_thresh(0.0)
107 this->add(::mia::property_gradient);
110template <
typename TCost>
111TSSDCost<TCost>::TSSDCost(
bool normalize,
float automask_thresh):
113 m_automask_thresh(automask_thresh)
115 this->add(::mia::property_gradient);
118template <
typename TCost>
119double TSSDCost<TCost>::do_value(
const Data& a,
const Data& b)
const
121 FEvalSSD essd(m_normalize, m_automask_thresh);
122 return filter(essd, a, b);
125template <
typename Force>
126struct FEvalForce:
public mia::TFilter<float> {
127 FEvalForce(Force& force,
bool normalize,
float automask_thresh):
130 m_automask_thresh(automask_thresh)
133 template <
typename T,
typename R>
134 float operator ()(
const T& a,
const R& b)
const
140 auto fi = m_force.begin();
141 auto gi = gradient.begin();
143 if (m_automask_thresh == 0.0f) {
144 float scale = m_normalize ? 1.0 / a.size() : 1.0;
146 for (
size_t i = 0; i < a.size(); ++i, ++ai, ++bi, ++fi, ++gi) {
147 float delta = float(*ai) - float(*bi);
148 *fi = *gi * delta * scale;
149 cost += delta * delta * scale;
156 for (
size_t i = 0; i < a.size(); ++i, ++ai, ++bi, ++fi, ++gi) {
157 if (*ai > m_automask_thresh) {
158 float delta = float(*ai) - float(*bi);
160 cost += delta * delta;
166 float scale = 1.0f / n;
167 transform(m_force.begin(), m_force.end(), m_force.begin(),
168 [scale](
const typename Force::value_type & x) {
171 return 0.5 * cost * scale;
173 return std::numeric_limits<float>::max();
180 float m_automask_thresh;
187template <
typename TCost>
188double TSSDCost<TCost>::do_evaluate_force(
const Data& a,
const Data& b, Force& force)
const
190 assert(a.get_size() == b.get_size());
191 assert(a.get_size() == force.get_size());
192 FEvalForce<Force> ef(force, m_normalize, m_automask_thresh);
202template <
typename CP,
typename C>
203class TSSDCostPlugin:
public CP
210 float m_automask_thresh;
217template <
typename CP,
typename C>
218TSSDCostPlugin<CP, C>::TSSDCostPlugin():
223 TRACE(
"TSSDCostPlugin<CP,C>::TSSDCostPlugin()");
224 this->add_property(::mia::property_gradient);
225 this->add_parameter(
"norm",
new mia::CBoolParameter(m_normalize,
false,
226 "Set whether the metric should be normalized by the number of image pixels")
228 this->add_parameter(
"autothresh", mia::make_ci_param(m_automask_thresh, 0.0f, 1000.0f,
false,
229 "Use automatic masking of the moving image by only takeing "
230 "intensity values into accound that are larger than the given threshold"));
236template <
typename CP,
typename C>
237C *TSSDCostPlugin<CP, C>::do_create()
const
239 return new TSSDCost<C>(m_normalize, m_automask_thresh);
EXPORT_2D C2DFVectorfield get_gradient(const C2DImage &image)
The generic cost function interface.
V Force
typedef for generic programming: The gradient forca type create by the cost function
T Data
typedef for generic programming: The data type used by the cost function
#define NS_BEGIN(NS)
conveniance define to start a namespace
#define NS_END
conveniance define to end a namespace
static F::result_type filter(const F &f, const B &b)
std::shared_ptr< Image > normalize(const Image &image)
a normalizer for image intensities