Developer documentation
Version 3.0.3-105-gd3941f44
median.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 __image_adapter_median_h__
18#define __image_adapter_median_h__
19
20#include "math/median.h"
21#include "adapter/base.h"
22
23namespace MR
24{
25 namespace Adapter
26 {
27
28
29 template <class ImageType>
30 class Median :
31 public Base<Median<ImageType>,ImageType>
33 public:
34
35 using base_type = Base<Median<ImageType>, ImageType>;
36 using value_type = typename ImageType::value_type;
37 using voxel_type = Median;
38
39 using base_type::name;
40 using base_type::size;
41 using base_type::index;
42
43 Median (const ImageType& parent) :
44 base_type (parent) {
45 set_extent (vector<int>(1,3));
46 }
47
48 Median (const ImageType& parent, const vector<uint32_t>& extent) :
49 base_type (parent) {
50 set_extent (extent);
51 }
52
53 void set_extent (const vector<uint32_t>& ext)
54 {
55 for (size_t i = 0; i < ext.size(); ++i)
56 if (! (ext[i] & uint32_t(1)))
57 throw Exception ("expected odd number for extent");
58 if (ext.size() != 1 && ext.size() != 3)
59 throw Exception ("unexpected number of elements specified in extent");
60 if (ext.size() == 1)
61 extent = vector<uint32_t> (3, ext[0]);
62 else
63 extent = ext;
64
65 DEBUG ("median adapter for image \"" + name() + "\" initialised with extent " + str(extent));
66
67 for (size_t i = 0; i < 3; ++i)
68 extent[i] = (extent[i]-1)/2;
69 }
70
71
72
73 value_type value ()
74 {
75 const ssize_t old_pos [3] = { index(0), index(1), index(2) };
76 const ssize_t from[3] = {
77 index(0) < extent[0] ? 0 : index(0) - extent[0],
78 index(1) < extent[1] ? 0 : index(1) - extent[1],
79 index(2) < extent[2] ? 0 : index(2) - extent[2]
80 };
81 const ssize_t to[3] = {
82 index(0) >= size(0)-extent[0] ? size(0) : index(0)+extent[0]+1,
83 index(1) >= size(1)-extent[1] ? size(1) : index(1)+extent[1]+1,
84 index(2) >= size(2)-extent[2] ? size(2) : index(2)+extent[2]+1
85 };
86
87 values.clear();
88
89 for (index(2) = from[2]; index(2) < to[2]; ++index(2))
90 for (index(1) = from[1]; index(1) < to[1]; ++index(1))
91 for (index(0) = from[0]; index(0) < to[0]; ++index(0))
92 values.push_back (base_type::value());
93
94 index(0) = old_pos[0];
95 index(1) = old_pos[1];
96 index(2) = old_pos[2];
97
98 return Math::median (values);
99 }
100
101 protected:
104 };
105
106 }
107}
108
109
110#endif
111
vector< uint32_t > extent
Definition: median.h:102
vector< value_type > values
Definition: median.h:103
#define DEBUG(msg)
Definition: exception.h:75
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
MR::default_type value_type
Definition: typedefs.h:33
Container::value_type median(Container &list)
Definition: median.h:45
Definition: base.h:24
std::string str(const T &value, int precision=0)
Definition: mrtrix.h:247
T to(const std::string &string)
Definition: mrtrix.h:260
size_t index
#define MEMALIGN(...)
Definition: types.h:185
const std::string name
Definition: thread.h:108