21 #include <metaCommand.h>
25 #include <itkImageFileWriter.h>
27 #include <boost/lexical_cast.hpp>
30 #include <itksys/SystemTools.hxx>
33 #define _USE_MATH_DEFINES
41 int main(
int argc,
char* argv[])
45 parser.
setTitle(
"Local Directional Fiber Plausibility");
46 parser.
setCategory(
"Fiber Tracking and Processing Methods");
47 parser.
setDescription(
"Calculate angular error of a tractogram with respect to the input reference directions.");
55 parser.
addArgument(
"athresh",
"a",
mitkCommandLineParser::Float,
"Angular threshold:",
"angular threshold in degrees. closer fiber directions are regarded as one direction and clustered together.", 25,
true);
63 map<string, us::Any> parsedArgs = parser.
parseArguments(argc, argv);
64 if (parsedArgs.size()==0)
69 if (parsedArgs.count(
"mask"))
70 maskImages = us::any_cast<mitkCommandLineParser::StringContainerType>(parsedArgs[
"mask"]);
72 string fibFile =
us::any_cast<
string>(parsedArgs[
"input"]);
74 float angularThreshold = 25;
75 if (parsedArgs.count(
"athresh"))
76 angularThreshold = us::any_cast<float>(parsedArgs[
"athresh"]);
78 float sizeThreshold = 0;
79 if (parsedArgs.count(
"sthresh"))
80 sizeThreshold = us::any_cast<float>(parsedArgs[
"sthresh"]);
83 if (parsedArgs.count(
"maxdirs"))
84 maxDirs = us::any_cast<int>(parsedArgs[
"maxdirs"]);
86 string outRoot =
us::any_cast<
string>(parsedArgs[
"out"]);
89 if (parsedArgs.count(
"verbose"))
90 verbose = us::any_cast<bool>(parsedArgs[
"verbose"]);
92 bool ignoreMissing =
false;
93 if (parsedArgs.count(
"ignore"))
94 ignoreMissing = us::any_cast<bool>(parsedArgs[
"ignore"]);
96 bool ignoreEmpty =
false;
97 if (parsedArgs.count(
"empty"))
98 ignoreEmpty = us::any_cast<bool>(parsedArgs[
"empty"]);
101 if (parsedArgs.count(
"fileID"))
102 fileID = us::any_cast<string>(parsedArgs[
"fileID"]);
107 typedef itk::Image<unsigned char, 3> ItkUcharImgType;
108 typedef itk::Image< itk::Vector< float, 3>, 3 > ItkDirectionImage3DType;
109 typedef itk::VectorContainer< unsigned int, ItkDirectionImage3DType::Pointer > ItkDirectionImageContainerType;
117 for (
unsigned int i=0; i<referenceImages.size(); i++)
124 caster->SetInput(img);
127 referenceImageContainer->InsertElement(referenceImageContainer->Size(),itkImg);
129 catch(...){ std::cout <<
"could not load: " << referenceImages.at(i); }
134 itkMaskImage->SetSpacing( dirImg->GetSpacing() );
135 itkMaskImage->SetOrigin( dirImg->GetOrigin() );
136 itkMaskImage->SetDirection( dirImg->GetDirection() );
137 itkMaskImage->SetLargestPossibleRegion( dirImg->GetLargestPossibleRegion() );
138 itkMaskImage->SetBufferedRegion( dirImg->GetLargestPossibleRegion() );
139 itkMaskImage->SetRequestedRegion( dirImg->GetLargestPossibleRegion() );
140 itkMaskImage->Allocate();
141 itkMaskImage->FillBuffer(1);
145 fOdfFilter->SetFiberBundle(inputTractogram);
146 fOdfFilter->SetMaskImage(itkMaskImage);
147 fOdfFilter->SetAngularThreshold(cos(angularThreshold*
M_PI/180));
148 fOdfFilter->SetNormalizeVectors(
true);
149 fOdfFilter->SetUseWorkingCopy(
false);
150 fOdfFilter->SetSizeThreshold(sizeThreshold);
151 fOdfFilter->SetMaxNumDirections(maxDirs);
152 fOdfFilter->Update();
160 string outfilename = outRoot;
161 outfilename.append(
"_VECTOR_FIELD.fib");
166 for (
unsigned int i=0; i<directionImageContainer->Size(); i++)
169 typedef itk::ImageFileWriter< itk::TractsToVectorImageFilter<float>::ItkDirectionImageType > WriterType;
172 string outfilename = outRoot;
173 outfilename.append(
"_DIRECTION_");
174 outfilename.append(boost::lexical_cast<string>(i));
175 outfilename.append(
".nrrd");
177 writer->SetFileName(outfilename.c_str());
178 writer->SetInput(itkImg);
185 typedef itk::ImageFileWriter< ItkUcharImgType > WriterType;
188 string outfilename = outRoot;
189 outfilename.append(
"_NUM_DIRECTIONS.nrrd");
191 writer->SetFileName(outfilename.c_str());
192 writer->SetInput(numDirImage);
198 logFile.append(
"_ANGULAR_ERROR.csv");
200 file.open (logFile.c_str());
202 if (maskImages.size()>0)
204 for (
unsigned int i=0; i<maskImages.size(); i++)
211 evaluationFilter->SetImageSet(directionImageContainer);
212 evaluationFilter->SetReferenceImageSet(referenceImageContainer);
213 evaluationFilter->SetMaskImage(itkMaskImage);
214 evaluationFilter->SetIgnoreMissingDirections(ignoreMissing);
215 evaluationFilter->SetIgnoreEmptyVoxels(ignoreEmpty);
216 evaluationFilter->Update();
221 typedef itk::ImageFileWriter< EvaluationFilterType::OutputImageType > WriterType;
224 string outfilename = outRoot;
225 outfilename.append(
"_ERROR_IMAGE.nrrd");
227 writer->SetFileName(outfilename.c_str());
228 writer->SetInput(angularErrorImage);
232 string maskFileName = itksys::SystemTools::GetFilenameWithoutExtension(maskImages.at(i));
233 unsigned found = maskFileName.find_last_of(
"_");
235 string sens = itksys::SystemTools::GetFilenameWithoutLastExtension(fibFile);
240 sens.append(maskFileName.substr(found+1));
243 sens.append(boost::lexical_cast<string>(evaluationFilter->GetMeanAngularError()));
246 sens.append(boost::lexical_cast<string>(evaluationFilter->GetMedianAngularError()));
249 sens.append(boost::lexical_cast<string>(evaluationFilter->GetMaxAngularError()));
252 sens.append(boost::lexical_cast<string>(evaluationFilter->GetMinAngularError()));
255 sens.append(boost::lexical_cast<string>(std::sqrt(evaluationFilter->GetVarAngularError())));
264 evaluationFilter->SetImageSet(directionImageContainer);
265 evaluationFilter->SetReferenceImageSet(referenceImageContainer);
266 evaluationFilter->SetMaskImage(itkMaskImage);
267 evaluationFilter->SetIgnoreMissingDirections(ignoreMissing);
268 evaluationFilter->SetIgnoreEmptyVoxels(ignoreEmpty);
269 evaluationFilter->Update();
274 typedef itk::ImageFileWriter< EvaluationFilterType::OutputImageType > WriterType;
277 string outfilename = outRoot;
278 outfilename.append(
"_ERROR_IMAGE.nrrd");
280 writer->SetFileName(outfilename.c_str());
281 writer->SetInput(angularErrorImage);
285 string sens = itksys::SystemTools::GetFilenameWithoutLastExtension(fibFile);
290 sens.append(boost::lexical_cast<string>(evaluationFilter->GetMeanAngularError()));
293 sens.append(boost::lexical_cast<string>(evaluationFilter->GetMedianAngularError()));
296 sens.append(boost::lexical_cast<string>(evaluationFilter->GetMaxAngularError()));
299 sens.append(boost::lexical_cast<string>(evaluationFilter->GetMinAngularError()));
302 sens.append(boost::lexical_cast<string>(std::sqrt(evaluationFilter->GetVarAngularError())));
308 catch (itk::ExceptionObject e)
313 catch (std::exception e)
315 std::cout << e.what();
320 std::cout <<
"ERROR!?!";
int main(int argc, char *argv[])
Calculate angular error of a tractogram with respect to the input reference directions.
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)
T::Pointer GetData(const std::string &name)
static bool SaveBaseData(mitk::BaseData *data, const std::string &path)
SaveBaseData Convenience method to save arbitrary baseData.
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)
Evaluates the voxel-wise angular error between two sets of directions.
Image class for storing images.
static std::ofstream * logFile
Base Class for Fiber Bundles;.
void setCategory(std::string category)
static mitk::DataNode::Pointer LoadDataNode(const std::string &path)
LoadDataNode Method to load an arbitrary DataNode.
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
void MITKCORE_EXPORT CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer< ItkOutputImageType > &itkOutputImage)
Cast an mitk::Image to an itk::Image with a specific type.
std::vector< std::string > StringContainerType
void setTitle(std::string title)
void setDescription(std::string description)
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.