Developer documentation
Version 3.0.3-105-gd3941f44
dilate.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_dilate_h__
18#define __filter_dilate_h__
19
20#include "memory.h"
21#include "image.h"
22#include "algo/copy.h"
23#include "algo/loop.h"
24#include "filter/base.h"
25
26
27
28namespace MR
29{
30 namespace Filter
31 {
32
37
49 class Dilate : public Base { MEMALIGN(Dilate)
50
51 public:
52 template <class HeaderType>
53 Dilate (const HeaderType& in) :
54 Base (in),
55 npass (1)
56 {
58 }
59
60 template <class HeaderType>
61 Dilate (const HeaderType& in, const std::string& message) :
62 Base (in, message),
63 npass (1)
64 {
66 }
67
68
69 template <class InputImageType, class OutputImageType>
70 void operator() (InputImageType& input, OutputImageType& output)
71 {
72 std::shared_ptr <Image<bool> > in (new Image<bool> (Image<bool>::scratch (input)));
73 copy (input, *in);
74 std::shared_ptr <Image<bool> > out;
75 std::shared_ptr<ProgressBar> progress (message.size() ? new ProgressBar (message, npass + 1) : nullptr);
76
77 for (unsigned int pass = 0; pass < npass; pass++) {
78 out = make_shared<Image<bool>> (Image<bool>::scratch (input));
79 for (auto l = Loop (*in) (*in, *out); l; ++l)
80 out->value() = dilate (*in);
81 if (pass < npass - 1)
82 in = out;
83 if (progress)
84 ++(*progress);
85 }
86 copy (*out, output);
87 }
88
89
90 void set_npass (unsigned int npasses)
91 {
92 npass = npasses;
93 }
94
95
96 protected:
97
99 {
100 if (in.value()) return true;
101 bool val;
102 if (in.index(0) > 0) { in.index(0)--; val = in.value(); in.index(0)++; if (val) return true; }
103 if (in.index(1) > 0) { in.index(1)--; val = in.value(); in.index(1)++; if (val) return true; }
104 if (in.index(2) > 0) { in.index(2)--; val = in.value(); in.index(2)++; if (val) return true; }
105 if (in.index(0) < in.size(0)-1) { in.index(0)++; val = in.value(); in.index(0)--; if (val) return true; }
106 if (in.index(1) < in.size(1)-1) { in.index(1)++; val = in.value(); in.index(1)--; if (val) return true; }
107 if (in.index(2) < in.size(2)-1) { in.index(2)++; val = in.value(); in.index(2)--; if (val) return true; }
108 return false;
109 }
110
111 unsigned int npass;
112 };
114 }
115}
116
117
118
119
120#endif
static constexpr uint8_t Bit
Definition: datatype.h:142
std::string message
Definition: base.h:66
a filter to dilate a mask
Definition: dilate.h:49
unsigned int npass
Definition: dilate.h:111
bool dilate(Image< bool > &in)
Definition: dilate.h:98
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
ssize_t size(size_t axis) const
Definition: image.h:66
implements a progress meter to provide feedback to the user
Definition: progressbar.h:58
FORCE_INLINE LoopAlongAxes Loop()
Definition: loop.h:419
Definition: base.h:24
void copy(InputImageType &&source, OutputImageType &&destination, size_t from_axis=0, size_t to_axis=std::numeric_limits< size_t >::max())
Definition: copy.h:27