Developer documentation
Version 3.0.3-105-gd3941f44
demons.h
Go to the documentation of this file.
1/* Copyright (c) 2008-2022 the MRtrix3 contributors.
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 *
7 * Covered Software is provided under this License on an "as is"
8 * basis, without warranty of any kind, either expressed, implied, or
9 * statutory, including, without limitation, warranties that the
10 * Covered Software is free of defects, merchantable, fit for a
11 * particular purpose or non-infringing.
12 * See the Mozilla Public License v. 2.0 for more details.
13 *
14 * For more details, see http://www.mrtrix.org/.
15 */
16
17#ifndef __registration_metric_demons_h__
18#define __registration_metric_demons_h__
19
20#include <mutex>
21
22#include "image_helpers.h"
23#include "adapter/gradient3D.h"
24
25namespace MR
26{
27 namespace Registration
28 {
29 namespace Metric
30 {
31
32 template <class Im1ImageType, class Im2ImageType, class Im1MaskType, class Im2MaskType>
34 public:
35 Demons (default_type& global_energy, size_t& global_voxel_count,
36 const Im1ImageType& im1_image, const Im2ImageType& im2_image, const Im1MaskType im1_mask, const Im2MaskType im2_mask) :
37 global_cost (global_energy),
39 thread_cost (0.0),
41 mutex (new std::mutex),
42 normaliser (0.0),
43 robustness_parameter (1.e-12),
46 im1_gradient (im1_image, true), im2_gradient (im2_image, true),
48 {
49 for (size_t d = 0; d < 3; ++d)
50 normaliser += im1_image.spacing(d) * im2_image.spacing(d);
51 normaliser /= 3.0;
52 }
53
54 ~Demons () {
55 std::lock_guard<std::mutex> lock (*mutex);
58 }
59
60 void set_im1_mask (const Image<float>& mask) {
61 im1_mask = mask;
62 }
63
64 void set_im2_mask (const Image<float>& mask) {
65 im2_mask = mask;
66 }
67
68
69 void operator() (const Im1ImageType& im1_image,
70 const Im2ImageType& im2_image,
71 Image<default_type>& im1_update,
72 Image<default_type>& im2_update) {
73
74 if (im1_image.index(0) == 0 || im1_image.index(0) == im1_image.size(0) - 1 ||
75 im1_image.index(1) == 0 || im1_image.index(1) == im1_image.size(1) - 1 ||
76 im1_image.index(2) == 0 || im1_image.index(2) == im1_image.size(2) - 1) {
77 im1_update.row(3) = 0.0;
78 im2_update.row(3) = 0.0;
79 return;
80 }
81
82 typename Im1MaskType::value_type im1_mask_value = 1.0;
83 if (im1_mask.valid()) {
84 assign_pos_of (im1_image, 0, 3).to (im1_mask);
85 im1_mask_value = im1_mask.value();
86 if (im1_mask_value < 0.1) {
87 im1_update.row(3) = 0.0;
88 im2_update.row(3) = 0.0;
89 return;
90 }
91 }
92
93 typename Im2MaskType::value_type im2_mask_value = 1.0;
94 if (im2_mask.valid()) {
95 assign_pos_of (im2_image, 0, 3).to (im2_mask);
96 im2_mask_value = im2_mask.value();
97 if (im2_mask_value < 0.1) {
98 im1_update.row(3) = 0.0;
99 im2_update.row(3) = 0.0;
100 return;
101 }
102 }
103
104
105 default_type speed = im2_image.value() - im1_image.value();
106 if (abs (speed) < robustness_parameter)
107 speed = 0.0;
108
109 default_type speed_squared = speed * speed;
110 thread_cost += speed_squared;
112
113 assign_pos_of (im1_image, 0, 3).to (im1_gradient, im2_gradient);
114
115 Eigen::Matrix<typename Im1ImageType::value_type, 3, 1> grad = (im2_gradient.value() + im1_gradient.value()).array() / 2.0;
116 default_type denominator = speed_squared / normaliser + grad.squaredNorm();
117 if (abs (speed) < intensity_difference_threshold || denominator < denominator_threshold) {
118 im1_update.row(3) = 0.0;
119 im2_update.row(3) = 0.0;
120 } else {
121 im1_update.row(3) = Eigen::Vector3d (speed * grad.array() / denominator);
122 im2_update.row(3) = -Eigen::Vector3d (im1_update.row(3));
123 }
124 }
125
126
127 protected:
132 std::shared_ptr<std::mutex> mutex;
137
140 Im1MaskType im1_mask;
141 Im2MaskType im2_mask;
142
143 };
144 }
145 }
146}
147#endif
Adapter::Gradient3D< Im1ImageType > im1_gradient
Definition: demons.h:138
std::shared_ptr< std::mutex > mutex
Definition: demons.h:132
const default_type denominator_threshold
Definition: demons.h:136
default_type & global_cost
Definition: demons.h:128
Adapter::Gradient3D< Im2ImageType > im2_gradient
Definition: demons.h:139
const default_type intensity_difference_threshold
Definition: demons.h:135
const default_type robustness_parameter
Definition: demons.h:134
constexpr double e
Definition: math.h:39
MR::default_type value_type
Definition: typedefs.h:33
Definition: base.h:24
double default_type
the default type used throughout MRtrix
Definition: types.h:228
constexpr std::enable_if< std::is_arithmetic< X >::value &&std::is_unsigned< X >::value, X >::type abs(X x)
Definition: types.h:297