-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathitkGPUGradientOrientationMatching.h
305 lines (222 loc) · 12.5 KB
/
itkGPUGradientOrientationMatching.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
/*=========================================================================
Ibis Neuronav
Copyright (c) Simon Drouin, Anna Kochanowska, Louis Collins.
All rights reserved.
See Copyright.txt or http://ibisneuronav.org/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
// Thanks to Dante De Nigris for writing this class
#ifndef __itkGPUGradientOrientationMatching_h
#define __itkGPUGradientOrientationMatching_h
#include <itkObject.h>
#include <itkOpenCLUtil.h>
#include <itkCovariantVector.h>
#include <itkMatrixOffsetTransformBase.h>
#include <itkGaussianDerivativeOperator.h>
#include <itkImage.h>
#include <itkSampleToHistogramFilter.h>
#include <itkListSample.h>
#include <itkHistogram.h>
#include <itkImageSample.h>
#include <itkImageSamplerBase.h>
#include <itkImageRandomSamplerBase.h>
#include <itkImageRandomSampler.h>
#include <itkImageGridSampler.h>
#include <itkImageFullSampler.h>
#include <itkImageMaskSpatialObject.h>
#include <itkImageRegionIteratorWithIndex.h>
namespace itk
{
template< class TFixedImage, class TMovingImage >
class ITK_EXPORT GPUOrientationMatchingMatrixTransformationSparseMask :
public Object
{
public:
/** Standard class typedefs. */
typedef GPUOrientationMatchingMatrixTransformationSparseMask Self;
typedef Object Superclass;
typedef SmartPointer< Self > Pointer;
typedef SmartPointer< const Self > ConstPointer;
typedef float InternalRealType;
/** Method for creation through the object factory. */
itkNewMacro(Self);
/** Run-time type information (and related methods). */
itkTypeMacro(GPUOrientationMatchingMatrixTransformationSparseMask,
Object);
/** FixedImage image type. */
typedef TFixedImage FixedImageType;
typedef typename FixedImageType::PixelType FixedImagePixelType;
typedef typename FixedImageType::Pointer FixedImagePointer;
typedef typename FixedImageType::ConstPointer FixedImageConstPointer;
itkGetObjectMacro(FixedImage, FixedImageType);
itkSetObjectMacro(FixedImage, FixedImageType);
/** MovingImage image type. */
typedef TMovingImage MovingImageType;
typedef typename MovingImageType::PixelType MovingImagePixelType;
typedef typename MovingImageType::Pointer MovingImagePointer;
typedef typename MovingImageType::ConstPointer MovingImageConstPointer;
typedef typename MovingImageType::DirectionType MovingImageDirectionType;
typedef typename MovingImageType::PointType MovingImagePointType;
itkGetObjectMacro(MovingImage, MovingImageType);
itkSetObjectMacro(MovingImage, MovingImageType);
/** Extract dimension from input image. */
itkStaticConstMacro(FixedImageDimension, unsigned int,
TFixedImage::ImageDimension);
itkStaticConstMacro(MovingImageDimension, unsigned int,
TMovingImage::ImageDimension);
typedef Image<InternalRealType, FixedImageDimension> RealImageType;
typedef typename RealImageType::Pointer RealImagePointer;
typedef CovariantVector< InternalRealType,itkGetStaticConstMacro(FixedImageDimension) >
FixedGradientType;
typedef Image< FixedGradientType, itkGetStaticConstMacro(FixedImageDimension) >
FixedImageGradientType;
typedef typename FixedImageGradientType::Pointer FixedImageGradientPointer;
typedef typename FixedImageGradientType::ConstPointer FixedImageGradientConstPointer;
typedef CovariantVector< InternalRealType,itkGetStaticConstMacro(MovingImageDimension) >
MovingGradientType;
typedef Image< MovingGradientType , itkGetStaticConstMacro(MovingImageDimension) >
MovingImageGradientType;
typedef typename MovingImageGradientType::Pointer MovingImageGradientPointer;
typedef typename MovingImageGradientType::ConstPointer MovingImageGradientConstPointer;
typedef double TransformRealType;
typedef MatrixOffsetTransformBase< TransformRealType,
FixedImageDimension, MovingImageDimension>
MatrixTransformType;
typedef typename MatrixTransformType::Pointer MatrixTransformPointer;
typedef typename MatrixTransformType::ConstPointer MatrixTransformConstPointer;
typedef typename MatrixTransformType::MatrixType TransformMatrixType;
typedef typename MatrixTransformType::InverseMatrixType TransformInverseMatrixType;
typedef typename MatrixTransformType::OutputVectorType TransformOffsetType;
itkGetConstObjectMacro(Transform, MatrixTransformType);
itkSetObjectMacro(Transform, MatrixTransformType);
itkGetConstMacro(MetricValue, InternalRealType);
itkSetMacro(NumberOfPixels, unsigned int);
itkSetMacro(N, unsigned int);
itkSetMacro(Percentile, double);
itkSetMacro(ComputeMask, bool);
itkSetMacro(MaskThreshold, double);
itkSetMacro(GradientScale, double);
itkSetMacro(Debug, bool);
typedef GaussianDerivativeOperator<InternalRealType, FixedImageDimension> FixedDerivativeOperatorType;
typedef GaussianDerivativeOperator<InternalRealType, MovingImageDimension> MovingDerivativeOperatorType;
typedef itk::Vector<InternalRealType, 1> MeasurementVectorType;
typedef itk::Statistics::ListSample< MeasurementVectorType > FixedGradientMagnitudeSampleType;
typedef itk::Statistics::ListSample< itk::Vector<unsigned int, 1> > IdxSampleType;
typedef itk::Statistics::Histogram< float,
itk::Statistics::DenseFrequencyContainer2 > HistogramType;
typedef itk::Statistics::SampleToHistogramFilter<FixedGradientMagnitudeSampleType, HistogramType>
SampleToHistogramFilterType;
/** Image Sampler typedefs */
using ImageSamplerType = itk::ImageSamplerBase< FixedImageType >;
using RandomImageSamplerType = itk::ImageRandomSampler< FixedImageType >;
using GridImageSamplerType = itk::ImageGridSampler< FixedImageType >;
using FullImageSamplerType = itk::ImageFullSampler< FixedImageType >;
using SampleContainerType = typename ImageSamplerType::ImageSampleContainerType;
using SampleType = typename ImageSamplerType::ImageSampleType;
using GridSpacingType = typename GridImageSamplerType::SampleGridSpacingType;
enum SamplingStrategyType { RANDOM = 0, GRID = 1, FULL = 2 };
itkSetMacro(SamplingStrategy, SamplingStrategyType);
void SetSamplingStrategyToRandom() { this->m_SamplingStrategy = RANDOM; }
void SetSamplingStrategyToGrid() { this->m_SamplingStrategy = GRID; }
void SetSamplingStrategyToFull() { this->m_SamplingStrategy = FULL; }
using FixedImageMaskSpatialObjectType = itk::ImageMaskSpatialObject< FixedImageDimension >;
using FixedImageMaskSpatialObjectPointer = typename FixedImageMaskSpatialObjectType::Pointer;
using FixedImageMaskType = typename FixedImageMaskSpatialObjectType::ImageType;
using FixedImageMaskPointer = typename FixedImageMaskType::Pointer;
using FixedImageMaskPixelType = typename FixedImageMaskType::PixelType;
using MovingImageMaskSpatialObjectType = itk::ImageMaskSpatialObject< MovingImageDimension >;
using MovingImageMaskSpatialObjectPointer = typename MovingImageMaskSpatialObjectType::Pointer;
using MovingImageMaskType = typename MovingImageMaskSpatialObjectType::ImageType;
using MovingImageMaskPointer = typename MovingImageMaskType::Pointer;
using MovingImageMaskPixelType = typename MovingImageMaskType::PixelType;
itkSetMacro(FixedImageMaskSpatialObject, FixedImageMaskSpatialObjectPointer);
itkSetMacro(MovingImageMaskSpatialObject, MovingImageMaskSpatialObjectPointer);
itkSetMacro(UseFixedImageMask, bool);
itkSetMacro(UseMovingImageMask, bool);
using FixedImageMaskIteratorType = itk::ImageRegionConstIteratorWithIndex< FixedImageMaskType >;
using FixedImageIteratorType = itk::ImageRegionConstIteratorWithIndex< FixedImageType >;
using MovingImageMaskIteratorType = itk::ImageRegionConstIteratorWithIndex< MovingImageMaskType >;
using MovingImageIteratorType = itk::ImageRegionConstIteratorWithIndex< MovingImageType >;
void Update(void);
unsigned int NextPow2( unsigned int x );
protected:
GPUOrientationMatchingMatrixTransformationSparseMask();
~GPUOrientationMatchingMatrixTransformationSparseMask();
void InitializeGPUContext(void);
void PrintSelf(std::ostream & os, Indent indent) const override;
void ComputeFixedImageGradient(void);
void ComputeMovingImageGradient(void);
void CreateGPUVariablesForCostFunction(void);
void UpdateGPUTransformVariables(void);
cl_kernel CreateKernelFromFile(const char * filename, const char * cPreamble, const char * kernelname, const char * cOptions);
cl_kernel CreateKernelFromString(const char * cOriginalSourceString, const char * cPreamble, const char * kernelname, const char * cOptions);
void PrepareGPUImages(void);
unsigned int m_NumberOfPixels;
double m_Percentile;
unsigned int m_N;
double m_GradientScale;
// m_ComputeMask: when true only select strong gradient magnitudes
bool m_ComputeMask;
double m_MaskThreshold;
bool m_Debug;
unsigned int m_Blocks;
unsigned int m_Threads;
SamplingStrategyType m_SamplingStrategy;
FixedImagePointer m_FixedImage;
cl_mem m_FixedImageGPUBuffer;
MovingImagePointer m_MovingImage;
cl_mem m_MovingImageGPUBuffer;
std::vector<InternalRealType*> m_CPUDerivOperatorBuffers;
std::vector<cl_mem> m_GPUDerivOperatorBuffers;
cl_mem m_FixedImageGradientGPUBuffer;
cl_mem m_MovingImageGradientGPUBuffer;
cl_mem m_MovingImageGradientGPUImage;
cl_mem m_MovingImageMaskGPUBuffer;
cl_mem m_FixedImageMaskGPUBuffer;
InternalRealType m_MetricValue;
MatrixTransformConstPointer m_Transform;
TransformMatrixType m_TransformMatrix;
TransformOffsetType m_TransformOffset;
InternalRealType* m_cpuDummy;
cl_mem m_gpuDummy;
unsigned int m_NbrPixelsInMask;
InternalRealType * m_cpuFixedGradientSamples;
cl_mem m_gpuFixedGradientSamples;
InternalRealType * m_cpuFixedLocationSamples;
cl_mem m_gpuFixedLocationSamples;
InternalRealType * m_cpuMovingGradientImageBuffer;
InternalRealType* m_cpuMetricAccum;
cl_mem m_gpuMetricAccum;
cl_kernel m_OrientationMatchingKernel;
cl_kernel m_GradientKernel;
vnl_matrix_fixed<TransformRealType, MovingImageDimension, MovingImageDimension> m_locToIdx;
vnl_matrix_fixed<TransformRealType, MovingImageDimension, MovingImageDimension> m_matrixDummy;
vnl_matrix_fixed<TransformRealType, MovingImageDimension, MovingImageDimension> m_matrixTranspose;
vnl_vector_fixed<TransformRealType, MovingImageDimension> m_mOrigin;
vnl_vector_fixed<TransformRealType, MovingImageDimension> m_OffsetOrigin;
vnl_vector_fixed<TransformRealType, MovingImageDimension> m_vectorDummy;
cl_platform_id m_Platform;
cl_context m_Context;
cl_device_id* m_Devices;
cl_command_queue* m_CommandQueue;
cl_program m_Program;
cl_uint m_NumberOfDevices, m_NumberOfPlatforms;
FixedImageMaskSpatialObjectPointer m_FixedImageMaskSpatialObject;
MovingImageMaskSpatialObjectPointer m_MovingImageMaskSpatialObject;
// m_UseFixedImageMask: when true samples gradients from masked region in FixedImage
bool m_UseFixedImageMask;
// m_UseMovingImageMask: when true samples gradients from masked region in MovingImage
bool m_UseMovingImageMask;
InternalRealType * m_cpuMovingImageBuffer;
InternalRealType * m_cpuFixedImageBuffer;
private:
GPUOrientationMatchingMatrixTransformationSparseMask(const Self &); //purposely not implemented
void operator=(const Self &); //purposely not implemented
};
} // end namespace itk
#ifndef ITK_MANUAL_INSTANTIATION
#include "itkGPUGradientOrientationMatching.hxx"
#endif
#endif