forked from carma-center/carma_slicer_extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CarmaIsolatedConnectedFilter.cxx
139 lines (113 loc) · 4.01 KB
/
CarmaIsolatedConnectedFilter.cxx
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
/*
* CarmaIsolatedConnectedFilter.cxx
*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
#ifdef __BORLANDC__
#define ITK_LEAN_AND_MEAN
#endif
#include "itkIsolatedConnectedImageFilter.h"
#include "CarmaIsolatedConnectedFilterCLP.h"
#include "itkImage.h"
#include "itkCastImageFilter.h"
#include "itkCurvatureFlowImageFilter.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
namespace
{
typedef float InternalPixelType;
const unsigned int Dimension = 3;
typedef itk::Image< InternalPixelType, Dimension > InternalImageType;
typedef InternalImageType::PointType ImagePointType;
typedef std::vector<ImagePointType> PointList;
// Method to convert RAS point to LPS point
static itk::Point<float, 3> convertStdVectorToITKPoint(const std::vector<float> & vec)
{
itk::Point<float, 3> p;
// convert RAS to LPS
p[0] = -vec[0];
p[1] = -vec[1];
p[2] = vec[2];
return p;
}
}
int main( int argc, char *argv[] )
{
PARSE_ARGS;
typedef unsigned char OutputPixelType;
typedef itk::Image< OutputPixelType, Dimension > OutputImageType;
typedef itk::CastImageFilter< InternalImageType, OutputImageType > CastingFilterType;
CastingFilterType::Pointer caster = CastingFilterType::New();
typedef itk::ImageFileReader< InternalImageType > ReaderType;
typedef itk::ImageFileWriter< OutputImageType > WriterType;
ReaderType::Pointer reader = ReaderType::New();
WriterType::Pointer writer = WriterType::New();
reader->SetFileName( inputImage );
writer->SetFileName( outputImage );
InternalImageType::Pointer inputImagePtr = InternalImageType::New();
inputImagePtr = reader->GetOutput();
typedef itk::CurvatureFlowImageFilter< InternalImageType, InternalImageType > CurvatureFlowImageFilterType;
CurvatureFlowImageFilterType::Pointer smoothing = CurvatureFlowImageFilterType::New();
typedef itk::IsolatedConnectedImageFilter< InternalImageType, InternalImageType > ConnectedFilterType;
ConnectedFilterType::Pointer isolatedConnected = ConnectedFilterType::New();
reader->Update();
smoothing->SetInput( reader->GetOutput() );
isolatedConnected->SetInput( smoothing->GetOutput() );
caster->SetInput( isolatedConnected->GetOutput() );
writer->SetInput( caster->GetOutput() );
smoothing->SetNumberOfIterations( 5 );
smoothing->SetTimeStep( 0.125 );
PointList seeds1_list;
PointList seeds2_list;
typedef std::vector<InternalImageType::IndexType> IndexList;
isolatedConnected->FindUpperThresholdOn();
isolatedConnected->SetLower( lowerThreshold );
IndexList indexList1;
IndexList indexList2;
if( seeds1.size() > 0 && seeds2.size() > 0 )
{
seeds1_list.resize( seeds1.size() );
seeds2_list.resize( seeds2.size() );
// Convert both point lists to ITK points and convert RAS -> LPS
std::transform( seeds1.begin(), seeds1.end(),
seeds1_list.begin(), convertStdVectorToITKPoint );
std::transform( seeds2.begin(), seeds2.end(),
seeds2_list.begin(), convertStdVectorToITKPoint );
indexList1.resize( seeds1.size() );
indexList2.resize( seeds2.size() );
for( unsigned int i = 0; i<seeds1.size(); i++ )
{
inputImagePtr->TransformPhysicalPointToIndex( seeds1_list[i], indexList1[i] );
isolatedConnected->SetSeed1( indexList1[i] );
}
for( unsigned int i = 0; i<seeds2.size(); i++ )
{
inputImagePtr->TransformPhysicalPointToIndex( seeds2_list[i], indexList2[i] );
isolatedConnected->SetSeed2( indexList2[i] );
}
}
else
{
std::cerr << "Must supply at least one seed for each region" << std::endl;
return EXIT_FAILURE;
}
isolatedConnected->SetReplaceValue( 255 );
if( inputImagePtr->GetBufferedRegion().IsInside( indexList1[0] ) )
{
try
{
writer->Update();
}
catch( itk::ExceptionObject & excep )
{
std::cerr << "Exception caught !" << std::endl;
std::cerr << excep << std::endl;
}
}
else
{
std::cerr << "Index not within image boundaries" << std::endl;
}
return EXIT_SUCCESS;
}