-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathROIData.h
469 lines (374 loc) · 16.8 KB
/
ROIData.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2013 Carlos Becker //
// Ecole Polytechnique Federale de Lausanne //
// Contact <[email protected]> for comments & bug reports //
// //
// This program is free software: you can redistribute it and/or modify //
// it under the terms of the version 3 of the GNU General Public License //
// as published by the Free Software Foundation. //
// //
// This program is distributed in the hope that it will be useful, but //
// WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU //
// General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
//////////////////////////////////////////////////////////////////////////////////
#ifndef _ROI_DATA_H_
#define _ROI_DATA_H_
#include <Eigen/Dense>
#include <vector>
#include "globaldefs.h"
#include <Matrix3D.h>
#include "IntegralImage.h"
#if USE_SUPERVOXELS
#include <slic/SuperVoxeler.h>
#endif
#include "auxItk/AllEigenVectorsOfHessian.h"
// specifies a ROI within an image
struct ROICoordinates
{
unsigned x1,y1,z1; // ROI includes both 1 and 2
unsigned x2,y2,z2;
void printInfo()
{
std::cout << "subROI: " << x1 << " " << y1 << " " << z1 << " ==> " << x2 << " " << y2 << " " << z2 << std::endl;
}
};
/**
* Holds information for each Region of Interest, namely:
* - Rotation matrices
* - Raw image
* - Ground truth image
* - Integral images / channels
* - Mean-variance normalization (if USE_MEANVAR_NORMALIZATION != 0)
*
*
* -- IMPORTANT --: you need to specify how labels are encoded in the ground truth
* for it to function properly. The defaults may not match the
* ground truth.
* Use setGTNegativeSampleLabel() and setGTPositiveSampleLabel()
*/
struct ROIData
{
public:
typedef AllEigenVectorsOfHessian::EigenVectorImageType ItkEigenVectorImageType;
private:
// rotation matrices, as Itk matrices, one per channel
ItkEigenVectorImageType::Pointer rotMatricesImg;
float mZAnisotropyFactor;
bool mInitialized; // if it has been initialized with init() or not
// positive and negative sample values in ground truth
GTPixelType mGTNegativeSampleLabel, mGTPositiveSampleLabel;
public:
// type of channels, needed specially for mean-var norm
enum class ChannelType : char { GAUSS, // smoothed gradient / image itself
GRADIENT, // gradient / gradient magnitude
HESS_EIGVAL, // hessian eigenvalues
STENS_EIGVAL, // structure tensor eigenvalues
OTHER // only working when not using mean-var normalization
};
// saves II type for each added II
std::vector< ChannelType > integralImageType;
#if USE_SUPERVOXELS
SuperVoxeler<ImagePixelType> SVox;
std::vector<UIntPoint3D> svCentroids;
#endif
typedef IntegralImage<IntegralImagePixelType> IntegralImageType;
// each integral image channel
std::vector< IntegralImageType > integralImages;
// for now we need this to compute rotMatrices
Matrix3D<ImagePixelType> rawImage;
// ground truth
// see GTIgnoreLabel, etc in globaldefs.h
Matrix3D<GTPixelType> gtImage;
// ITK stores matrices in row major, but the eigenvectors are in the rows
// of the vnl matrix
typedef Eigen::Matrix<float,3,3,Eigen::ColMajor> RotationMatrixType;
// this will point to rotMatricesImg's data
const RotationMatrixType *rotMatrices;
#if USE_MEANVAR_NORMALIZATION
// current implementation is very memory-intensive!!
// this is for the raw image, so we can re-use it. one element per voxel
std::vector<IntegralImagePixelType> rawImgMean; // mean around every voxel
std::vector<IntegralImagePixelType> rawImgInvStdDev; // 1 / std dev
// one per integral image, containing one element for each voxel
std::vector< std::vector<IntegralImagePixelType> > meanVarMult; // to multiply by
std::vector< std::vector<IntegralImagePixelType> > meanVarSubtract; // to subtract
#endif
//////////////////////////////////////////////////////////////////////////////////////
inline bool initialized() const { return mInitialized; }
inline float zAnisotropyFactor() const { return mZAnisotropyFactor; }
inline GTPixelType gtNegativeSampleLabel() const { return mGTNegativeSampleLabel; }
inline GTPixelType gtPositiveSampleLabel() const { return mGTPositiveSampleLabel; }
void setGTNegativeSampleLabel( GTPixelType lbl ) { mGTNegativeSampleLabel = lbl; }
void setGTPositiveSampleLabel( GTPixelType lbl ) { mGTPositiveSampleLabel = lbl; }
// removes NaN and Inf from rotation matrices, if any
// This may be needed because ITK may produce such weird output
// numElemen is how many rotation matrices are stored in _rotMatrices
static void removeRotationMatrixNaNs( RotationMatrixType *_rotMatrices, long int numElem )
{
for (long int i=0; i < numElem; i++)
{
auto total = _rotMatrices[i].sum();
if ( std::isnan(total) || std::isinf(total) )
_rotMatrices[i].setIdentity();
}
}
// computes rotation matrices from raw image
void updateRotationMatrices( const float rotHessianSigma )
{
// compute rotation matrix
rotMatricesImg =
AllEigenVectorsOfHessian::allEigenVectorsOfHessian<ImagePixelType>(
rotHessianSigma, mZAnisotropyFactor, rawImage.asItkImage(),
AllEigenVectorsOfHessian::EByMagnitude );
// remove NaNs if any
RotationMatrixType *rmPtr = (RotationMatrixType *) rotMatricesImg->GetPixelContainer()->GetImportPointer();
removeRotationMatrixNaNs( rmPtr, rotMatricesImg->GetPixelContainer()->Size() );
// convert pointers
rotMatrices = (const RotationMatrixType *) rmPtr;
}
#if USE_MEANVAR_NORMALIZATION
// compute meanVarMult and meanVarSubtract from data
void addMeanVarianceNormParameters( ChannelType chanType )
{
// see if we need to compute raw-image stats
if ( rawImgMean.size() != rawImage.numElem() )
{
// we need normal and squared II
IntegralImageType rawII;
IntegralImageType squaredII;
{
// temporary squared image
Matrix3D<IntegralImagePixelType> squaredImg;
squaredImg.reallocSizeLike( rawImage );
for (unsigned i=0; i < rawImage.numElem(); i++)
{
const IntegralImagePixelType val = rawImage.data()[i];
squaredImg.data()[i] = val*val;
}
// compute squared II
squaredII.compute( squaredImg );
rawII.compute( rawImage );
}
const int boxSize = MEANVAR_NORMALIZATION_CUBE_RADIUS;
const int Vwidth = rawImage.width();
const int Vheight = rawImage.height();
const int Vdepth = rawImage.depth();
rawImgMean.resize( rawImage.numElem() );
rawImgInvStdDev.resize( rawImage.numElem() );
#pragma omp parallel for
for (unsigned i=0; i < rawImage.numElem(); i++)
{
unsigned _x,_y,_z;
rawImage.idxToCoord( i, _x, _y, _z );
int x = _x;
int y = _y;
int z = _z;
// check image borders
if ( x - boxSize <= 1 ) x = boxSize + 1;
if ( y - boxSize <= 1 ) y = boxSize + 1;
if ( z - boxSize <= 1 ) z = boxSize + 1;
if ( x + boxSize >= Vwidth ) x = Vwidth - boxSize - 1;
if ( y + boxSize >= Vheight) y = Vheight - boxSize - 1;
if ( z + boxSize >= Vdepth ) z = Vdepth - boxSize - 1;
IntegralImagePixelType mean = rawII.centeredSumNormalized( x, y, z, boxSize, boxSize, boxSize, 0, 1 );
IntegralImagePixelType stdDev = sqrt( squaredII.centeredSumNormalized( x, y, z, boxSize, boxSize, boxSize, 0, 1 ) - mean*mean );
#if 0
IntegralImageType realStdDev = 0;
// compare std dev vs real one
for (unsigned qx=x - boxSize; qx <= x + boxSize; qx++)
for (unsigned qy=y - boxSize; qy <= y + boxSize; qy++)
for (unsigned qz=z - boxSize; qz <= z + boxSize; qz++)
{
realStdDev += pow(combo.rawImage(qx,qy,qz) - mean, 2);
}
double fR = 2*boxSize + 1;
realStdDev = sqrt(realStdDev / (fR * fR * fR));
qDebug("Real vs computed: %f %f", realStdDev, stdDev);
#endif
rawImgMean[i] = mean;
rawImgInvStdDev[i] = 1.0 / (stdDev + 1e-6);
}
}
// add channels
meanVarMult.emplace_back();
meanVarSubtract.emplace_back();
switch( chanType )
{
case ChannelType::GAUSS:
// mean-std dev not zero
meanVarSubtract.back() = rawImgMean;
meanVarMult.back() = rawImgInvStdDev;
qDebug("Mean variance normalization Gauss");
break;
case ChannelType::GRADIENT:
// mean zero, std dev
meanVarSubtract.back().resize( rawImgMean.size() );
for (auto &v: meanVarSubtract.back()) v = 0;
meanVarMult.back() = rawImgInvStdDev;
qDebug("Mean variance normalization Gradient");
break;
case ChannelType::HESS_EIGVAL:
// mean zero, std dev
meanVarSubtract.back().resize( rawImgMean.size() );
for (auto &v: meanVarSubtract.back()) v = 0;
meanVarMult.back() = rawImgInvStdDev;
qDebug("Mean variance normalization Hess Eigval");
break;
case ChannelType::STENS_EIGVAL:
// mean zero, std dev squared
meanVarSubtract.back().resize( rawImgMean.size() );
for (auto &v: meanVarSubtract.back()) v = 0;
meanVarMult.back() = rawImgInvStdDev;
for (auto &v: meanVarMult.back()) v = v * v;
qDebug("Mean variance normalization STens Eigval");
break;
default:
qFatal("Channel type of uknown type when using mean-variance normalization");
}
}
#endif
// initialize from images, using move semantics
// (except if cachedRotMatrices is provided, which is a pointer
// and it must remain valid for training and/or prediction )
// If used, make sure cachedRotMatrices doesn't contain any NaNs or Infs
void init( Matrix3D<ImagePixelType> &&rawImg,
Matrix3D<ImagePixelType> &>Img,
const float zAnisotropyFactor = 1.0,
const float rotHessianSigma = 3.5,
const RotationMatrixType *cachedRotMatrices = nullptr )
{
// in case we had other info before
freeIntegralImages();
mZAnisotropyFactor = zAnisotropyFactor;
rawImage = std::move(rawImg);
gtImage = std::move(gtImg);
if ( cachedRotMatrices == nullptr )
updateRotationMatrices( rotHessianSigma );
else
rotMatrices = cachedRotMatrices;
mInitialized = true;
}
// initialize from pointers, which must remain valid
void init( ImagePixelType *rawImgPtr,
ImagePixelType *gtImgPtr,
IntegralImagePixelType **intImgPtr,
unsigned numII,
unsigned width, unsigned height, unsigned depth,
const float zAnisotropyFactor = 1.0,
const float rotHessianSigma = 3.5,
const RotationMatrixType *cachedRotMatrices = nullptr )
{
#if USE_MEANVAR_NORMALIZATION
qFatal("init() with pointers not supported with mean variance normalization, needs fix!");
#endif
// in case we had other info before
freeIntegralImages();
mZAnisotropyFactor = zAnisotropyFactor;
rawImage.fromSharedData( rawImgPtr, width, height, depth );
gtImage.fromSharedData( gtImgPtr, width, height, depth );
if ( cachedRotMatrices == nullptr )
updateRotationMatrices( rotHessianSigma );
else
rotMatrices = cachedRotMatrices;
for (unsigned i=0; i < numII; i++)
{
integralImages.push_back( IntegralImageType() );
integralImages.back().fromSharedData( intImgPtr[i],
width,
height,
depth );
}
mInitialized = true;
}
// it won't free pointer data
void addII( IntegralImagePixelType *iiDataPtr, ChannelType chanType = ChannelType::OTHER )
{
if ( rawImage.isEmpty() )
qFatal("Trued to add integral image to non-initialized ROIData");
#if USE_MEANVAR_NORMALIZATION
if ( chanType == ChannelType::OTHER )
qFatal("Channel type cannot be OTHER with mean-var normalization");
#endif
integralImages.push_back( IntegralImageType() );
integralImages.back().fromSharedData( iiDataPtr,
rawImage.width(),
rawImage.height(),
rawImage.depth() );
integralImageType.push_back( chanType );
#if USE_MEANVAR_NORMALIZATION
addMeanVarianceNormParameters( chanType );
#endif
}
// to be used with std::move(), we love move semantics
void addII( IntegralImageType &&iiDataPtr, ChannelType chanType = ChannelType::OTHER )
{
if ( rawImage.isEmpty() )
qFatal("Trued to add integral image to non-initialized ROIData");
#if USE_MEANVAR_NORMALIZATION
if ( chanType == ChannelType::OTHER )
qFatal("Channel type cannot be OTHER with mean-var normalization");
#endif
integralImages.push_back( std::move(iiDataPtr) );
integralImageType.push_back( chanType );
#if USE_MEANVAR_NORMALIZATION
addMeanVarianceNormParameters( chanType );
#endif
}
void freeIntegralImages()
{
integralImages.clear();
#if USE_MEANVAR_NORMALIZATION
meanVarMult.clear();
meanVarSubtract.clear();
integralImageType.clear();
#endif
}
~ROIData()
{
freeIntegralImages();
}
ROIData()
{
mZAnisotropyFactor = 0; // indicates invalid anisotropy factor
mInitialized = false;
// defaults for labels
mGTNegativeSampleLabel = 0;
mGTPositiveSampleLabel = 1;
}
#if defined(_MSC_VER) && _MSC_VER <= 1700
private:
ROIData(ROIData const &);
ROIData & operator=(ROIData const &);
public:
#endif
// inverts orientation of voxel with ID idx
inline void invertOrientation( unsigned idx )
{
// we need to un-const it first
// we want to keep y-direction, but invert z
// so we have to invert x and z only
((RotationMatrixType *)rotMatrices)[idx].col(0) = - ((RotationMatrixType *)rotMatrices)[idx].col(0);
((RotationMatrixType *)rotMatrices)[idx].col(2) = - ((RotationMatrixType *)rotMatrices)[idx].col(2);
}
// invert Z-orientation of orientation estimate
// used for synapses in particular. Inverts orientation of all voxels
void invertOrientation()
{
for ( unsigned i=0; i < rawImage.numElem(); i++ )
invertOrientation(i);
}
// for debugging only
void setAllOrientationsToIdentity()
{
for ( unsigned i=0; i < rawImage.numElem(); i++ )
{
// un-const it first
((RotationMatrixType *)rotMatrices)[i].setIdentity();
}
}
};
#endif