37 template <
typename TCost>
38 class TSSDCost:
public TCost {
44 TSSDCost(
bool normalize,
float automask_thresh);
46 virtual double do_value(
const Data& a,
const Data& b)
const;
47 virtual double do_evaluate_force(
const Data& a,
const Data& b, Force& force)
const;
49 float m_automask_thresh;
53 struct FEvalSSD :
public mia::TFilter<double> {
54 FEvalSSD(
bool normalize,
float automask_thresh):
55 m_normalize(normalize), m_automask_thresh(automask_thresh){}
57 template <
typename T,
typename R>
59 double operator ()(T a, R b)
const {
60 double d = (double)a - (
double)b;
65 template <
typename T,
typename R>
66 FEvalSSD::result_type operator () (
const T& a,
const R& b)
const {
67 double scale = m_normalize ? 0.5 / a.size() : 0.5;
68 if (m_automask_thresh == 0.0f)
69 return scale * inner_product(a.begin(), a.end(), b.begin(), 0.0, ::std::plus<double>(),
70 SQD<typename T::value_type , typename R::value_type >());
78 if (*ia > m_automask_thresh) {
79 double d = (double)*ia - (
double)*ib;
86 return n > 0 ? 0.5 * sum / n : std::numeric_limits<float>::max();
90 float m_automask_thresh;
94 template <
typename TCost>
95 TSSDCost<TCost>::TSSDCost():
97 m_automask_thresh(0.0)
102 template <
typename TCost>
103 TSSDCost<TCost>::TSSDCost(
bool normalize,
float automask_thresh):
104 m_normalize(normalize),
105 m_automask_thresh(automask_thresh)
110 template <
typename TCost>
111 double TSSDCost<TCost>::do_value(
const Data& a,
const Data& b)
const 113 FEvalSSD essd(m_normalize, m_automask_thresh);
114 return filter(essd, a, b);
117 template <
typename Force>
118 struct FEvalForce:
public mia::TFilter<float> {
119 FEvalForce(Force& force,
bool normalize,
float automask_thresh):
121 m_normalize(normalize),
122 m_automask_thresh(automask_thresh)
125 template <
typename T,
typename R>
126 float operator ()(
const T& a,
const R& b)
const {
131 auto fi = m_force.begin();
132 auto gi = gradient.begin();
134 if (m_automask_thresh == 0.0f) {
135 float scale = m_normalize ? 1.0 / a.size() : 1.0;
137 for (
size_t i = 0; i < a.size(); ++i, ++ai, ++bi, ++fi, ++gi) {
138 float delta = float(*ai) - float(*bi);
139 *fi = *gi * delta * scale;
140 cost += delta * delta * scale;
145 for (
size_t i = 0; i < a.size(); ++i, ++ai, ++bi, ++fi, ++gi) {
146 if (*ai > m_automask_thresh) {
147 float delta = float(*ai) - float(*bi);
149 cost += delta * delta;
154 float scale = 1.0f / n;
155 transform(m_force.begin(), m_force.end(), m_force.begin(),
156 [scale](
const typename Force::value_type& x){
return scale * x;});
157 return 0.5 * cost *scale;
159 return std::numeric_limits<float>::max();
166 float m_automask_thresh;
173 template <
typename TCost>
174 double TSSDCost<TCost>::do_evaluate_force(
const Data& a,
const Data& b, Force& force)
const 176 assert(a.get_size() == b.get_size());
177 assert(a.get_size() == force.get_size());
178 FEvalForce<Force> ef(force, m_normalize, m_automask_thresh);
188 template <
typename CP,
typename C>
189 class TSSDCostPlugin:
public CP {
195 float m_automask_thresh;
202 template <
typename CP,
typename C>
203 TSSDCostPlugin<CP,C>::TSSDCostPlugin():
208 TRACE(
"TSSDCostPlugin<CP,C>::TSSDCostPlugin()");
211 "Set whether the metric should be normalized by the number of image pixels")
213 this->add_parameter(
"autothresh",
mia::make_ci_param(m_automask_thresh, 0.0f, 1000.0f,
false,
214 "Use automatic masking of the moving image by only takeing " 215 "intensity values into accound that are larger than the given threshold"));
222 template <
typename CP,
typename C>
223 C *TSSDCostPlugin<CP,C>::do_create()
const 225 return new TSSDCost<C>(m_normalize, m_automask_thresh);
The generic cost function interface.
#define NS_BEGIN(NS)
conveniance define to start a namespace
CTParameter< bool > CBoolParameter
boolean parameter
T Data
typedef for generic programming: The data type used by the cost function
std::shared_ptr< Image > normalize(const Image &image)
a normalizer for image intensities
static F::result_type filter(const F &f, const B &b)
EXPORT_2D C2DFVectorfield get_gradient(const C2DImage &image)
EXPORT_CORE const char * property_gradient
constant defining the gradient property
CParameter * make_ci_param(T &value, S1 lower_bound, S2 upper_bound, bool required, const char *descr)
#define NS_END
conveniance define to end a namespace
V Force
typedef for generic programming: The gradient forca type create by the cost function ...