PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
vtkSegmentedWiresPositionsTest.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 
10 #include "igsioTrackedFrame.h"
11 #include "vtkCommand.h"
12 #include "vtkMath.h"
13 #include "vtkMatrix4x4.h"
14 #include "vtkIGSIOSequenceIO.h"
15 #include "vtkSmartPointer.h"
16 #include "vtkIGSIOTrackedFrameList.h"
17 #include "vtkTransform.h"
18 #include "vtksys/CommandLineArguments.hxx"
19 #include <iostream>
20 #include <stdlib.h>
21 
23 
24 int main (int argc, char* argv[])
25 {
26  std::string inputSequenceMetafile;
27  std::string inputTransformName;
28  std::string outputWirePositionFile("./SegmentedWirePositions.txt");
29 
30  std::string inputBaselineFileName;
31  double inputTranslationErrorThreshold(0);
32  double inputRotationErrorThreshold(0);
33 
34  int verboseLevel=vtkPlusLogger::LOG_LEVEL_UNDEFINED;
35 
36  vtksys::CommandLineArguments cmdargs;
37  cmdargs.Initialize(argc, argv);
38 
39  cmdargs.AddArgument("--image-seq-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputSequenceMetafile, "Image sequence metafile");
40  cmdargs.AddArgument("--image-position-transform", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputTransformName, "Transform name used for image position display");
41  cmdargs.AddArgument("--output-wire-position-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &outputWirePositionFile, "Result wire position file name (Default: ./SegmentedWirePositions.txt)");
42 
43  cmdargs.AddArgument("--baseline", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputBaselineFileName, "Name of file storing baseline calibration results");
44  cmdargs.AddArgument("--translation-error-threshold", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputTranslationErrorThreshold, "Translation error threshold in mm.");
45  cmdargs.AddArgument("--rotation-error-threshold", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputRotationErrorThreshold, "Rotation error threshold in degrees.");
46  cmdargs.AddArgument("--verbose", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &verboseLevel, "Verbose level (1=error only, 2=warning, 3=info, 4=debug, 5=trace)");
47 
48  if ( !cmdargs.Parse() )
49  {
50  std::cerr << "Problem parsing arguments" << std::endl;
51  std::cout << "Help: " << cmdargs.GetHelp() << std::endl;
52  exit(EXIT_FAILURE);
53  }
54 
55  vtkPlusLogger::Instance()->SetLogLevel(verboseLevel);
56 
57  if ( inputSequenceMetafile.empty() )
58  {
59  std::cerr << "image-seq-file argument required" << std::endl << std::endl;
60  std::cout << "Help: " << cmdargs.GetHelp() << std::endl;
61  exit(EXIT_FAILURE);
62 
63  }
64 
65  LOG_INFO( "Reading sequence meta file");
66  vtkSmartPointer<vtkIGSIOTrackedFrameList> trackedFrameList = vtkSmartPointer<vtkIGSIOTrackedFrameList>::New();
67  if( vtkIGSIOSequenceIO::Read(inputSequenceMetafile, trackedFrameList) != PLUS_SUCCESS )
68  {
69  LOG_ERROR("Failed to read sequence metafile: " << inputSequenceMetafile);
70  return EXIT_FAILURE;
71  }
72 
73  std::ofstream positionInfo;
74  positionInfo.open (outputWirePositionFile.c_str(), ios::out );
75 
76  LOG_INFO( "Segmenting frames...");
77 
78  for ( unsigned int frameIndex = 0; frameIndex < trackedFrameList->GetNumberOfTrackedFrames(); frameIndex++ )
79  {
80  vtkPlusLogger::PrintProgressbar( (100.0 * frameIndex) / trackedFrameList->GetNumberOfTrackedFrames() );
81 
82  PlusFidPatternRecognition patternRecognition;
85 
86  try
87  {
88  // Send the image to the Segmentation component to segment
89  if (trackedFrameList->GetTrackedFrame(frameIndex)->GetImageData()->GetVTKScalarPixelType() != VTK_UNSIGNED_CHAR)
90  {
91  LOG_ERROR("patternRecognition.RecognizePattern only works on unsigned char images");
92  }
93  else
94  {
95  patternRecognition.RecognizePattern( trackedFrameList->GetTrackedFrame(frameIndex), segResults, error, frameIndex );
96  }
97  }
98  catch(...)
99  {
100  LOG_ERROR("SegmentImage: The segmentation has failed due to UNKNOWN exception thrown, the image is ignored");
101  continue;
102  }
103 
104  igsioTransformName transformName;
105  if ( transformName.SetTransformName(inputTransformName.c_str()) != PLUS_SUCCESS )
106  {
107  LOG_ERROR("Invalid transform name: " << inputTransformName );
108  return EXIT_FAILURE;
109  }
110 
111  if ( segResults.GetDotsFound() )
112  {
113  double defaultTransform[16]={0};
114  if ( trackedFrameList->GetTrackedFrame(frameIndex)->GetFrameTransform(transformName, defaultTransform) != PLUS_SUCCESS )
115  {
116  LOG_ERROR("Failed to get default frame transform from tracked frame #" << frameIndex);
117  continue;
118  }
119 
120  vtkSmartPointer<vtkTransform> frameTransform = vtkSmartPointer<vtkTransform>::New();
121  frameTransform->SetMatrix(defaultTransform);
122 
123  double posZ = frameTransform->GetPosition()[2];
124  double rotZ = frameTransform->GetOrientation()[2];
125 
126  int dataType = -1;
127  positionInfo << dataType << "\t\t" << posZ << "\t" << rotZ << "\t\t";
128 
129  for (unsigned int i=0; i < segResults.GetFoundDotsCoordinateValue().size(); i++)
130  {
131  positionInfo << segResults.GetFoundDotsCoordinateValue()[i][0] << "\t" << segResults.GetFoundDotsCoordinateValue()[i][1] << "\t\t";
132  }
133 
134  positionInfo << std::endl;
135 
136  }
137  }
138 
139  positionInfo.close();
140  vtkPlusLogger::PrintProgressbar(100);
141  std::cout << std::endl;
142 
143  std::cout << "Exit success!!!" << std::endl;
144  return EXIT_SUCCESS;
145 }
for i
std::vector< std::vector< double > > & GetFoundDotsCoordinateValue()
#define PLUS_SUCCESS
Definition: PlusCommon.h:44
int main(int argc, char *argv[])
static vtkIGSIOLogger * Instance()