-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnbiasedNonLocalMeans.cxx
184 lines (159 loc) · 5.72 KB
/
UnbiasedNonLocalMeans.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
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
/*=========================================================================
Program: Diffusion Applications
Language: C++
Module: $HeadURL: http://svn.slicer.org/Slicer3/trunk/Applications/CLI/DiffusionApplications/dwiNoiseFilter/dwiNoiseFilter.cxx $
Date: $Date: 2008-11-25 18:46:58 +0100 (Tue, 25 Nov 2008) $
Version: $Revision: 7972 $
Copyright (c) Brigham and Women's Hospital (BWH) All Rights Reserved.
See License.txt or http://www.slicer.org/copyright/copyright.txt for details.
==========================================================================*/
#ifdef _WIN32
// to pick up M_SQRT2 and other nice things...
#define _USE_MATH_DEFINES
#endif
#include <math.h>
#include <itkImageFileWriter.h>
#include <itkImageFileReader.h>
#include <itkImageRegionIterator.h>
#include <itkImageRegionConstIteratorWithIndex.h>
#include "itkCastImageFilter.h"
#include "UnbiasedNonLocalMeansCLP.h"
//--------------------------------------------------
// Specific includes:
#include "itkNLMFilter.h"
//--------------------------------------------------
static const unsigned int DIMENSION=3;
/* Copied locally to avoid complicated build dependancies */
/* Origincal code taken from Slicer/Base/CLI/itkPluginUtilities.h */
namespace UNLM {
//-----------------------------------------------------------------------------
/// Get the PixelType and ComponentType from fileName
static void GetImageType (std::string fileName,
itk::ImageIOBase::IOPixelType &pixelType,
itk::ImageIOBase::IOComponentType &componentType)
{
typedef itk::Image<unsigned char, 3> ImageType;
itk::ImageFileReader<ImageType>::Pointer imageReader = itk::ImageFileReader<ImageType>::New();
imageReader->SetFileName(fileName.c_str());
imageReader->UpdateOutputInformation();
pixelType = imageReader->GetImageIO()->GetPixelType();
componentType = imageReader->GetImageIO()->GetComponentType();
}
}
template<class PixelType>
int DoIt( int argc, char * argv[], PixelType )
{
PARSE_ARGS;
// do the typedefs
typedef itk::Image<PixelType,DIMENSION> ImageType;
typename itk::ImageFileReader<ImageType>::Pointer reader = itk::ImageFileReader<ImageType>::New();
reader->SetFileName( inputVolume.c_str() );
try
{
reader->Update();
}
catch ( itk::ExceptionObject & e )
{
std::cerr << "exception in file reader " << std::endl;
std::cerr << e.GetDescription() << std::endl;
std::cerr << e.GetLocation() << std::endl;
return EXIT_FAILURE;
}
typedef itk::NLMFilter< ImageType, ImageType > FilterType;
typename FilterType::Pointer filter = FilterType::New();
filter->SetInput( reader->GetOutput() );
/** SET PARAMETERS TO THE FILTER */
// The power of noise:
filter->SetSigma( iSigma );
// The search radius
typename FilterType::InputImageSizeType radius;
for( unsigned int d=0; d<DIMENSION; ++d )
radius[d] = iRadiusSearch[d];
filter->SetRSearch( radius );
// The comparison radius:
for( unsigned int d=0; d<DIMENSION; ++d )
radius[d] = iRadiusComp[d];
filter->SetRComp( radius );
// The "h" parameter:
filter->SetH( iH );
// The preselection threshold:
filter->SetPSTh( iPs );
// Run the filter:
try
{
filter->Update();
}
catch ( itk::ExceptionObject & e )
{
std::cerr << "exception in filter" << std::endl;
std::cerr << e.GetDescription() << std::endl;
std::cerr << e.GetLocation() << std::endl;
return EXIT_FAILURE;
}
// Generate output image
typename itk::ImageFileWriter<ImageType>::Pointer writer = itk::ImageFileWriter<ImageType>::New();
writer->SetInput( filter->GetOutput() );
writer->SetFileName( outputVolume.c_str() );
try
{
writer->Update();
}
catch ( itk::ExceptionObject & e )
{
std::cerr << "exception in file writer " << std::endl;
std::cerr << e.GetDescription() << std::endl;
std::cerr << e.GetLocation() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
int main( int argc, char * argv[] )
{
PARSE_ARGS;
itk::ImageIOBase::IOPixelType pixelType;
itk::ImageIOBase::IOComponentType componentType;
UNLM::GetImageType (inputVolume, pixelType, componentType);
// This filter handles all types
switch (componentType)
{
#ifndef WIN32
case itk::ImageIOBase::UCHAR:
return DoIt( argc, argv, static_cast<unsigned char>(0));
break;
case itk::ImageIOBase::CHAR:
return DoIt( argc, argv, static_cast<char>(0));
break;
#endif
case itk::ImageIOBase::USHORT:
return DoIt( argc, argv, static_cast<unsigned short>(0));
break;
case itk::ImageIOBase::SHORT:
return DoIt( argc, argv, static_cast<short>(0));
break;
case itk::ImageIOBase::UINT:
return DoIt( argc, argv, static_cast<unsigned int>(0));
break;
case itk::ImageIOBase::INT:
return DoIt( argc, argv, static_cast<int>(0));
break;
#ifndef WIN32
case itk::ImageIOBase::ULONG:
return DoIt( argc, argv, static_cast<unsigned long>(0));
break;
case itk::ImageIOBase::LONG:
return DoIt( argc, argv, static_cast<long>(0));
break;
#endif
case itk::ImageIOBase::FLOAT:
return DoIt( argc, argv, static_cast<float>(0));
break;
case itk::ImageIOBase::DOUBLE:
std::cout << "WARNING: DOUBLE type not currently supported, so defaulting to float." << std::endl;
case itk::ImageIOBase::UNKNOWNCOMPONENTTYPE:
default:
std::cout << "WARNING: UNKNOWN Component type not currently supported, so defaulting to float." << std::endl;
return DoIt( argc, argv, static_cast<float>(0));
break;
}
return EXIT_SUCCESS;
}