Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
NetworkCreation.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 // std includes
18 #include <string>
19 
20 // CTK includes
21 #include "mitkCommandLineParser.h"
22 
23 // MITK includes
25 #include <mitkCoreObjectFactory.h>
26 #include <mitkIOUtil.h>
27 
28 using namespace std;
29 
30 int main(int argc, char* argv[])
31 {
32  mitkCommandLineParser parser;
33 
34  parser.setTitle("Network Creation");
35  parser.setCategory("Connectomics");
36  parser.setDescription("");
37  parser.setContributor("MBI");
38 
39  parser.setArgumentPrefix("--", "-");
40  parser.addArgument("fiberImage", "f", mitkCommandLineParser::InputFile, "Input image", "input fiber image (.fib)", us::Any(), false);
41  parser.addArgument("parcellation", "p", mitkCommandLineParser::InputFile, "Parcellation image", "parcellation image", us::Any(), false);
42  parser.addArgument("outputNetwork", "o", mitkCommandLineParser::String, "Output network", "where to save the output (.cnf)", us::Any(), false);
43 
44  parser.addArgument("radius", "r", mitkCommandLineParser::Int, "Radius", "Search radius in mm", 15, true);
45  parser.addArgument("noCenterOfMass", "com", mitkCommandLineParser::Bool, "No center of mass", "Do not use center of mass for node positions");
46 
47  parser.setCategory("Connectomics");
48  parser.setTitle("Network Creation");
49  parser.setDescription("");
50  parser.setContributor("MBI");
51 
52  map<string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
53  if (parsedArgs.size()==0)
54  return EXIT_FAILURE;
55 
56  //default values
57  int searchRadius( 15 );
58  bool noCenterOfMass( false );
59 
60  // parse command line arguments
61  std::string fiberFilename = us::any_cast<std::string>(parsedArgs["fiberImage"]);
62  std::string parcellationFilename = us::any_cast<std::string>(parsedArgs["parcellation"]);
63  std::string outputFilename = us::any_cast<std::string>(parsedArgs["outputNetwork"]);
64 
65  if (parsedArgs.count("radius"))
66  searchRadius = us::any_cast<int>(parsedArgs["radius"]);
67 
68 
69  if (parsedArgs.count("noCenterOfMass"))
70  noCenterOfMass = us::any_cast<bool>(parsedArgs["noCenterOfMass"]);
71 
72  try
73  {
74 
75  const std::string s1="", s2="";
76 
77  // load fiber image
78  std::vector<mitk::BaseData::Pointer> fiberInfile =
79  mitk::IOUtil::Load( fiberFilename);
80  if( fiberInfile.empty() )
81  {
82  std::string errorMessage = "Fiber Image at " + fiberFilename + " could not be read. Aborting.";
83  MITK_ERROR << errorMessage;
84  return EXIT_FAILURE;
85  }
86  mitk::BaseData* fiberBaseData = fiberInfile.at(0);
87  mitk::FiberBundle* fiberBundle = dynamic_cast<mitk::FiberBundle*>( fiberBaseData );
88 
89  // load parcellation
90  std::vector<mitk::BaseData::Pointer> parcellationInFile =
91  mitk::IOUtil::Load( parcellationFilename);
92  if( parcellationInFile.empty() )
93  {
94  std::string errorMessage = "Parcellation at " + parcellationFilename + " could not be read. Aborting.";
95  MITK_ERROR << errorMessage;
96  return EXIT_FAILURE;
97  }
98  mitk::BaseData* parcellationBaseData = parcellationInFile.at(0);
99  mitk::Image* parcellationImage = dynamic_cast<mitk::Image*>( parcellationBaseData );
100 
101 
102 
103  // do creation
105  connectomicsNetworkCreator->SetSegmentation( parcellationImage );
106  connectomicsNetworkCreator->SetFiberBundle( fiberBundle );
107  if( !noCenterOfMass )
108  {
109  connectomicsNetworkCreator->CalculateCenterOfMass();
110  }
111  connectomicsNetworkCreator->SetEndPointSearchRadius( searchRadius );
112  connectomicsNetworkCreator->CreateNetworkFromFibersAndSegmentation();
113 
114 
115  mitk::ConnectomicsNetwork::Pointer network = connectomicsNetworkCreator->GetNetwork();
116 
117  std::cout << "searching writer";
118 
119  mitk::IOUtil::SaveBaseData(network.GetPointer(), outputFilename );
120 
121  return EXIT_SUCCESS;
122  }
123  catch (itk::ExceptionObject e)
124  {
125  std::cout << e;
126  return EXIT_FAILURE;
127  }
128  catch (std::exception e)
129  {
130  std::cout << e.what();
131  return EXIT_FAILURE;
132  }
133  catch (...)
134  {
135  std::cout << "ERROR!?!";
136  return EXIT_FAILURE;
137  }
138  std::cout << "DONE";
139  return EXIT_SUCCESS;
140 }
itk::SmartPointer< Self > Pointer
Base of all data objects.
Definition: mitkBaseData.h:39
#define MITK_ERROR
Definition: mitkLogMacros.h:24
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)
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)
Image class for storing images.
Definition: mitkImage.h:76
Definition: usAny.h:163
Base Class for Fiber Bundles;.
void setCategory(std::string category)
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
int main(int argc, char *argv[])
static DataStorage::SetOfObjects::Pointer Load(const std::string &path, DataStorage &storage)
Load a file into the given DataStorage.
Definition: mitkIOUtil.cpp:483
void setTitle(std::string title)
void setDescription(std::string description)