PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
RfProcessor.cxx
Go to the documentation of this file.
1 /*=Plus=header=begin======================================================
2 Program: Plus
3 Copyright (c) Laboratory for Percutaneous Surgery. All rights reserved.
4 See License.txt for details.
5 =========================================================Plus=header=end*/
6 
7 #include "PlusConfigure.h"
8 #include "igsioTrackedFrame.h"
9 #include "vtkImageData.h"
10 #include "vtkPlusRfProcessor.h"
11 #include "vtkPlusSequenceIO.h"
12 #include "vtkSmartPointer.h"
13 #include "vtkIGSIOTrackedFrameList.h"
14 #include "vtkTransform.h"
15 #include "vtkXMLUtilities.h"
16 #include "vtksys/CommandLineArguments.hxx"
17 #include "vtksys/SystemTools.hxx"
18 #include <iomanip>
19 #include <iostream>
20 
21 
22 //-----------------------------------------------------------------------------
23 int main(int argc, char **argv)
24 {
25  bool printHelp=false;
26  std::string inputRfFile;
27  std::string inputConfigFileName;
28  std::string outputImgFile;
29  std::string operation="BRIGHTNESS_SCAN_CONVERT";
30  bool useCompression(true);
31 
32  int verboseLevel=vtkPlusLogger::LOG_LEVEL_UNDEFINED;
33 
34  vtksys::CommandLineArguments args;
35  args.Initialize(argc, argv);
36 
37  args.AddArgument("--help", vtksys::CommandLineArguments::NO_ARGUMENT, &printHelp, "Print this help.");
38  args.AddArgument("--rf-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputRfFile, "File name of input RF image data");
39  args.AddArgument("--config-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputConfigFileName, "Config file containing processing parameters");
40  args.AddArgument("--output-img-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &outputImgFile, "File name of the generated output brightness image");
41  args.AddArgument("--use-compression", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &useCompression, "Use compression when outputting data");
42  args.AddArgument("--operation", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &operation, "Processing operation to be applied on the input file (BRIGHTNESS_CONVERT, BRIGHTNESS_SCAN_CONVERT, default: BRIGHTNESS_SCAN_CONVERT");
43  args.AddArgument("--verbose", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &verboseLevel, "Verbose level (1=error only, 2=warning, 3=info, 4=debug, 5=trace)");
44 
45 
46  // Input arguments error checking
47  if ( !args.Parse() )
48  {
49  std::cerr << "Problem parsing arguments" << std::endl;
50  std::cout << "Help: " << args.GetHelp() << std::endl;
51  exit(EXIT_FAILURE);
52  }
53 
54  if ( printHelp )
55  {
56  std::cout << args.GetHelp() << std::endl;
57  exit(EXIT_SUCCESS);
58  }
59 
60  vtkPlusLogger::Instance()->SetLogLevel(verboseLevel);
61 
62  if (inputConfigFileName.empty())
63  {
64  std::cerr << "--config-file required " << std::endl;
65  exit(EXIT_FAILURE);
66  }
67  if (inputRfFile.empty())
68  {
69  std::cerr << "--rf-file required" << std::endl;
70  exit(EXIT_FAILURE);
71  }
72  if (outputImgFile.empty())
73  {
74  std::cerr << "Missing --output-img-file parameter. Specification of the output image file name is required." << std::endl;
75  exit(EXIT_FAILURE);
76  }
77 
78  // Read transformations data
79  LOG_DEBUG("Reading input meta file...");
80  // frameList it will contain initially the RF data and the image data will be replaced by the processed output
81  vtkSmartPointer< vtkIGSIOTrackedFrameList > frameList = vtkSmartPointer< vtkIGSIOTrackedFrameList >::New();
82  if(vtkPlusSequenceIO::Read(inputRfFile, frameList) != PLUS_SUCCESS )
83  {
84  LOG_ERROR("Unable to load input sequences file.");
85  exit(EXIT_FAILURE);
86  }
87  LOG_DEBUG("Reading input RF file completed");
88 
89  // Read config file
90  LOG_DEBUG("Reading config file...")
91  vtkSmartPointer<vtkXMLDataElement> configRootElement = vtkSmartPointer<vtkXMLDataElement>::New();
92  if (PlusXmlUtils::ReadDeviceSetConfigurationFromFile(configRootElement, inputConfigFileName.c_str())==PLUS_FAIL)
93  {
94  LOG_ERROR("Unable to read configuration from file " << inputConfigFileName.c_str());
95  return EXIT_FAILURE;
96  }
97  LOG_DEBUG("Reading config file finished.");
98 
99  vtkXMLDataElement* dataCollectionConfig = configRootElement->FindNestedElementWithName("DataCollection");
100  if (dataCollectionConfig == NULL)
101  {
102  LOG_ERROR("Cannot find DataCollection element in XML tree!");
103  return PLUS_FAIL;
104  }
105  vtkXMLDataElement* deviceConfig = dataCollectionConfig->FindNestedElementWithName("Device");
106  if (deviceConfig == NULL)
107  {
108  LOG_ERROR("Cannot find DataCollection/ImageAcquisition element in XML tree!");
109  return PLUS_FAIL;
110  }
111  vtkXMLDataElement* outputChannelsElement = deviceConfig->FindNestedElementWithName("OutputChannels");
112  if (outputChannelsElement == NULL)
113  {
114  LOG_ERROR("Cannot find OutputChannels element in device tag tree!");
115  return PLUS_FAIL;
116  }
117 
118  for ( int i = 0; i < outputChannelsElement->GetNumberOfNestedElements(); ++i )
119  {
120  vtkXMLDataElement* outputChannelElement = outputChannelsElement->GetNestedElement(i);
121 
122  if (STRCASECMP(outputChannelElement->GetName(), "OutputChannel") != 0 )
123  {
124  continue;
125  }
126 
127  vtkXMLDataElement* rfProcesingElement = outputChannelElement->FindNestedElementWithName("RfProcessing");
128  if (rfProcesingElement == NULL)
129  {
130  LOG_ERROR("Cannot find RfProcessing element in channel tag!");
131  return PLUS_FAIL;
132  }
133 
134  // Create converter
135  vtkSmartPointer<vtkPlusRfProcessor> rfProcessor = vtkSmartPointer<vtkPlusRfProcessor>::New();
136  if ( rfProcessor->ReadConfiguration(rfProcesingElement) != PLUS_SUCCESS )
137  {
138  LOG_ERROR("Failed to read conversion parameters from the configuration file");
139  exit(EXIT_FAILURE);
140  }
141 
142  // Process the frames
143  for (unsigned int j = 0; j < frameList->GetNumberOfTrackedFrames(); j++)
144  {
145  igsioTrackedFrame* rfFrame = frameList->GetTrackedFrame(j);
146 
147  // Do the conversion
148  rfProcessor->SetRfFrame(rfFrame->GetImageData()->GetImage(), rfFrame->GetImageData()->GetImageType());
149 
150  if (STRCASECMP(operation.c_str(),"BRIGHTNESS_CONVERT")==0)
151  {
152  // do brightness conversion only
153  vtkImageData* brightnessImage = rfProcessor->GetBrightnessConvertedImage();
154  // Update the pixel data in the frame
155  rfFrame->GetImageData()->DeepCopyFrom(brightnessImage);
156  rfFrame->GetImageData()->SetImageType(US_IMG_BRIGHTNESS);
157  }
158  else if (STRCASECMP(operation.c_str(),"BRIGHTNESS_SCAN_CONVERT")==0)
159  {
160  // do brightness and scan conversion
161  vtkImageData* brightnessImage = rfProcessor->GetBrightnessScanConvertedImage();
162  // Update the pixel data in the frame
163  rfFrame->GetImageData()->DeepCopyFrom(brightnessImage);
164  rfFrame->GetImageData()->SetImageOrientation(US_IMG_ORIENT_MF);
165  rfFrame->GetImageData()->SetImageType(US_IMG_BRIGHTNESS);
166  }
167  else
168  {
169  LOG_ERROR("Unknown operation: "<<operation);
170  exit(EXIT_FAILURE);
171  }
172  }
173 
174  std::ostringstream ss;
175  std::string path = vtksys::SystemTools::GetFilenamePath(outputImgFile);
176  if( !path.empty() )
177  {
178  ss << path << "/";
179  }
180  if( outputChannelElement->GetAttribute("Id") != NULL )
181  {
182  ss << vtksys::SystemTools::GetFilenameWithoutExtension(outputImgFile) << "_OutputChannel_" << outputChannelElement->GetAttribute("Id") << vtksys::SystemTools::GetFilenameExtension(outputImgFile);
183  }
184  else
185  {
186  ss << vtksys::SystemTools::GetFilenameWithoutExtension(outputImgFile) << "_OutputChannel_" << i << vtksys::SystemTools::GetFilenameExtension(outputImgFile);
187  }
188 
189  if(vtkPlusSequenceIO::Write(ss.str(), frameList, frameList->GetImageOrientation(), useCompression) != PLUS_SUCCESS )
190  {
191  // Error has already been logged
192  exit(EXIT_FAILURE);
193  }
194  }
195 
196  return EXIT_SUCCESS;
197 }
int main(int argc, char **argv)
Definition: RfProcessor.cxx:23
for i
#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()
static PlusStatus ReadDeviceSetConfigurationFromFile(vtkXMLDataElement *config, const char *filename)
Definition: PlusXmlUtils.h:23