17 #include <itkImageFileWriter.h>
18 #include <itkResampleImageFilter.h>
30 #include <itkFlipImageFilter.h>
31 #include <boost/lexical_cast.hpp>
32 #include <boost/algorithm/string.hpp>
52 parser.
addArgument(
"clusterThres",
"c",
mitkCommandLineParser::Float,
"Clustering threshold",
"directions closer together than the specified angular threshold will be clustered (in rad)", 0.9);
63 map<string, us::Any> parsedArgs = parser.
parseArguments(argc, argv);
64 if (parsedArgs.size()==0)
68 string imageName =
us::any_cast<
string>(parsedArgs[
"image"]);
69 string outRoot =
us::any_cast<
string>(parsedArgs[
"outroot"]);
72 string maskImageName(
"");
73 if (parsedArgs.count(
"mask"))
74 maskImageName = us::any_cast<string>(parsedArgs[
"mask"]);
76 int normalization = 1;
77 if (parsedArgs.count(
"normalization"))
78 normalization = us::any_cast<int>(parsedArgs[
"normalization"]);
81 if (parsedArgs.count(
"numpeaks"))
82 numPeaks = us::any_cast<int>(parsedArgs[
"numpeaks"]);
84 float peakThres = 0.4;
85 if (parsedArgs.count(
"peakthres"))
86 peakThres = us::any_cast<float>(parsedArgs[
"peakthres"]);
88 float absPeakThres = 0.06;
89 if (parsedArgs.count(
"abspeakthres"))
90 absPeakThres = us::any_cast<float>(parsedArgs[
"abspeakthres"]);
92 float clusterThres = 0.9;
93 if (parsedArgs.count(
"clusterThres"))
94 clusterThres = us::any_cast<float>(parsedArgs[
"clusterThres"]);
97 if (parsedArgs.count(
"noFlip"))
98 noFlip = us::any_cast<bool>(parsedArgs[
"noFlip"]);
101 if (parsedArgs.count(
"flipX"))
102 flipX = us::any_cast<bool>(parsedArgs[
"flipX"]);
105 if (parsedArgs.count(
"flipY"))
106 flipY = us::any_cast<bool>(parsedArgs[
"flipY"]);
109 if (parsedArgs.count(
"flipZ"))
110 flipZ = us::any_cast<bool>(parsedArgs[
"flipZ"]);
112 std::cout <<
"image: " << imageName;
113 std::cout <<
"outroot: " << outRoot;
114 if (!maskImageName.empty())
115 std::cout <<
"mask: " << maskImageName;
117 std::cout <<
"no mask image selected";
118 std::cout <<
"numpeaks: " << numPeaks;
119 std::cout <<
"peakthres: " << peakThres;
120 std::cout <<
"abspeakthres: " << absPeakThres;
121 std::cout <<
"shOrder: " << shOrder;
128 typedef itk::Image<unsigned char, 3> ItkUcharImgType;
132 int toolkitConvention = 0;
134 if (parsedArgs.count(
"shConvention"))
136 string convention =
us::any_cast<
string>(parsedArgs[
"shConvention"]).c_str();
139 toolkitConvention = 1;
140 std::cout <<
"Using FSL SH-basis";
144 toolkitConvention = 2;
145 std::cout <<
"Using MRtrix SH-basis";
148 std::cout <<
"Using MITK SH-basis";
151 std::cout <<
"Using MITK SH-basis";
154 if (mask.IsNotNull())
159 filter->SetMaskImage(itkMaskImage);
167 if (toolkitConvention>0)
169 std::cout <<
"Converting coefficient image to MITK format";
173 caster->SetInput(image);
181 converter->SetInputImage(itkImage);
185 std::cout <<
"Flipping image";
186 itk::FixedArray<bool, 4> flipAxes;
191 itk::FlipImageFilter< itk::Image< float, 4 > >
::Pointer flipper = itk::FlipImageFilter< itk::Image< float, 4 > >
::New();
192 flipper->SetInput(itkImage);
193 flipper->SetFlipAxes(flipAxes);
196 itk::Matrix< double,4,4 > m = itkImage->GetDirection(); m[0][0] *= -1; m[1][1] *= -1;
197 flipped->SetDirection(m);
199 itk::Point< float, 4 > o = itkImage->GetOrigin();
200 o[0] -= (flipped->GetLargestPossibleRegion().GetSize(0)-1);
201 o[1] -= (flipped->GetLargestPossibleRegion().GetSize(1)-1);
202 flipped->SetOrigin(o);
203 converter->SetInputImage(flipped);
206 std::cout <<
"Starting conversion";
207 switch (toolkitConvention)
210 converter->SetToolkit(ConverterType::FSL);
211 filter->SetToolkit(MaximaExtractionFilterType::FSL);
214 converter->SetToolkit(ConverterType::MRTRIX);
215 filter->SetToolkit(MaximaExtractionFilterType::MRTRIX);
218 converter->SetToolkit(ConverterType::FSL);
219 filter->SetToolkit(MaximaExtractionFilterType::FSL);
222 converter->GenerateData();
223 filter->SetInput(converter->GetCoefficientImage());
230 caster->SetInput(image);
232 filter->SetInput(caster->GetOutput());
236 std::cout <<
"wrong image type";
241 filter->SetMaxNumPeaks(numPeaks);
242 filter->SetPeakThreshold(peakThres);
243 filter->SetAbsolutePeakThreshold(absPeakThres);
244 filter->SetAngularThreshold(1);
245 filter->SetClusteringThreshold(clusterThres);
246 filter->SetFlipX(flipX);
247 filter->SetFlipY(flipY);
248 filter->SetFlipZ(flipZ);
250 switch (normalization)
253 filter->SetNormalizationMethod(MaximaExtractionFilterType::NO_NORM);
256 filter->SetNormalizationMethod(MaximaExtractionFilterType::MAX_VEC_NORM);
259 filter->SetNormalizationMethod(MaximaExtractionFilterType::SINGLE_VEC_NORM);
263 std::cout <<
"Starting extraction";
268 typedef typename MaximaExtractionFilterType::ItkDirectionImageContainer ItkDirectionImageContainer;
270 for (
unsigned int i=0; i<container->Size(); i++)
274 if (itkMaskImage.IsNotNull())
276 itkImg->SetDirection(itkMaskImage->GetDirection());
277 itkImg->SetOrigin(itkMaskImage->GetOrigin());
280 string outfilename = outRoot;
281 outfilename.append(
"_DIRECTION_");
282 outfilename.append(boost::lexical_cast<string>(i));
283 outfilename.append(
".nrrd");
285 typedef itk::ImageFileWriter< typename MaximaExtractionFilterType::ItkDirectionImage > WriterType;
287 writer->SetFileName(outfilename);
288 writer->SetInput(itkImg);
297 if (itkMaskImage.IsNotNull())
299 numDirImage->SetDirection(itkMaskImage->GetDirection());
300 numDirImage->SetOrigin(itkMaskImage->GetOrigin());
303 string outfilename = outRoot.c_str();
304 outfilename.append(
"_NUM_DIRECTIONS.nrrd");
305 typedef itk::ImageFileWriter< ItkUcharImgType > WriterType;
307 writer->SetFileName(outfilename);
308 writer->SetInput(numDirImage);
316 string outfilename = outRoot.c_str();
317 outfilename.append(
"_VECTOR_FIELD.fib");
321 catch (itk::ExceptionObject e)
326 catch (std::exception e)
328 std::cout << e.what();
333 std::cout <<
"ERROR!?!";
342 int main(
int argc,
char* argv[])
359 parser.
setDescription(
"Extract maxima in the input spherical harmonics image.");
362 map<string, us::Any> parsedArgs = parser.
parseArguments(argc, argv);
363 if (parsedArgs.size()==0)
368 if (parsedArgs.count(
"shOrder"))
369 shOrder = us::any_cast<int>(parsedArgs[
"shOrder"]);
374 return StartPeakExtraction<4>(argc, argv);
376 return StartPeakExtraction<6>(argc, argv);
378 return StartPeakExtraction<8>(argc, argv);
380 return StartPeakExtraction<10>(argc, argv);
382 return StartPeakExtraction<12>(argc, argv);
static void Save(const mitk::BaseData *data, const std::string &path)
Save a mitk::BaseData instance.
itk::SmartPointer< Self > Pointer
void setContributor(std::string contributor)
ValueType * any_cast(Any *operand)
std::map< std::string, us::Any > parseArguments(const StringContainerType &arguments, bool *ok=nullptr)
void addArgument(const std::string &longarg, const std::string &shortarg, Type type, const std::string &argLabel, const std::string &argHelp=std::string(), const us::Any &defaultValue=us::Any(), bool optional=true, bool ignoreRest=false, bool deprecated=false)
void setCategory(std::string category)
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
bool equals(const mitk::ScalarType &val1, const mitk::ScalarType &val2, mitk::ScalarType epsilon=mitk::eps)
void MITKCORE_EXPORT CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer< ItkOutputImageType > &itkOutputImage)
Cast an mitk::Image to an itk::Image with a specific type.
void setTitle(std::string title)
void setDescription(std::string description)
static mitk::Image::Pointer LoadImage(const std::string &path)
LoadImage Convenience method to load an arbitrary mitkImage.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.