Developer documentation
Version 3.0.3-105-gd3941f44
cc_helper.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
18#ifndef __registration_metric_cc_helper_h__
19#define __registration_metric_cc_helper_h__
20
21#include "debug.h"
22#include "math/math.h"
23#include "image_helpers.h"
25
26namespace MR
27{
28 namespace Registration
29 {
30 namespace Metric
31 {
32
33 template <class Im1ImageType, class Im2ImageType, class Im1MaskType, class Im2MaskType, class DerivedImageType>
34 void cc_precompute (Im1ImageType& im1_image,
35 Im2ImageType& im2_image,
36 Im1MaskType& im1_mask,
37 Im2MaskType& im2_mask,
38 DerivedImageType& A,
39 DerivedImageType& B,
40 DerivedImageType& C,
41 DerivedImageType& im1_meansubtr,
42 DerivedImageType& im2_meansubtr,
43 const vector<size_t>& extent) {
44 // TODO check extent
45 int nmax = extent[0] * extent[1] * extent[2];
46 Eigen::VectorXd n1 = Eigen::VectorXd(nmax);
47 Eigen::VectorXd n2 = Eigen::VectorXd(nmax);
48 Eigen::Vector3d pos;
49 for (auto l = Loop("precomputing cross correlation values") (im1_image); l; ++l) {
50 pos[0] = im1_image.index(0);
51 pos[1] = im1_image.index(1);
52 pos[2] = im1_image.index(2);
53 assign_pos_of(im1_image, 0, 3).to(A, B, C, im1_meansubtr, im2_meansubtr);
54 default_type m1(0.0);
55 default_type m2(0.0);
56
57 n1.setZero();
58 n2.setZero();
59
60 int nvox(0);
61 auto niter = NeighbourhoodIterator(im1_image, extent);
62 while (niter.loop()) {
63 if (im1_mask.valid()) {
64 assign_pos_of(niter, 0, 3).to(im1_mask);
65 if (!im1_mask.value())
66 continue;
67 }
68 if (im2_mask.valid()) {
69 assign_pos_of(niter, 0, 3).to(im2_mask);
70 if (!im2_mask.value())
71 continue;
72 }
73 assign_pos_of(niter, 0, 3).to(im1_image);
74 assign_pos_of(niter, 0, 3).to(im2_image);
75
76 n1[nvox] = im1_image.value();
77 n2[nvox] = im2_image.value();
78
79 m1 += n1[nvox];
80 m2 += n2[nvox];
81 nvox++;
82 }
83
84 if (nvox == 0) {
85 A.value() = NaN;
86 C.value() = NaN;
87 B.value() = NaN;
88 im1_meansubtr.value() = NaN;
89 im2_meansubtr.value() = NaN;
90 continue;
91 }
92
93 // local mean subtracted
94 n1.array() -= n1.sum() / nvox;
95 n2.array() -= n2.sum() / nvox;
96 A.value() = n1.adjoint() * n2;
97 B.value() = n1.adjoint() * n1;
98 C.value() = n2.adjoint() * n2;
99
100 // reset image positions
101 im1_image.index(0) = pos[0];
102 im1_image.index(1) = pos[1];
103 im1_image.index(2) = pos[2];
104
105 im2_image.index(0) = pos[0];
106 im2_image.index(1) = pos[1];
107 im2_image.index(2) = pos[2];
108
109 im1_meansubtr.value() = im1_image.value() - m1 / nvox;
110 im2_meansubtr.value() = im2_image.value() - m2 / nvox;
111 }
112
113 }
114
115 }
116 }
117}
118#endif
a dummy image to iterate over a certain neighbourhood, useful for multi-threaded looping.
FORCE_INLINE LoopAlongAxes Loop()
Definition: loop.h:419
void cc_precompute(Im1ImageType &im1_image, Im2ImageType &im2_image, Im1MaskType &im1_mask, Im2MaskType &im2_mask, DerivedImageType &A, DerivedImageType &B, DerivedImageType &C, DerivedImageType &im1_meansubtr, DerivedImageType &im2_meansubtr, const vector< size_t > &extent)
Definition: cc_helper.h:34
Definition: base.h:24
double default_type
the default type used throughout MRtrix
Definition: types.h:228
constexpr default_type NaN
Definition: types.h:230