Developer documentation
Version 3.0.3-105-gd3941f44
dwi_brain_mask.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 __filter_dwi_brain_mask_h__
18#define __filter_dwi_brain_mask_h__
19
20#include "memory.h"
21#include "image.h"
22#include "phase_encoding.h"
23#include "progressbar.h"
24#include "filter/base.h"
26#include "filter/median.h"
28#include "algo/histogram.h"
29#include "algo/copy.h"
30#include "algo/loop.h"
31#include "dwi/gradient.h"
32
33
34namespace MR
35{
36 namespace Filter
37 {
38
39
44
57 class DWIBrainMask : public Base { MEMALIGN(DWIBrainMask)
58
59 public:
60
61 template <class HeaderType>
62 DWIBrainMask (const HeaderType& input, const Eigen::MatrixXd& grad) :
63 Base (input),
64 grad (grad)
65 {
68 axes_.resize(3);
70 }
71
72
73 template <class InputImageType, class OutputImageType>
74 void operator() (InputImageType& input, OutputImageType& output) {
76
77 Header header (input);
78 header.ndim() = 3;
79
80 // Generate a 'master' scratch buffer mask, to which all shells will contribute
81 auto mask_image = Image<bool>::scratch (header, "DWI mask");
82
83 std::unique_ptr<ProgressBar> progress (message.size() ? new ProgressBar (message) : nullptr);
84
85 // Loop over each shell, including b=0, in turn
86 DWI::Shells shells (grad);
87 for (size_t s = 0; s != shells.count(); ++s) {
88 const DWI::Shell& shell (shells[s]);
89
90 auto shell_image = Image<value_type>::scratch (header, "mean b=" + str(size_t(std::round(shell.get_mean()))) + " image");
91
92 for (auto l = Loop (0, 3) (input, shell_image); l; ++l) {
93 default_type sum = 0.0;
94 for (vector<size_t>::const_iterator v = shell.get_volumes().begin(); v != shell.get_volumes().end(); ++v) {
95 input.index(3) = *v;
96 const value_type value = input.value();
97 if (value > value_type(0))
98 sum += value;
99 }
100 shell_image.value() = value_type(sum / default_type(shell.count()));
101 }
102 if (progress)
103 ++(*progress);
104
105 // Threshold the mean intensity image for this shell
106 OptimalThreshold threshold_filter (shell_image);
107
108 auto shell_mask_voxel = Image<bool>::scratch (threshold_filter);
109 threshold_filter (shell_image, shell_mask_voxel);
110 if (progress)
111 ++(*progress);
112
113 // Add this mask to the master
114 for (auto l = Loop (0, 3)(mask_image, shell_mask_voxel); l; ++l) {
115 if (shell_mask_voxel.value())
116 mask_image.value() = true;
117 }
118 if (progress)
119 ++(*progress);
120
121 }
122
123 // The following operations apply to the mask as combined from all shells
124 auto temp_image = Image<bool>::scratch (header, "temporary mask");
125 Median median_filter (mask_image);
126 median_filter (mask_image, temp_image);
127 if (progress)
128 ++(*progress);
129
130 ConnectedComponents connected_filter (temp_image);
131 connected_filter.set_largest_only (true);
132 connected_filter (temp_image, temp_image);
133 if (progress)
134 ++(*progress);
135
136 for (auto l = Loop (0,3)(temp_image); l; ++l)
137 temp_image.value() = !temp_image.value();
138 if (progress)
139 ++(*progress);
140
141 connected_filter (temp_image, temp_image);
142 if (progress)
143 ++(*progress);
144
145 for (auto l = Loop (0,3) (temp_image, output); l; ++l)
146 output.value() = !temp_image.value();
147 }
148
149 protected:
150 const Eigen::MatrixXd& grad;
151
152 };
154 }
155}
156
157
158
159
160#endif
default_type get_mean() const
Definition: shells.h:85
size_t count() const
Definition: shells.h:83
const vector< size_t > & get_volumes() const
Definition: shells.h:82
size_t count() const
Definition: shells.h:121
static constexpr uint8_t Bit
Definition: datatype.h:142
std::string message
Definition: base.h:66
a filter to compute a whole brain mask from a DWI image.
const Eigen::MatrixXd & grad
a filter to compute the optimal threshold to mask a DataSet.
vector< Axis > axes_
Definition: header.h:361
DataType datatype_
the type of the data as stored on file
Definition: header.h:370
static Image scratch(const Header &template_header, const std::string &label="scratch image")
Definition: image.h:195
implements a progress meter to provide feedback to the user
Definition: progressbar.h:58
constexpr I round(const T x)
Definition: math.h:64
FORCE_INLINE LoopAlongAxes Loop()
Definition: loop.h:419
VectorType::Scalar value(const VectorType &coefs, typename VectorType::Scalar cos_elevation, typename VectorType::Scalar cos_azimuth, typename VectorType::Scalar sin_azimuth, int lmax)
Definition: SH.h:233
void stash_DW_scheme(Header &header, const MatrixType &grad)
'stash' the DW gradient table
Definition: gradient.h:212
MR::default_type value_type
Definition: typedefs.h:33
void clear_scheme(Header &header)
clear the phase encoding matrix from a header
Definition: base.h:24
double default_type
the default type used throughout MRtrix
Definition: types.h:228
std::string str(const T &value, int precision=0)
Definition: mrtrix.h:247