Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
FiberExtraction.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include <metaCommand.h>
18 #include "mitkCommandLineParser.h"
19 #include <usAny.h>
20 #include <mitkIOUtil.h>
21 #include <boost/lexical_cast.hpp>
22 #include <mitkCoreObjectFactory.h>
23 #include <mitkPlanarFigure.h>
25 #include <mitkFiberBundle.h>
26 #include <mitkImageCast.h>
27 #include <mitkImageToItk.h>
29 
30 #define _USE_MATH_DEFINES
31 #include <math.h>
32 
33 using namespace std;
34 
38 int main(int argc, char* argv[])
39 {
40  mitkCommandLineParser parser;
41 
42  parser.setTitle("Fiber Extraction");
43  parser.setCategory("Fiber Tracking and Processing Methods");
44  parser.setContributor("MBI");
45  parser.setDescription("Extract fibers from a tractogram using planar figure ROIs");
46 
47  parser.setArgumentPrefix("--", "-");
48  parser.addArgument("input", "i", mitkCommandLineParser::String, "Input:", "input tractogram (.fib/.trk)", us::Any(), false);
49  parser.addArgument("out", "o", mitkCommandLineParser::String, "Output:", "output tractogram", us::Any(), false);
50  parser.addArgument("planfirgure1", "pf1", mitkCommandLineParser::String, "Figure 1:", "first ROI", us::Any(), false);
51  parser.addArgument("planfirgure2", "pf2", mitkCommandLineParser::String, "Figure 2:", "second ROI", us::Any());
52  parser.addArgument("operation", "op", mitkCommandLineParser::String, "Operation:", "logical operation (AND, OR, NOT)", us::Any());
53 
54 
55  map<string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
56  if (parsedArgs.size()==0)
57  return EXIT_FAILURE;
58 
59  string inFib = us::any_cast<string>(parsedArgs["input"]);
60  string outFib = us::any_cast<string>(parsedArgs["out"]);
61  string pf1_path = us::any_cast<string>(parsedArgs["planfirgure1"]);
62 
63  string operation("");
64  string pf2_path("");
65  if (parsedArgs.count("operation"))
66  {
67  operation = us::any_cast<string>(parsedArgs["operation"]);
68  if (parsedArgs.count("planfirgure2") && (operation=="AND" || operation=="OR"))
69  pf2_path = us::any_cast<string>(parsedArgs["planfirgure2"]);
70  }
71 
72  try
73  {
74  typedef itk::Image<unsigned char, 3> ItkUcharImgType;
75 
76  // load fiber bundle
77  mitk::FiberBundle::Pointer inputTractogram = dynamic_cast<mitk::FiberBundle*>(mitk::IOUtil::LoadDataNode(inFib)->GetData());
79 
82 
83  if (input1.IsNotNull())
84  {
87  pfcNode->SetData(pfc);
89  set1->push_back(pfcNode);
90  storage->Add(pfcNode);
91 
93  if (!pf2_path.empty())
94  {
95  input2 = mitk::IOUtil::LoadDataNode(pf2_path);
96  }
97 
98  if (operation.empty())
99  {
100  result = inputTractogram->ExtractFiberSubset(input1, NULL);
101  }
102  else if (operation=="NOT")
103  {
104  pfc->setOperationType(mitk::PlanarFigureComposite::NOT);
105  storage->Add(input1, set1);
106  result = inputTractogram->ExtractFiberSubset(pfcNode, storage);
107  }
108  else if (operation=="AND" && input2.IsNotNull())
109  {
110  pfc->setOperationType(mitk::PlanarFigureComposite::AND);
111  storage->Add(input1, set1);
112  storage->Add(input2, set1);
113  result = inputTractogram->ExtractFiberSubset(pfcNode, storage);
114  }
115  else if (operation=="OR" && input2.IsNotNull())
116  {
117  pfc->setOperationType(mitk::PlanarFigureComposite::OR);
118  storage->Add(input1, set1);
119  storage->Add(input2, set1);
120  result = inputTractogram->ExtractFiberSubset(pfcNode, storage);
121  }
122  else
123  {
124  std::cout << "Could not process input:";
125  std::cout << pf1_path;
126  std::cout << pf2_path;
127  std::cout << operation;
128  }
129  }
130  else
131  {
133  mitk::Image::Pointer mitkMaskImage = dynamic_cast<mitk::Image*>(mitk::IOUtil::LoadDataNode(pf1_path)->GetData());
134  mitk::CastToItkImage<ItkUcharImgType>(mitkMaskImage, itkMaskImage);
135 
136  if (operation=="NOT")
137  result = inputTractogram->ExtractFiberSubset(itkMaskImage, true, true);
138  else
139  result = inputTractogram->ExtractFiberSubset(itkMaskImage, true, false);
140  }
141 
142  if (result.IsNotNull())
143  mitk::IOUtil::SaveBaseData(result, outFib);
144  else
145  std::cout << "No valid fiber bundle extracted.";
146  }
147  catch (itk::ExceptionObject e)
148  {
149  std::cout << e;
150  return EXIT_FAILURE;
151  }
152  catch (std::exception e)
153  {
154  std::cout << e.what();
155  return EXIT_FAILURE;
156  }
157  catch (...)
158  {
159  std::cout << "ERROR!?!";
160  return EXIT_FAILURE;
161  }
162  return EXIT_SUCCESS;
163 }
itk::SmartPointer< Self > Pointer
void setContributor(std::string contributor)
STL namespace.
ValueType * any_cast(Any *operand)
Definition: usAny.h:377
std::map< std::string, us::Any > parseArguments(const StringContainerType &arguments, bool *ok=nullptr)
int main(int argc, char *argv[])
Extract fibers from a tractogram using planar figure ROIs.
static bool SaveBaseData(mitk::BaseData *data, const std::string &path)
SaveBaseData Convenience method to save arbitrary baseData.
Definition: mitkIOUtil.cpp:888
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)
static Pointer New()
Image class for storing images.
Definition: mitkImage.h:76
Definition: usAny.h:163
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.
Definition: mitkIOUtil.cpp:586
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
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.