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