PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
EnhanceUsTrpSequence.cxx
Go to the documentation of this file.
1 #include "PlusConfigure.h"
2 #include "igsioTrackedFrame.h"
3 #include "vtkImageCast.h"
4 #include "vtkImageData.h"
5 #include "vtkSmartPointer.h"
7 #include <vtkPlusSequenceIO.h>
8 
9 #include <vtkIGSIOTrackedFrameList.h>
10 #include <vtkIGSIOMetaImageSequenceIO.h>
11 
12 #include "vtksys/CommandLineArguments.hxx"
13 
14 #include "string"
15 
16 int main(int argc, char **argv)
17 {
18  bool printHelp = false;
19  vtksys::CommandLineArguments args;
20 
21  std::string inputFileName;
22  std::string outputFileName;
23  std::string configFileName;
24  bool saveIntermediateResults = false;
25  int verboseLevel=vtkPlusLogger::LOG_LEVEL_UNDEFINED;
26 
27  args.Initialize(argc, argv);
28  args.AddArgument("--help", vtksys::CommandLineArguments::NO_ARGUMENT, &printHelp, "Print this help");
29  args.AddArgument("--input-seq-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputFileName, "The filename for the input ultrasound sequence to process.");
30  args.AddArgument("--config-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &configFileName, "The filename for input config file.");
31  args.AddArgument("--output-seq-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &outputFileName, "The filename to write the processed sequence to.");
32  args.AddArgument("--save-intermediate-images", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &saveIntermediateResults, "If intermediate images should be saved to output files");
33  args.AddArgument("--verbose", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &verboseLevel, "Verbose level (1=error only, 2=warning, 3=info, 4=debug, 5=trace)");
34 
35  if (!args.Parse())
36  {
37  LOG_ERROR("Error parsing arguments");
38  LOG_ERROR("Help: " << args.GetHelp());
39  //std::cerr << "Error parsing arguments" << std::endl;
40  //std::cout << "Help: " << args.GetHelp() << std::endl;
41  exit(EXIT_FAILURE);
42  }
43 
44  if (printHelp)
45  {
46  std::cout << args.GetHelp() << std::endl;
47  return EXIT_SUCCESS;
48  }
49 
50  vtkPlusLogger::Instance()->SetLogLevel(verboseLevel);
51 
52  if (inputFileName.empty())
53  {
54  //std::cerr << "--input-seq-file not found!" << std::endl;
55  LOG_ERROR("--input-seq-file not found!");
56  return EXIT_FAILURE;
57  }
58 
59  if (configFileName.empty())
60  {
61  //std::cerr << "--config-file not found!" << std::endl;
62  LOG_ERROR("--config-file not found!");
63  return EXIT_FAILURE;
64  }
65 
66  if (outputFileName.empty())
67  {
68  //std::cerr << "--output-seq-file not found!" << std::endl;
69  LOG_ERROR("--output-seq-file not found!");
70  return EXIT_FAILURE;
71  }
72 
73  // Read config file
74 
75  LOG_DEBUG("Reading config file...")
76  vtkSmartPointer<vtkXMLDataElement> configRootElement = vtkSmartPointer<vtkXMLDataElement>::New();
77  if (PlusXmlUtils::ReadDeviceSetConfigurationFromFile(configRootElement, configFileName.c_str())==PLUS_FAIL)
78  {
79  LOG_ERROR("Unable to read configuration from file " << configFileName.c_str());
80  return EXIT_FAILURE;
81  }
82  LOG_DEBUG("Reading config file finished.");
83 
84  vtkXMLDataElement* dataCollectionElement = configRootElement->FindNestedElementWithName("DataCollection");
85  if (dataCollectionElement == NULL)
86  {
87  LOG_ERROR("Cannot find device set in XML tree element: " << ((*configRootElement).GetName()));
88  return PLUS_FAIL;
89  }
90 
91  vtkXMLDataElement* boneEnhancerElement = dataCollectionElement->FindNestedElementWithNameAndAttribute("Device", "Type", "ImageProcessor");
92  if (boneEnhancerElement == NULL)
93  {
94  LOG_ERROR("Cannot find BoneEnhancer element in XML tree element: " << ((*dataCollectionElement).GetName()));
95  return PLUS_FAIL;
96  }
97 
98  vtkXMLDataElement* processorElement = boneEnhancerElement->FindNestedElementWithNameAndAttribute("Processor", "Type", "vtkPlusTransverseProcessEnhancer");
99  if (processorElement == NULL)
100  {
101  LOG_ERROR("Cannot find image Processor vtkPlusTransverseProcessEnhancer config in XML element: " << ((*boneEnhancerElement).GetName()));
102  return PLUS_FAIL;
103  }
104 
105  // Read the input sequence.
106 
107  vtkSmartPointer<vtkIGSIOTrackedFrameList> trackedFrameList = vtkSmartPointer<vtkIGSIOTrackedFrameList>::New();
108  vtkPlusSequenceIO::Read(inputFileName.c_str(), trackedFrameList);
109 
110  vtkSmartPointer<vtkImageCast> castToUchar = vtkSmartPointer<vtkImageCast>::New();
111  castToUchar->SetOutputScalarTypeToUnsignedChar();
112 
113 
114  int numberOfFrames = trackedFrameList->GetNumberOfTrackedFrames();
115  LOG_INFO("Number of frames in input: " << numberOfFrames);
116 
117  // Bone filter.
118 
119  vtkSmartPointer<vtkPlusTransverseProcessEnhancer> boneFilter = vtkSmartPointer<vtkPlusTransverseProcessEnhancer>::New();
120 
121  boneFilter->SetInputFrames(trackedFrameList);
122  boneFilter->ReadConfiguration(processorElement);
123 
124  PlusStatus filterStatus = boneFilter->Update();
125  if (filterStatus != PlusStatus::PLUS_SUCCESS)
126  {
127  LOG_ERROR("Failed processing frames");
128  return EXIT_FAILURE;
129  }
130 
131  LOG_INFO("Writing output to file");
132 
133  if (saveIntermediateResults)
134  {
135  // Find out where to add the unique suffix for each intermediate image
136  int startInputFileNameIndex = 0;
137  if (inputFileName.find("/") != std::string::npos)
138  {
139  startInputFileNameIndex = inputFileName.rfind("/") + 1;
140  }
141  if (inputFileName.find("\\") != std::string::npos)
142  {
143  startInputFileNameIndex = inputFileName.rfind("\\") + 1;
144  }
145  int startOutputFileNameIndex = 0;
146  if (outputFileName.find("/") != std::string::npos)
147  {
148  startOutputFileNameIndex = outputFileName.rfind("/") + 1;
149  }
150  if (outputFileName.find("\\") != std::string::npos)
151  {
152  startOutputFileNameIndex = outputFileName.rfind("\\") + 1;
153  }
154 
155  // Saves the intermediate results that were recorded during the call to boneFilter->Update()
156  boneFilter->SetIntermediateImageFileName(
157  outputFileName.substr(0, startOutputFileNameIndex) + inputFileName.substr(startInputFileNameIndex, inputFileName.find(".") - startInputFileNameIndex) );
158  boneFilter->SaveAllIntermediateResultsToFile();
159  }
160 
161  if (vtkPlusSequenceIO::Write(outputFileName.c_str(), boneFilter->GetOutputFrames())== PLUS_FAIL)
162  {
163  LOG_ERROR("Could not save output sequence to the file: " << outputFileName);
164  return EXIT_FAILURE;
165  }
166  return EXIT_SUCCESS;
167 }
igsioStatus PlusStatus
Definition: PlusCommon.h:40
#define PLUS_FAIL
Definition: PlusCommon.h:43
static igsioStatus Write(const std::string &filename, igsioTrackedFrame *frame, US_IMAGE_ORIENTATION orientationInFile=US_IMG_ORIENT_MF, bool useCompression=true, bool EnableImageDataWrite=true)
#define PLUS_SUCCESS
Definition: PlusCommon.h:44
static igsioStatus Read(const std::string &filename, vtkIGSIOTrackedFrameList *frameList)
static vtkIGSIOLogger * Instance()
int main(int argc, char **argv)
static PlusStatus ReadDeviceSetConfigurationFromFile(vtkXMLDataElement *config, const char *filename)
Definition: PlusXmlUtils.h:23