-
Notifications
You must be signed in to change notification settings - Fork 10
/
ExplicitVolume.h
275 lines (221 loc) · 6.94 KB
/
ExplicitVolume.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#ifndef IMAGEPROCESSING_EXPLICIT_VOLUME_H__
#define IMAGEPROCESSING_EXPLICIT_VOLUME_H__
#include <cmath>
#include <vigra/multi_array.hxx>
#include <vigra/functorexpression.hxx>
#include <config.h>
#ifdef HAVE_NUMPY
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <numpy/arrayobject.h>
#endif
#include <imageprocessing/Image.h>
#include <util/exceptions.h>
#include <util/typename.h>
#include "DiscreteVolume.h"
template <typename T>
struct numpy_type_traits {};
template <>
struct numpy_type_traits<int> {
static char getNumpyType() { return NPY_INT32; }
};
template <>
struct numpy_type_traits<float> {
static char getNumpyType() { return NPY_FLOAT32; }
};
/**
* Explicit representation of a discrete volume as a vigra multi-array.
*/
template <typename ValueType>
class ExplicitVolume : public DiscreteVolume {
public:
typedef ValueType value_type;
typedef vigra::MultiArray<3, ValueType> data_type;
/**
* Create an empty volume.
*/
ExplicitVolume() {}
/**
* Create a volume from another (type compatible) volume.
*/
template <typename T>
ExplicitVolume(const ExplicitVolume<T>& other) :
DiscreteVolume(other),
_data(other.data()) {}
/**
* Create a new explicit volume of the given size.
*/
ExplicitVolume(
unsigned int width,
unsigned int height,
unsigned int depth) :
_data(vigra::Shape3(width, height, depth)) {}
/**
* Create a new explicit volume of the given size. Initialize all voxels
* with the given value.
*/
ExplicitVolume(
unsigned int width,
unsigned int height,
unsigned int depth,
const ValueType& value) :
_data(vigra::Shape3(width, height, depth), value) {}
/**
* Pixel access.
*/
ValueType& operator[](vigra::Shape3 pos) { return _data[pos]; }
const ValueType& operator[](vigra::Shape3 pos) const { return _data[pos]; }
ValueType& operator[](util::point<unsigned int, 3> pos) { return _data(pos.x(), pos.y(), pos.z()); }
const ValueType& operator[](util::point<unsigned int, 3> pos) const { return _data(pos.x(), pos.y(), pos.z()); }
ValueType& operator()(unsigned int x, unsigned int y, unsigned int z) { return _data(x, y, z); }
const ValueType& operator()(unsigned int x, unsigned int y, unsigned int z) const { return _data(x, y, z); }
/**
* 2D z-slice access.
*/
Image slice(int z) {
vigra::MultiArrayView<2, ValueType> slice = _data.template bind<2>(z);
Image image;
image = slice;
image.setResolution(
getResolutionX(),
getResolutionY(),
getResolutionZ());
image.setOffset(
getBoundingBox().min().x(),
getBoundingBox().min().y(),
getBoundingBox().min().z() + z*getResolutionZ());
return image;
}
/**
* Get access to the vigra multi-array that contains the data of this
* volume.
*/
data_type& data() { return _data; }
const data_type& data() const { return _data; }
unsigned int width() const { return _data.shape()[0]; }
unsigned int height() const { return _data.shape()[1]; }
unsigned int depth() const { return _data.shape()[2]; }
/**
* Resize this volume and initialize with zeros.
*/
void resize(
unsigned int width,
unsigned int height,
unsigned int depth) {
_data.reshape(vigra::Shape3(width, height, depth));
setDiscreteBoundingBoxDirty();
}
/**
* Ensure that the values of this ExplicitVolume are in the range [0,1], if
* they aren't already. If the max exceeds 1 but not 255, values will be
* scaled with 1/255. Otherwise, values will be scaled with 1/max.
*
* Should the min be negative, a shift of -min will be applied before
* determining the max.
*/
void normalize() {
ValueType min, max;
ValueType shift = 0;
data().minmax(&min, &max);
if (min < 0) {
shift = -min;
max += shift;
}
if (min >= 0 && max > 1.0 && max <= 255.0)
max = 255;
using namespace vigra::functor;
if (shift != 0 || max != 1.0)
vigra::transformMultiArray(
data(),
data(),
(Arg1() + Param(shift))/Param(max));
}
/**
* Reverse the order of the axises.
*/
void transpose() {
_data = _data.transpose();
auto res = getResolution();
auto off = getOffset();
setResolution(res.z(), res.y(), res.x());
setOffset(off.z(), off.y(), off.x());
setBoundingBoxDirty();
}
/**
* Cut a subvolume of this ExplicitVolume<ValueType>.
*
* @param boundingBox
* The bounding box of the requested subvolume. The target gets
* resized to be at least that large, but might be larger to
* fit all the voxels that are intersecting the requested
* subvolume.
* @param target
* An explicit volume to fill.
*/
void cut(const util::box<float, 3>& boundingBox, ExplicitVolume<ValueType>& target) {
util::box<float, 3> intersection = boundingBox.intersection(getBoundingBox());
if (intersection.isZero()) {
target = ExplicitVolume<ValueType>();
return;
}
// the discrete offset of the requested region in this volume
util::point<unsigned int, 3> offset =
(intersection.min() - getBoundingBox().min())/
getResolution();
// the discrete size of the requested region
util::point<unsigned int, 3> size(
std::ceil(intersection.width() /getResolution().x()),
std::ceil(intersection.height()/getResolution().y()),
std::ceil(intersection.depth() /getResolution().z()));
target = ExplicitVolume<ValueType>(size.x(), size.y(), size.z());
target.setResolution(getResolution());
target.setOffset(getOffset() + offset*getResolution());
typedef typename data_type::difference_type Shape;
target.data() = data().subarray(
Shape(
offset.x(),
offset.y(),
offset.z()),
Shape(
offset.x() + size.x(),
offset.y() + size.y(),
offset.z() + size.z()));
}
protected:
util::box<unsigned int,3> computeDiscreteBoundingBox() const override {
return util::box<unsigned int,3>(0, 0, 0, _data.shape(0), _data.shape(1), _data.shape(2));
}
private:
data_type _data;
};
#ifdef HAVE_NUMPY
template <typename ValueType>
ExplicitVolume<ValueType>
volumeFromNumpyArray(PyObject* a) {
PyArray_Descr* desc = PyArray_DescrFromType(numpy_type_traits<ValueType>::getNumpyType());
PyArrayObject* array = (PyArrayObject*)PyArray_FromAny(
a,
desc,
0, 0, // min and max dimension, we check that later
0, // requirements
0);
if (array == NULL)
UTIL_THROW_EXCEPTION(
UsageError,
"given numpy array is not of type " << typeName(ValueType()));
int dims = PyArray_NDIM(array);
if (dims != 3)
UTIL_THROW_EXCEPTION(
UsageError,
"only arrays of dimensions 3 are supported.");
size_t d = PyArray_DIM(array, 0);
size_t h = PyArray_DIM(array, 1);
size_t w = PyArray_DIM(array, 2);
auto volume = ExplicitVolume<ValueType>(w, h, d);
for (size_t z = 0; z < d; z++)
for (size_t y = 0; y < h; y++)
for (size_t x = 0; x < w; x++)
volume(x,y,z) = *static_cast<ValueType*>(PyArray_GETPTR3(array, z, y, x));
return volume;
}
#endif
#endif // IMAGEPROCESSING_EXPLICIT_VOLUME_H__