-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsynapseresamplerdialog.cpp
293 lines (236 loc) · 11.3 KB
/
synapseresamplerdialog.cpp
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
#include "synapseresamplerdialog.h"
#include "ui_synapseresamplerdialog.h"
#include "itkImage.h"
#include "itkVectorImage.h"
#include "itkImageFileReader.h"
#include "itkExtractImageFilter.h"
#include "itkFixedCenterOfRotationAffineTransform.h"
#include "itkBSplineInterpolateImageFunction.h"
#include "itkLinearInterpolateImageFunction.h"
#include "itkNearestNeighborInterpolateImageFunction.h"
#include "itkWindowedSincInterpolateImageFunction.h"
#include "itkResampleImageFilter.h"
#include "itkGradientMagnitudeRecursiveGaussianImageFilter.h"
#include "itkMirrorPadImageFilter.h"
#include <QMessageBox>
#include <QFileDialog>
#include <QProcess>
typedef itk::Image<unsigned char, 3> ImageType;
typedef itk::ImageFileReader<ImageType> ReaderType;
typedef itk::ImageFileWriter<ImageType> WriterType;
typedef itk::VectorImage<float, 3> OrientImageType;
/* Generate a (non-unique) coordinate rotation matrix, so that z-axis in the
coordinates corresponds to direction (x, y, z) in the original coordinates.
This one uses rotation about y-axis and then z-axis.
*/
static itk::Matrix<double,3,3> genRotMtx_001_to_xyz(double x, double y, double z)
{
double r = sqrt(x * x + y * y + z * z);
if(r == 0) {
std::string err("[genRotMtx_001_to_xyz] Error: x, y, z are all zeros!\n");
fprintf(stderr, "%s\n", err.c_str());
throw err;
}
x = x / r;
y = y / r;
z = z / r;
double cos_ay = z;
double sin_ay = sqrt(1 - z * z);
double cos_az = x / sin_ay;
double sin_az = y / sin_ay;
itk::Matrix<double,3,3> Ry, Rz, R;
Ry.Fill(0);
Ry(0, 0) = cos_ay;
Ry(0, 2) = sin_ay;
Ry(1, 1) = 1;
Ry(2, 0) = -sin_ay;
Ry(2, 2) = cos_ay;
Rz.Fill(0);
Rz(0, 0) = cos_az;
Rz(0, 1) = -sin_az;
Rz(1, 0) = sin_az;
Rz(1, 1) = cos_az;
Rz(2, 2) = 1;
R = Rz * Ry;
return R;
}
/* Same as above, but give x, y, z as array [x, y, z].
*/
template <class T>
itk::Matrix<double,3,3> genRotMtx_001_to_xyz(const T *v)
{
return genRotMtx_001_to_xyz(v[0], v[1], v[2]);
}
// some interpolator typedefs
//typedef itk::BSplineInterpolateImageFunction BSplineInterpolator;
//typedef itk::NearestNeighborInterpolateImageFunction NNInterpolator;
/* Extract a subwindow of size specified by radii 'rad' centered at location
'loc' and orientation 'orient'. The output will be rotated so that the
z-axis of the extracted window point to 'orient' direction in the original
image (volume).
- Assume the volume (i.e. "image") is sufficiently padded.
*/
template <class TInputImagePointer, class TOutputImage, class InterpolatorType>
typename TOutputImage::Pointer
extractZRotatedSubvolume(const TInputImagePointer inputImage,
double loc_x, double loc_y, double loc_z,
double orient_x, double orient_y, double orient_z,
unsigned long rad_x, unsigned long rad_y, unsigned long rad_z, bool inverseProjection = false)
{
typedef typename TInputImagePointer::ObjectType InputImageType;
typedef TOutputImage OutputImageType;
//typedef itk::Image<float,3> FloatImageType;
typedef typename InputImageType::IndexType IndexType;
typedef typename InputImageType::SizeType SizeType;
typedef typename InputImageType::RegionType RegionType;
typedef itk::ExtractImageFilter<InputImageType,InputImageType> ExtractorType;
typedef itk::ResampleImageFilter<InputImageType,OutputImageType> ResamplerType;
typedef itk::FixedCenterOfRotationAffineTransform<double,3> TransformType;
//typedef InterpolatorTemplate<InputImageType> InterpolatorType;
//typedef itk::WindowedSincInterpolateImageFunction<InputImageType,4> InterpolatorType;
assert(InputImageType::ImageDimension == 3); // 3D volumes only
itk::Point<double,3> loc;
loc[0] = loc_x; loc[1] = loc_y; loc[2] = loc_z;
// transform
TransformType::Pointer transform = TransformType::New();
itk::Point<double,3> center;
//itk::Vector<double,3> shift;
for(int i=0; i<3; i++) {
center[i] = loc[i]; // - origin1[i];
}
transform->SetCenterOfRotationComponent(center);
itk::Matrix<double,3,3> rotMtx = genRotMtx_001_to_xyz(orient_x, orient_y, orient_z);
transform->SetMatrixComponent(rotMtx);
//std::cout << "rotMtx:\n" << rotMtx << std::endl; //debug
// interpolator
typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
//interpolator->SetSplineOrder(3);
// rotation by resampling
typename ResamplerType::Pointer resampler = ResamplerType::New();
resampler->SetDefaultPixelValue( 0 );
if (inverseProjection) {
resampler->SetTransform( (const TransformType *) transform->GetInverseTransform().GetPointer() );
}
else
resampler->SetTransform(transform);
//resampler->SetInterpolator(interpolator); // default is LinearInterpolateImageFunction
resampler->SetInput(inputImage);
resampler->SetInterpolator(interpolator);
resampler->SetOutputStartIndex( resampler->GetInput()->GetLargestPossibleRegion().GetIndex() );
resampler->SetSize( resampler->GetInput()->GetLargestPossibleRegion().GetSize() );
resampler->SetOutputOrigin( resampler->GetInput()->GetOrigin() );
resampler->SetOutputSpacing( resampler->GetInput()->GetSpacing() );
resampler->SetOutputDirection( resampler->GetInput()->GetDirection() );
//debug
//std::cout << "resampler->GetInput()->GetBufferedRegion().GetIndex():\n" << resampler->GetInput()->GetBufferedRegion().GetIndex() << std::endl;
//std::cout << "resampler->GetInput()->GetBufferedRegion().GetSize():\n" << resampler->GetInput()->GetBufferedRegion().GetSize() << std::endl;
//std::cout << "resampler->GetOutputStartIndex():\n" << resampler->GetOutputStartIndex() << std::endl;
//std::cout << "resampler->GetSize():\n" << resampler->GetSize() << std::endl;
//resampler->Update();
//std::cout << "resampler->GetOutput()->GetBufferedRegion():\n" << resampler->GetOutput()->GetBufferedRegion() << std::endl;
//std::cout << "resampler->GetInput()->GetBufferedRegion():\n" << resampler->GetInput()->GetBufferedRegion() << std::endl;
//std::cout << "resampler->GetOutput()->GetBufferedRegion():\n" << resampler->GetOutput()->GetBufferedRegion() << std::endl;
//std::cout << "resampler->GetOutput()->GetLargestPossibleRegion():\n" << resampler->GetOutput()->GetLargestPossibleRegion() << std::endl;
//std::cout << "resampler->GetOutput()->GetRequestedRegion():\n" << resampler->GetOutput()->GetRequestedRegion() << std::endl;
// finally extract the wanted subvolume
#if 0
typename ExtractorType::Pointer extractor2 = ExtractorType::New();
unsigned long dstWindowRad[3] = {rad_x, rad_y, rad_z};
IndexType origin2;
SizeType size2;
for(int i=0; i<3; i++) {
origin2[i] = (long)(center[i]+0.5) - (long)dstWindowRad[i];
size2[i] = 2 * dstWindowRad[i] + 1;
}
RegionType region2(origin2, size2);
extractor2->SetExtractionRegion(region2);
extractor2->SetInput(resampler->GetOutput());
//debug
//std::cout << "region2:\n" << region2 << std::endl;
extractor2->Update();
//debug
//std::cout << "extractor2->GetOutput()->GetBufferedRegion():\n" << extractor2->GetOutput()->GetBufferedRegion() << std::endl;
typename TOutputImage::Pointer outputImage = extractor2->GetOutput();
#else
resampler->UpdateLargestPossibleRegion();
typename TOutputImage::Pointer outputImage = resampler->GetOutput();
#endif
return outputImage;
}
SynapseResamplerDialog::SynapseResamplerDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::SynapseResamplerDialog)
{
ui->setupUi(this);
ui->butBack->setEnabled(false);
ui->butFwd->setEnabled(false);
}
void SynapseResamplerDialog::setAuxData( const Matrix3D<PixelType> *rawImg, Matrix3D<LabelType> *labelImg )
{
mRawImg = rawImg;
mLabelImg = labelImg;
}
void SynapseResamplerDialog::mainWindowClicked(unsigned int px, unsigned int py, unsigned int pz)
{
static OrientImageType::Pointer orientImg = 0;
if (orientImg.IsNull())
{
QMessageBox::information( this, "Orientation image", "No orientation image was opened previously. Please open one in order to continue." );
QString fileName = QFileDialog::getOpenFileName( 0, "Load orientation image", ".", "*.nrrd" );
if (fileName.isEmpty())
return;
std::string stdFName = fileName.toLocal8Bit().constData();
itk::ImageFileReader< OrientImageType >::Pointer reader = itk::ImageFileReader< OrientImageType >::New();
reader->SetFileName(stdFName);
reader->Update();
orientImg = reader->GetOutput();
}
// get orientation at the given point
OrientImageType::IndexType index;
index[0] = px; index[1] = py; index[2] = pz;
float vx = orientImg->GetPixel(index)[0];
float vy = orientImg->GetPixel(index)[1];
float vz = orientImg->GetPixel(index)[2];
qDebug("Orient estimate: %.2f %.2f %.2f", vx, vy, vz);
// save image
{
// image is interpolated with bspline interpolation
ImageType::Pointer rotImg = extractZRotatedSubvolume<ImageType::Pointer, ImageType, itk::BSplineInterpolateImageFunction<ImageType> >(
mRawImg->asItkImage(), px, py, pz, vx, vy, vz, 0, 0, 0, false );
WriterType::Pointer writer = WriterType::New();
writer->SetInput(rotImg);
writer->SetFileName("/tmp/rotated.tif");
writer->Update();
}
// save annot
{
// interpolated with NN
ImageType::Pointer rotImg = extractZRotatedSubvolume<ImageType::Pointer, ImageType, itk::LinearInterpolateImageFunction<ImageType> >(
mLabelImg->asItkImage(), px, py, pz, vx, vy, vz, 0, 0, 0, false );
WriterType::Pointer writer = WriterType::New();
writer->SetInput(rotImg);
writer->SetFileName("/tmp/rotated-label.tif");
writer->Update();
}
// run!
QStringList args;
args << QString("/tmp/rotated.tif"); // file itself
args << QString("/tmp/rotated-label.tif");
args << QString("%1").arg(pz); // z-value
args << QString("yes");
// this will wait until the process finishes
QProcess::execute( QFileInfo( qApp->argv()[0] ).absoluteFilePath(), args );
// so now load the annotation and perform the inverse transformation on the labels image
{
Matrix3D<LabelType> newLabels;
newLabels.load("/tmp/rotated-label.tif");
ImageType::Pointer rotImg = extractZRotatedSubvolume<ImageType::Pointer, ImageType, itk::LinearInterpolateImageFunction<ImageType> >(
newLabels.asItkImage(), px, py, pz, vx, vy, vz, 0, 0, 0, true );
// copy to labels
mLabelImg->copyFrom( rotImg );
}
}
SynapseResamplerDialog::~SynapseResamplerDialog()
{
delete ui;
}