PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
ConvertFcsvToXml.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 // Local includes
8 #include "PlusConfigure.h"
11 #include "vtkFcsvReader.h"
12 
13 // STL includes
14 #include <iostream>
15 #include <fstream>
16 #include <sstream>
17 #include <limits>
18 
19 // VTK includes
20 #include <vtksys/CommandLineArguments.hxx>
21 #include <vtkXMLDataElement.h>
22 #include <vtkXMLUtilities.h>
23 
24 // ITK includes
25 #include "itkLandmarkSpatialObject.h"
26 
27 //----------------------------------------------------------------------------
28 void WriteFiducialPositions(std::ofstream& outFile, const std::string& inputTestcaseName, const std::string& inputImageSequenceFileName, const std::string& fiducialPosFileName)
29 {
30  vtkSmartPointer<vtkFcsvReader> readerf = vtkSmartPointer<vtkFcsvReader>::New();
31  readerf->SetFileName(fiducialPosFileName);
32  readerf->Update();
33  auto fcsvData = readerf->GetFcsvDataObject();
34 
35  double sumFiducialNum = 0; // divide by frame number
36 
37  LOG_DEBUG("Number Of Points: " << fcsvData.points.size());
38  if (fcsvData.points.size() == 0)
39  {
40  LOG_ERROR("No fiducial points.");
41  exit(EXIT_FAILURE);
42  }
43 
44  std::vector<FcsvPoint>::iterator it = fcsvData.points.begin();
45  //int fidNum = 7 ; // number of fiducials for a vector, 7 because 7 is max number of fiducials visible in the phantom
46  int firstFrameIndex = std::numeric_limits<int>::max();
47  int lastFrameIndex = -1;
48  while (it != fcsvData.points.end())
49  {
50  int frameIndex = (*it).position[2];
51  if (firstFrameIndex > frameIndex)
52  {
53  firstFrameIndex = frameIndex;
54  }
55  if (lastFrameIndex < frameIndex)
56  {
57  lastFrameIndex = frameIndex;
58  }
59  it++;
60  }
61  int numberOfFrames = lastFrameIndex - firstFrameIndex + 1;
62 
63  for (int currentFrameIndex = firstFrameIndex; currentFrameIndex <= lastFrameIndex; currentFrameIndex++)
64  {
65  std::vector<FcsvPoint>::iterator it = fcsvData.points.begin();
66  std::vector<std::vector<double>> foundDotsCoordinateValue;
67  int validFidNum = 0;
68  while (it != fcsvData.points.end())
69  {
70  if (currentFrameIndex == (*it).position[2])
71  {
72  // done to comply with earlier code ( see Segmentation.cpp void Segmentation::segment )
73  std::vector<double> fiducial(4, 0);
74  fiducial[0] = (*it).position[0];
75  fiducial[1] = (*it).position[1];
76  fiducial[2] = 0;
77  fiducial[3] = 1;
78  if ((*it).position[0] != 0.0 || (*it).position[1] != 0.0)
79  {
80  // if coordinates are 0,0 it means that the fiducial was not found
81  validFidNum++;
82  }
83  foundDotsCoordinateValue.push_back(fiducial);
84  }
85  it++;
86  }
87 
88  PlusPatternRecognitionResult patRecognitionResults;
89  patRecognitionResults.SetFoundDotsCoordinateValue(foundDotsCoordinateValue);
90  patRecognitionResults.SetDotsFound(true);
91 
92  sumFiducialNum = sumFiducialNum + validFidNum;
93 
94  PlusUsFidSegResultFile::WriteSegmentationResults(outFile, patRecognitionResults, inputTestcaseName, currentFrameIndex, inputImageSequenceFileName);
95 
96  if (vtkPlusLogger::Instance()->GetLogLevel() >= vtkPlusLogger::LOG_LEVEL_DEBUG)
97  {
98  PlusUsFidSegResultFile::WriteSegmentationResults(std::cout, patRecognitionResults, inputTestcaseName, currentFrameIndex, inputImageSequenceFileName);
99  }
100 
101  }
102 
103  double meanFid = sumFiducialNum / numberOfFrames;
105 }
106 
107 //----------------------------------------------------------------------------
108 int main(int argc, char** argv)
109 {
110  std::string inputTestDataDir;
111  std::string inputFcsvFileName;
112  std::string inputImageSequenceFileName;
113  std::string inputTestcaseName;
114  std::string outputXmlFileName;
115 
116  int verboseLevel(vtkPlusLogger::LOG_LEVEL_UNDEFINED);
117 
118  vtksys::CommandLineArguments args;
119  args.Initialize(argc, argv);
120 
121  args.AddArgument("--test-data-dir", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputTestDataDir, "Test data directory");
122  args.AddArgument("--fcsv-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputFcsvFileName, "Name of file storing fiducial point coordinates");
123  args.AddArgument("--img-seq-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputImageSequenceFileName, "Filename of the input image sequence. Segmentation will be performed for all frames of the sequence.");
124  args.AddArgument("--testcase", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputTestcaseName, "Name of the test case that will be printed to the output");
125  args.AddArgument("--output-xml-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &outputXmlFileName, "Name of file storing results of a new segmentation (fiducial coordinates, intensity, angle)");
126  args.AddArgument("--verbose", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &verboseLevel, "Verbose level (1=error only, 2=warning, 3=info, 4=debug, 5=trace)");
127 
128  if (!args.Parse())
129  {
130  std::cerr << "Problem parsing arguments" << std::endl;
131  std::cout << "Help: " << args.GetHelp() << std::endl;
132  exit(EXIT_FAILURE);
133  }
134 
135  vtkPlusLogger::Instance()->SetLogLevel(verboseLevel);
136 
137  if (inputImageSequenceFileName.empty() || inputFcsvFileName.empty() || inputTestcaseName.empty() || outputXmlFileName.empty())
138  {
139  std::cerr << "--img-seq-file, --input-fcsv-file, --input-testcase-name, and --output-xml-file parameters are required" << std::endl;
140  exit(EXIT_FAILURE);
141  }
142 
143  std::ofstream outputXmlFile;
144  outputXmlFile.open(outputXmlFileName.c_str());
146 
147  PlusFidPatternRecognition patternRecognition;
148  PlusUsFidSegResultFile::WriteSegmentationResultsParameters(outputXmlFile, patternRecognition, inputFcsvFileName);
149 
150  std::string inputFcsvFilePath = inputTestDataDir + "/" + inputFcsvFileName;
151  WriteFiducialPositions(outputXmlFile, inputTestcaseName, inputImageSequenceFileName, inputFcsvFilePath);
153  outputXmlFile.close();
154 
155  return EXIT_SUCCESS;
156 }
static void WriteSegmentationResultsFooter(std::ostream &outFile)
static void WriteSegmentationResultsParameters(std::ostream &outFile, PlusFidPatternRecognition &patternRcognitionObject, const std::string &trueFidFileName)
static void WriteSegmentationResults(std::ostream &outFile, PlusPatternRecognitionResult &segResults, const std::string &inputTestcaseName, int currentFrameIndex, const std::string &inputImageSequenceFileName)
void SetFoundDotsCoordinateValue(std::vector< std::vector< double > > value)
static vtkIGSIOLogger * Instance()
int main(int argc, char **argv)
void WriteFiducialPositions(std::ofstream &outFile, const std::string &inputTestcaseName, const std::string &inputImageSequenceFileName, const std::string &fiducialPosFileName)
static void WriteSegmentationResultsStats(std::ostream &outFile, double meanFid, double meanFidCandidate=-1)
static void WriteSegmentationResultsHeader(std::ostream &outFile)