PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
vtkSonixVolumeReaderTest1.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"
9 
10 // VTK includes
11 #include "vtkImageData.h"
12 #include "vtkImageDifference.h"
13 #include "vtkImageExtractComponents.h"
15 #include "vtkSmartPointer.h"
16 #include "vtksys/CommandLineArguments.hxx"
17 #include "vtksys/SystemTools.hxx"
18 
19 // IGSIO includes
20 #include "igsioVideoFrame.h"
21 #include "igsioTrackedFrame.h"
22 #include "vtkIGSIOSequenceIO.h"
23 #include "vtkIGSIOTrackedFrameList.h"
24 
25 // STD includes
26 #include <iostream>
27 #include <stdlib.h>
28 
29 int main ( int argc, char* argv[] )
30 {
31  bool printHelp( false );
32  std::string inputFileName;
33  std::string inputBaselineName;
34  int inputFrameNumber( -1 );
35  std::string outputFileName;
36 
37  vtksys::CommandLineArguments args;
38  args.Initialize( argc, argv );
39 
40  int verboseLevel = vtkPlusLogger::LOG_LEVEL_ERROR;
41 
42  args.AddArgument( "--help", vtksys::CommandLineArguments::NO_ARGUMENT, &printHelp, "Print this help." );
43  args.AddArgument( "--volume-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputFileName, "The file name of the Sonix volume." );
44  args.AddArgument( "--output-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &outputFileName, "Sequence meta file name to save (save only if defined)" );
45  args.AddArgument( "--frame-number", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputFrameNumber, "The frame number to compare with baseline." );
46  args.AddArgument( "--baseline", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputBaselineName, "The file name of the baseline image." );
47  args.AddArgument( "--verbose", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &verboseLevel, "Verbose level (Default: 1; 1=error only, 2=warning, 3=info, 4=debug)" );
48 
49  if ( !args.Parse() )
50  {
51  std::cerr << "Problem parsing arguments" << std::endl;
52  std::cout << "\n\nvtkPlusICCapturingSourceTest1 help:" << args.GetHelp() << std::endl;
53  exit( EXIT_FAILURE );
54  }
55 
56  vtkPlusLogger::Instance()->SetLogLevel( verboseLevel );
57 
58  if ( printHelp )
59  {
60  std::cout << "\n\nvtkPlusICCapturingSourceTest1 help:" << args.GetHelp() << std::endl;
61  exit( EXIT_SUCCESS );
62 
63  }
64 
65  if ( inputFileName.empty() )
66  {
67  LOG_ERROR( "The input-file-name parameter is required!" );
68  exit( EXIT_FAILURE );
69  }
70 
71 
72  vtkSmartPointer<vtkIGSIOTrackedFrameList> sonixVolumeData = vtkSmartPointer<vtkIGSIOTrackedFrameList>::New();
73 
74  if ( vtkPlusSonixVolumeReader::GenerateTrackedFrameFromSonixVolume( inputFileName.c_str(), sonixVolumeData ) != PLUS_SUCCESS )
75  {
76  LOG_ERROR( "Failed to generate tracked frame from sonix volume: " << inputFileName );
77  exit( EXIT_FAILURE );
78  }
79 
80  if ( !outputFileName.empty() )
81  {
82  std::string path = vtkPlusConfig::GetInstance()->GetOutputPath( outputFileName );
83  LOG_INFO( "Save tracked frames to " << path );
84  if( vtkIGSIOSequenceIO::Write( path, sonixVolumeData, sonixVolumeData->GetImageOrientation(), false ) != PLUS_SUCCESS )
85  {
86  LOG_ERROR( "Failed to save sonix volume to " << path );
87  }
88  }
89 
90  vtkSmartPointer<vtkImageDifference> imgDiff = vtkSmartPointer<vtkImageDifference>::New();
91  imgDiff->AllowShiftOff();
92 
93  igsioVideoFrame baselineVideoFrame;
94  if ( igsioVideoFrame::ReadImageFromFile( baselineVideoFrame, inputBaselineName.c_str() ) != PLUS_SUCCESS )
95  {
96  LOG_ERROR( "Failed to read baseline image from file: " << inputBaselineName );
97  exit( EXIT_FAILURE );
98  }
99 
100  vtkSmartPointer<vtkImageExtractComponents> imageExtractorBase = vtkSmartPointer<vtkImageExtractComponents>::New();
101  imageExtractorBase->SetInputData( baselineVideoFrame.GetImage() );
102  imageExtractorBase->SetComponents( 0, 0, 0 ); // we are using only the 0th component
103  imageExtractorBase->Update();
104  vtkImageData* baselineRGB = imageExtractorBase->GetOutput();
105 
106  if ( inputFrameNumber < 0 )
107  {
108  inputFrameNumber = 0;
109  }
110  unsigned int inputFrameNumberUint = static_cast<unsigned int>( inputFrameNumber );
111  if ( sonixVolumeData->GetNumberOfTrackedFrames() < inputFrameNumberUint )
112  {
113  LOG_ERROR( "Unable to get tracked frame from list, frame number (" << inputFrameNumberUint
114  << ") is larger than tracked frame list size (" << sonixVolumeData->GetNumberOfTrackedFrames() << ")!" );
115  exit( EXIT_FAILURE );
116  }
117 
118  igsioVideoFrame* videoFrame = sonixVolumeData->GetTrackedFrame( inputFrameNumberUint )->GetImageData();
119 
120  if ( !videoFrame->IsImageValid() )
121  {
122  LOG_ERROR( "Video frame is not valid!" );
123  exit( EXIT_FAILURE );
124  }
125 
126  vtkSmartPointer<vtkImageExtractComponents> imageExtractorInput = vtkSmartPointer<vtkImageExtractComponents>::New();
127  imageExtractorInput->SetInputData( videoFrame->GetImage() );
128  imageExtractorInput->SetComponents( 0, 0, 0 ); // we are using only the 0th component
129 
130  imgDiff->SetImageData( baselineRGB );
131 
132  imgDiff->SetInputConnection( imageExtractorInput->GetOutputPort() );
133  imgDiff->Update();
134 
135  double error = imgDiff->GetError();
136 
137  if ( error > 0 )
138  {
139  std::cout << "Error = " << error << std::endl;
140  return EXIT_FAILURE;
141  }
142 
143  return EXIT_SUCCESS;
144 }
std::string GetOutputPath(const std::string &subPath)
static vtkPlusConfig * GetInstance()
static PlusStatus GenerateTrackedFrameFromSonixVolume(const char *volumeFileName, vtkIGSIOTrackedFrameList *trackedFrameList, double acquisitionFrameRate=10)
#define PLUS_SUCCESS
Definition: PlusCommon.h:44
int main(int argc, char *argv[])
static vtkIGSIOLogger * Instance()