23 #include <itksys/SystemTools.hxx> 24 #include <tinyxml\tinyxml.h> 26 struct InputParameters
31 std::string settingsFile;
38 unsigned int cutAbove;
41 struct ResamplingSettings
51 parser.
setTitle(
"Mitk Photoacoustics Resample and Crop Tool");
52 parser.
setDescription(
"Reads a nrrd file as an input and crops and resamples it as set with the parameters defined in an additionally provided xml file.");
53 parser.
setContributor(
"Computer Assisted Medical Interventions, DKFZ");
60 "Input image (mitk::Image)",
"input image (.nrrd file)",
64 "Output filename",
"output image (.nrrd file)",
68 "settings file",
"file containing specifications (.xml file)",
75 "Verbose Output",
"Whether to produce verbose, or rather debug output. (default: false)");
78 InputParameters input;
80 std::map<std::string, us::Any> parsedArgs = parser.
parseArguments(argc, argv);
81 if (parsedArgs.size() == 0)
84 input.verbose = (bool)parsedArgs.count(
"verbose");
85 MITK_INFO(input.verbose) <<
"### VERBOSE OUTPUT ENABLED ###";
87 if (parsedArgs.count(
"inputImage"))
89 MITK_INFO(input.verbose) <<
"Reading input image...";
90 input.inputImage = mitk::IOUtil::Load<mitk::Image>(
us::any_cast<std::string>(parsedArgs[
"inputImage"]));
91 MITK_INFO(input.verbose) <<
"Reading input image...[Done]";
96 if (parsedArgs.count(
"output"))
97 input.outputFilename = us::any_cast<std::string>(parsedArgs[
"output"]);
99 mitkThrow() <<
"No output image path given..";
101 if (parsedArgs.count(
"settings"))
102 input.settingsFile = us::any_cast<std::string>(parsedArgs[
"settings"]);
104 mitkThrow() <<
"No settings image path given..";
109 void ParseXML(std::string xmlFile, InputParameters input, CropSettings& cropSet, ResamplingSettings& resSet)
111 MITK_INFO <<
"Loading configuration File \"" << xmlFile <<
"\"";
112 TiXmlDocument doc(xmlFile);
114 mitkThrow() <<
"Failed to load settings file \"" << xmlFile <<
"\" Error: " << doc.ErrorDesc();
116 TiXmlElement* root = doc.FirstChildElement();
119 mitkThrow() <<
"Failed to load file: No root element.";
122 for (TiXmlElement* elem = root->FirstChildElement(); elem != NULL; elem = elem->NextSiblingElement())
124 std::string elemName = elem->Value();
125 if (elemName ==
"ResampleCrop")
127 cropSet.dimX = std::stoi(elem->Attribute(
"dimX"));
128 cropSet.dimY = std::stoi(elem->Attribute(
"dimY"));
129 cropSet.cutAbove = std::stoi(elem->Attribute(
"cutAbove"));
130 resSet.spacing[0] = std::stod(elem->Attribute(
"spacingX"));
131 resSet.spacing[1] = std::stod(elem->Attribute(
"spacingY"));
132 resSet.spacing[2] = std::stod(elem->Attribute(
"spacingZ"));
137 int main(
int argc,
char * argv[])
141 CropSettings cropSettings{ 0,0,0 };
142 ResamplingSettings resSettings{ 0 };
147 ParseXML(input.settingsFile, input, cropSettings, resSettings);
155 MITK_INFO <<
"Parsing settings XML...[Done]";
157 MITK_INFO(input.verbose) <<
"Processing input image...";
162 MITK_INFO(input.verbose) <<
"Resampling input image...";
164 output = m_FilterService->ApplyResampling(output, resSettings.spacing);
165 MITK_INFO(input.verbose) <<
"Resampling input image...[Done]";
167 if (output->GetDimension(0) != cropSettings.dimX)
169 double outputDim[] = {(double)cropSettings.dimX, (
double)output->GetDimension(1), (double)output->GetDimension(2)};
170 output = m_FilterService->ApplyResamplingToDim(output, outputDim);
174 int below = output->GetDimension(1) - cropSettings.dimY - cropSettings.cutAbove;
177 MITK_INFO(input.verbose) <<
"Extending input image...";
178 output = m_FilterService->ExtendImage(output, 0, cropSettings.dimY);
179 MITK_INFO(input.verbose) <<
"Extending input image...[Done]";
183 MITK_INFO(input.verbose) <<
"Cropping input image...";
184 output = m_FilterService->ApplyCropping(output, cropSettings.cutAbove, below, 0, 0, 0, 0, &err);
185 MITK_INFO(input.verbose) <<
"Cropping input image...[Done]";
189 MITK_INFO(input.verbose) <<
"Saving image...";
191 MITK_INFO(input.verbose) <<
"Saving image...[Done]";
193 MITK_INFO(input.verbose) <<
"Processing input image...[Done]";
void setContributor(std::string contributor)
ValueType * any_cast(Any *operand)
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, mitkCommandLineParser::Channel channel=mitkCommandLineParser::Channel::None)
std::map< std::string, us::Any > parseArguments(const StringContainerType &arguments, bool *ok=nullptr)
std::string outputFilename
An object of this class represents an exception of MITK. Please don't instantiate exceptions manually...
void setCategory(std::string category)
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
static void Save(const mitk::BaseData *data, const std::string &path, bool setPathProperty=false)
Save a mitk::BaseData instance.
void setTitle(std::string title)
void setDescription(std::string description)
void beginGroup(const std::string &description)