PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
ScanConvert.cxx
Go to the documentation of this file.
1 #include "PlusConfigure.h"
2 #include "igsioTrackedFrame.h"
3 #include "vtkSmartPointer.h"
4 #include "vtksys/CommandLineArguments.hxx"
5 #include "vtkIGSIOTrackedFrameList.h"
6 #include "vtkPlusUsScanConvert.h"
9 #include "vtkPlusSequenceIO.h"
10 
11 int main(int argc, char **argv)
12 {
13  bool printHelp = false;
14  vtksys::CommandLineArguments args;
15 
16  std::string inputFileName;
17  std::string outputFileName;
18  std::string configFileName;
19  int verboseLevel=vtkPlusLogger::LOG_LEVEL_UNDEFINED;
20 
21  args.Initialize(argc, argv);
22  args.AddArgument("--help", vtksys::CommandLineArguments::NO_ARGUMENT, &printHelp, "Print this help");
23  args.AddArgument("--input-seq-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputFileName, "The filename for the input ultrasound sequence to process.");
24  args.AddArgument("--config-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &configFileName, "The filename for input config file.");
25  args.AddArgument("--output-seq-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &outputFileName, "The filename to write the processed sequence to.");
26  args.AddArgument("--verbose", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &verboseLevel, "Verbose level (1=error only, 2=warning, 3=info, 4=debug, 5=trace)");
27 
28  if (!args.Parse())
29  {
30  std::cerr << "Error parsing arguments" << std::endl;
31  std::cout << "Help: " << args.GetHelp() << std::endl;
32  exit(EXIT_FAILURE);
33  }
34 
35  if (printHelp)
36  {
37  std::cout << args.GetHelp() << std::endl;
38  return EXIT_SUCCESS;
39  }
40 
41  vtkPlusLogger::Instance()->SetLogLevel(verboseLevel);
42 
43  if (inputFileName.empty())
44  {
45  std::cerr << "--input-seq-file not found!" << std::endl;
46  return EXIT_FAILURE;
47  }
48 
49  if (configFileName.empty())
50  {
51  std::cerr << "--config-file not found!" << std::endl;
52  return EXIT_FAILURE;
53  }
54 
55  if (outputFileName.empty())
56  {
57  std::cerr << "--output-seq-file not found!" << std::endl;
58  return EXIT_FAILURE;
59  }
60 
61  // Read config file.
62 
63  vtkSmartPointer<vtkXMLDataElement> configRootElement = vtkSmartPointer<vtkXMLDataElement>::New();
64  if (PlusXmlUtils::ReadDeviceSetConfigurationFromFile(configRootElement, configFileName.c_str())==PLUS_FAIL)
65  {
66  LOG_ERROR("Unable to read configuration from file " << configFileName.c_str());
67  return EXIT_FAILURE;
68  }
69 
70  vtkXMLDataElement* scanConversionElement = configRootElement->FindNestedElementWithName("ScanConversion");
71  if (scanConversionElement == NULL)
72  {
73  LOG_ERROR("Cannot find ScanConversion element in XML tree!");
74  return PLUS_FAIL;
75  }
76 
77  const char* transducerGeometry = scanConversionElement->GetAttribute("TransducerGeometry");
78  if (transducerGeometry == NULL)
79  {
80  LOG_ERROR("Scan converter TransducerGeometry is undefined!");
81  return PLUS_FAIL;
82  }
83 
84  // Create scan converter.
85 
86  vtkSmartPointer<vtkPlusUsScanConvert> scanConverter;
87  if (STRCASECMP(transducerGeometry, "CURVILINEAR")==0)
88  {
89  scanConverter = vtkSmartPointer<vtkPlusUsScanConvert>::Take(vtkPlusUsScanConvertCurvilinear::New());
90  }
91  else if (STRCASECMP(transducerGeometry, "LINEAR")==0)
92  {
93  scanConverter = vtkSmartPointer<vtkPlusUsScanConvert>::Take(vtkPlusUsScanConvertLinear::New());
94  }
95  else
96  {
97  LOG_ERROR("Invalid scan converter TransducerGeometry: " << transducerGeometry);
98  return PLUS_FAIL;
99  }
100  scanConverter->ReadConfiguration(scanConversionElement);
101 
102  // Read input image.
103 
104  vtkSmartPointer<vtkIGSIOTrackedFrameList> inputFrameList = vtkSmartPointer<vtkIGSIOTrackedFrameList>::New();
105  vtkPlusSequenceIO::Read(inputFileName.c_str(), inputFrameList);
106  int numberOfFrames = inputFrameList->GetNumberOfTrackedFrames();
107 
108  // Create output frame list.
109 
110  vtkSmartPointer<vtkIGSIOTrackedFrameList> outputFrameList = vtkSmartPointer<vtkIGSIOTrackedFrameList>::New();
111 
112  // Iterate thought every frame.
113 
114  for (int frameIndex = 0; frameIndex < numberOfFrames; frameIndex ++ )
115  {
116  igsioTrackedFrame* inputFrame = inputFrameList->GetTrackedFrame(frameIndex);
117 
118  scanConverter->SetInputData( inputFrame->GetImageData()->GetImage() );
119  scanConverter->Update();
120 
121  // Allocate and store output image.
122 
123  outputFrameList->AddTrackedFrame(inputFrame);
124  igsioTrackedFrame* outputFrame = outputFrameList->GetTrackedFrame(outputFrameList->GetNumberOfTrackedFrames()-1);
125  outputFrame->GetImageData()->DeepCopyFrom(scanConverter->GetOutput());
126  }
127 
128  std::cout << "Writing output to file. Setting log level to error only, regardless of user specified verbose level." << std::endl;
129  vtkPlusLogger::Instance()->SetLogLevel(1);
130 
131  vtkPlusSequenceIO::Write(outputFileName.c_str(), outputFrameList);
132 
133  return EXIT_SUCCESS;
134 }
135 
136 
137 
int main(int argc, char **argv)
Definition: ScanConvert.cxx:11
#define PLUS_FAIL
Definition: PlusCommon.h:43
static vtkPlusUsScanConvertLinear * New()
static igsioStatus Write(const std::string &filename, igsioTrackedFrame *frame, US_IMAGE_ORIENTATION orientationInFile=US_IMG_ORIENT_MF, bool useCompression=true, bool EnableImageDataWrite=true)
static vtkPlusUsScanConvertCurvilinear * New()
static igsioStatus Read(const std::string &filename, vtkIGSIOTrackedFrameList *frameList)
static vtkIGSIOLogger * Instance()
static PlusStatus ReadDeviceSetConfigurationFromFile(vtkXMLDataElement *config, const char *filename)
Definition: PlusXmlUtils.h:23