Developer documentation
Version 3.0.3-105-gd3941f44
resize.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_filter_resize_h__
18#define __image_filter_resize_h__
19
20#include "filter/base.h"
21#include "filter/reslice.h"
22#include "filter/smooth.h"
23#include "interp/nearest.h"
24#include "interp/linear.h"
25#include "interp/cubic.h"
26#include "interp/sinc.h"
27#include "image.h"
28#include "algo/copy.h"
29
30namespace MR
31{
32 namespace Filter
33 {
58 class Resize : public Base { MEMALIGN(Resize)
59
60 public:
61 template <class HeaderType>
62 Resize (const HeaderType& in) :
63 Base (in),
64 interp_type (2), // Cubic
67 out_of_bounds_value (nullptr) { }
68
69 ~Resize () { delete out_of_bounds_value; }
70
71 void set_voxel_size (default_type size) {
72 vector<default_type> voxel_size (3, size);
73 set_voxel_size (voxel_size);
74 }
75
76
77 void set_voxel_size (const vector<default_type>& voxel_size) {
78 if (voxel_size.size() != 3)
79 throw Exception ("the voxel size must be defined using a value for all three dimensions.");
80
81 Eigen::Vector3d original_extent;
82 for (size_t j = 0; j < 3; ++j) {
83 if (voxel_size[j] <= 0.0)
84 throw Exception ("the voxel size must be larger than zero");
85 original_extent[j] = axes_[j].size * axes_[j].spacing;
86 axes_[j].size = std::round (axes_[j].size * axes_[j].spacing / voxel_size[j] - 0.0001); // round down at .5
87 // Here we adjust the translation to ensure the image extent is centered wrt the original extent.
88 // This is important when the new voxel size is not an exact multiple of the original extent
89 for (size_t i = 0; i < 3; ++i)
90 transform_(i,3) += 0.5 * ((voxel_size[j] - axes_[j].spacing) + (original_extent[j] - (axes_[j].size * voxel_size[j]))) * transform_(i,j);
91 axes_[j].spacing = voxel_size[j];
92 }
93 }
94
95
96 void set_size (const vector<uint32_t>& image_res) {
97 if (image_res.size() != 3)
98 throw Exception ("the image resolution must be defined for 3 spatial dimensions");
99 vector<default_type> new_voxel_size (3);
100 for (size_t d = 0; d < 3; ++d) {
101 if (image_res[d] <= 0)
102 throw Exception ("the image resolution must be larger than zero for all 3 spatial dimensions");
103 new_voxel_size[d] = (this->size(d) * this->spacing(d)) / image_res[d];
104 }
105 set_voxel_size (new_voxel_size);
106 }
107
108
109 void set_scale_factor (default_type scale) {
110 set_scale_factor (vector<default_type> (3, scale));
111 }
112
113
114 void set_scale_factor (const vector<default_type> & scale) {
115 if (scale.size() != 3)
116 throw Exception ("a scale factor for each spatial dimension is required");
117 vector<default_type> new_voxel_size (3);
118 for (size_t d = 0; d < 3; ++d) {
119 if (scale[d] <= 0.0)
120 throw Exception ("the scale factor must be larger than zero");
121 new_voxel_size[d] = (this->size(d) * this->spacing(d)) / std::ceil (this->size(d) * scale[d]);
122 }
123 set_voxel_size (new_voxel_size);
124 }
125
126 void set_oversample (vector<uint32_t> oversample) {
127 if (oversample.size() == 1)
128 oversample.resize (3, oversample[0]);
129 else if (oversample.size() != 3 and oversample.size() != 0)
130 throw Exception ("FIXME oversample requires either a vector of a 0 (auto), 1 or 3 integers integer, got " + str(oversample.size()));
131 for (auto f : oversample) {
132 if (f < 1)
133 throw Exception ("oversample factors must be positive integers");
134 }
135 oversampling = oversample;
136 }
137
138 void set_interp_type (int type) {
139 interp_type = type;
140 }
141
142 void set_transform (const transform_type& trafo) {
143 transform_ = trafo;
144 }
145
146 void set_out_of_bounds_value (default_type value) {
149 }
150
151 template <class InputImageType, class OutputImageType>
152 void operator() (InputImageType& input, OutputImageType& output) {
155 switch (interp_type) {
156 case 0:
157 // Use of oversampling is prevented in reslice adapter
158 reslice <Interp::Nearest> (input, output, transformation, oversampling, oob);
159 break;
160 case 1:
161 reslice <Interp::Linear> (input, output, transformation, oversampling, oob);
162 break;
163 case 2:
164 reslice <Interp::Cubic> (input, output, transformation, oversampling, oob);
165 break;
166 case 3:
167 reslice <Interp::Sinc> (input, output, transformation, oversampling, oob);
168 break;
169 default:
170 assert (0);
171 break;
172 }
173 }
174
175 protected:
180 };
182 }
183}
184
185
186#endif
vector< uint32_t > oversampling
Definition: resize.h:178
default_type * out_of_bounds_value
Definition: resize.h:179
transform_type transformation
Definition: resize.h:177
vector< Axis > axes_
Definition: header.h:361
transform_type transform_
Definition: header.h:362
This class defines the interface for classes that perform image interpolation.
Definition: base.h:70
constexpr I ceil(const T x)
template function with cast to different type
Definition: math.h:86
constexpr I round(const T x)
Definition: math.h:64
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
const vector< uint32_t > AutoOverSample
const transform_type NoTransform
mat4 scale(float x, float y, float z)
MR::default_type value_type
Definition: typedefs.h:33
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
Eigen::Transform< default_type, 3, Eigen::AffineCompact > transform_type
the type for the affine transform of an image:
Definition: types.h:234