/
Helpers.cpp
executable file
·166 lines (134 loc) · 5.93 KB
/
Helpers.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
/*=========================================================================
*
* Copyright David Doria 2011 daviddoria@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#include "Helpers.h"
// ITK
#include "itkImageRegionIterator.h"
#include "itkVectorMagnitudeImageFilter.h"
#include "itkRescaleIntensityImageFilter.h"
// VTK
#include <vtkIdList.h>
#include <vtkKdTree.h>
#include <vtkMath.h>
namespace Helpers
{
// Convert a vector ITK image to a VTK image for display
void ITKImagetoVTKImage(FloatVectorImageType::Pointer image, vtkImageData* outputImage)
{
//std::cout << "ITKImagetoVTKImage()" << std::endl;
if(image->GetNumberOfComponentsPerPixel() >= 3)
{
ITKImagetoVTKRGBImage(image, outputImage);
}
else
{
ITKImagetoVTKMagnitudeImage(image, outputImage);
}
}
// Convert a vector ITK image to a VTK image for display
void ITKImagetoVTKRGBImage(FloatVectorImageType::Pointer image, vtkImageData* outputImage)
{
// This function assumes an ND (with N>3) image has the first 3 channels as RGB and extra information in the remaining channels.
//std::cout << "ITKImagetoVTKRGBImage()" << std::endl;
if(image->GetNumberOfComponentsPerPixel() < 3)
{
std::cerr << "The input image has " << image->GetNumberOfComponentsPerPixel() << " components, but at least 3 are required." << std::endl;
return;
}
// Setup and allocate the image data
outputImage->SetNumberOfScalarComponents(3);
outputImage->SetScalarTypeToUnsignedChar();
outputImage->SetDimensions(image->GetLargestPossibleRegion().GetSize()[0],
image->GetLargestPossibleRegion().GetSize()[1],
1);
outputImage->AllocateScalars();
// Copy all of the input image pixels to the output image
itk::ImageRegionConstIteratorWithIndex<FloatVectorImageType> imageIterator(image,image->GetLargestPossibleRegion());
imageIterator.GoToBegin();
while(!imageIterator.IsAtEnd())
{
unsigned char* pixel = static_cast<unsigned char*>(outputImage->GetScalarPointer(imageIterator.GetIndex()[0],
imageIterator.GetIndex()[1],0));
for(unsigned int component = 0; component < 3; component++)
{
pixel[component] = static_cast<unsigned char>(imageIterator.Get()[component]);
}
++imageIterator;
}
}
// Convert a vector ITK image to a VTK image for display
void ITKImagetoVTKMagnitudeImage(FloatVectorImageType::Pointer image, vtkImageData* outputImage)
{
//std::cout << "ITKImagetoVTKMagnitudeImage()" << std::endl;
// Compute the magnitude of the ITK image
typedef itk::VectorMagnitudeImageFilter<
FloatVectorImageType, FloatScalarImageType > VectorMagnitudeFilterType;
// Create and setup a magnitude filter
VectorMagnitudeFilterType::Pointer magnitudeFilter = VectorMagnitudeFilterType::New();
magnitudeFilter->SetInput( image );
magnitudeFilter->Update();
// Rescale and cast for display
typedef itk::RescaleIntensityImageFilter<
FloatScalarImageType, UnsignedCharScalarImageType > RescaleFilterType;
RescaleFilterType::Pointer rescaleFilter = RescaleFilterType::New();
rescaleFilter->SetOutputMinimum(0);
rescaleFilter->SetOutputMaximum(255);
rescaleFilter->SetInput( magnitudeFilter->GetOutput() );
rescaleFilter->Update();
// Setup and allocate the VTK image
outputImage->SetNumberOfScalarComponents(1);
outputImage->SetScalarTypeToUnsignedChar();
outputImage->SetDimensions(image->GetLargestPossibleRegion().GetSize()[0],
image->GetLargestPossibleRegion().GetSize()[1],
1);
outputImage->AllocateScalars();
// Copy all of the scaled magnitudes to the output image
itk::ImageRegionConstIteratorWithIndex<UnsignedCharScalarImageType> imageIterator(rescaleFilter->GetOutput(), rescaleFilter->GetOutput()->GetLargestPossibleRegion());
imageIterator.GoToBegin();
while(!imageIterator.IsAtEnd())
{
unsigned char* pixel = static_cast<unsigned char*>(outputImage->GetScalarPointer(imageIterator.GetIndex()[0],
imageIterator.GetIndex()[1],0));
pixel[0] = imageIterator.Get();
++imageIterator;
}
}
float ComputeAverageSpacing(vtkPoints* points)
{
float sumOfDistances = 0.;
//Create the tree
vtkSmartPointer<vtkKdTree> pointTree = vtkSmartPointer<vtkKdTree>::New();
pointTree->BuildLocatorFromPoints(points);
for(vtkIdType i = 0; i < points->GetNumberOfPoints(); ++i)
{
// Get the coordinates of the current point
double queryPoint[3];
points->GetPoint(i,queryPoint);
// Find the 2 closest points (the first closest will be exactly the query point)
vtkSmartPointer<vtkIdList> result = vtkSmartPointer<vtkIdList>::New();
pointTree->FindClosestNPoints(2, queryPoint, result);
double closestPoint[3];
points->GetPoint(result->GetId(1), closestPoint);
float squaredDistance = vtkMath::Distance2BetweenPoints(queryPoint, closestPoint);
// Take the square root to get the Euclidean distance between the points.
float distance = sqrt(squaredDistance);
sumOfDistances += distance;
}
float averageDistance = sumOfDistances / static_cast<float>(points->GetNumberOfPoints());
return averageDistance;
}
} // end namespace