PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
EnhanceBone.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 "igsioVideoFrame.h"
9 #include "igsioTrackedFrame.h"
11 #include "vtkImageCast.h"
12 #include "vtkImageData.h"
13 #include "vtkMetaImageReader.h"
14 #include "vtkMetaImageWriter.h"
15 #include "vtkIGSIOSequenceIO.h"
16 #include "vtkSmartPointer.h"
17 #include "vtkIGSIOTrackedFrameList.h"
18 #include "vtkXMLUtilities.h"
19 #include "vtksys/CommandLineArguments.hxx"
20 
21 //----------------------------------------------------------------------------
22 int main(int argc, char** argv)
23 {
24  // Setup for command line arguments
25  bool printHelp(false);
26  std::string inputImgSeqFileName;
27  std::string outputImgSeqFileName;
28  std::string inputConfigFileName;
29  int verboseLevel = vtkPlusLogger::LOG_LEVEL_UNDEFINED;
30 
31  vtksys::CommandLineArguments args;
32  args.Initialize(argc, argv);
33 
34  args.AddArgument("--source-seq-file",vtksys::CommandLineArguments::EQUAL_ARGUMENT, &inputImgSeqFileName, "The ultrasound sequence to draw the scanlines on.");
35  args.AddArgument("--output-seq-file",vtksys::CommandLineArguments::EQUAL_ARGUMENT, &outputImgSeqFileName, "The output ultrasound sequence with scanlines overlaid on the images.");
36  args.AddArgument("--help", vtksys::CommandLineArguments::NO_ARGUMENT, &printHelp, "Print this help.");
37  args.AddArgument("--verbose", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &verboseLevel, "Verbose level (1=error only, 2=warning, 3=info, 4=debug, 5=trace)");
38 
39  // Fail if arguments can't be parsed
40  if (!args.Parse())
41  {
42  std::cerr << "Error parsing arguments." << std::endl;
43  std::cout << "Help: " << args.GetHelp() << std::endl;
44  exit(EXIT_FAILURE);
45  }
46  // Print help if requested
47  if (printHelp)
48  {
49  std::cout << args.GetHelp() << std::endl;
50  exit(EXIT_SUCCESS);
51  }
52 
53  vtkPlusLogger::Instance()->SetLogLevel(verboseLevel);
54 
55  // Fail if no ultrasound image file specified
56  if (inputImgSeqFileName.empty())
57  {
58  LOG_ERROR("--seq-file required");
59  exit(EXIT_FAILURE);
60  }
61 
62  // Read the image sequence
63  vtkSmartPointer<vtkIGSIOTrackedFrameList> trackedFrameList = vtkSmartPointer<vtkIGSIOTrackedFrameList>::New();
64  if( vtkIGSIOSequenceIO::Read(inputImgSeqFileName, trackedFrameList) != PLUS_SUCCESS )
65  {
66  LOG_ERROR("Unable to read sequence file: " << inputImgSeqFileName);
67  exit(EXIT_FAILURE);
68  }
69 
70  vtkSmartPointer<vtkImageCast> castToDouble = vtkSmartPointer<vtkImageCast>::New();
71  castToDouble->SetOutputScalarTypeToDouble();
72 
73  vtkSmartPointer<vtkPlusForoughiBoneSurfaceProbability> boneSurfaceFilter = vtkSmartPointer<vtkPlusForoughiBoneSurfaceProbability>::New();
74  boneSurfaceFilter->SetInputConnection(castToDouble->GetOutputPort());
75 
76  vtkSmartPointer<vtkImageCast> castToUnsignedChar = vtkSmartPointer<vtkImageCast>::New();
77  castToUnsignedChar->SetOutputScalarTypeToUnsignedChar();
78  castToUnsignedChar->SetInputConnection(boneSurfaceFilter->GetOutputPort());
79 
80  int numberOfFrames = trackedFrameList->GetNumberOfTrackedFrames();
81  LOG_INFO("Processing "<<numberOfFrames<<" frames...");
82  for (int frameIndex = 0; frameIndex < numberOfFrames; frameIndex++)
83  {
84  TrackedFrame* frame = trackedFrameList->GetTrackedFrame(frameIndex);
85  vtkImageData* imageData = frame->GetImageData()->GetImage();
86 
87  castToDouble->SetInputData(imageData);
88  castToUnsignedChar->Update();
89 
90  // Write back the processed output to the input trackedframelist
91  frame->GetImageData()->DeepCopyFrom(castToUnsignedChar->GetOutput());
92  }
93 
94  // Write the new TrackedFrameList to metafile
95  LOG_INFO("Writing new sequence to file...");
96  if (outputImgSeqFileName.empty())
97  {
98  int extensionDot = inputImgSeqFileName.find_last_of(".");
99  if (extensionDot != std::string::npos)
100  {
101  inputImgSeqFileName = inputImgSeqFileName.substr(0,extensionDot);
102  }
103  outputImgSeqFileName = inputImgSeqFileName + "-Bones.nrrd";
104  }
105  if( vtkIGSIOSequenceIO::Write(outputImgSeqFileName, trackedFrameList) != PLUS_SUCCESS )
106  {
107  LOG_ERROR("Failed to save output volume to " << outputImgSeqFileName);
108  return EXIT_FAILURE;
109  }
110  LOG_INFO("Writing to "<<outputImgSeqFileName<<" complete.");
111 
112  return EXIT_SUCCESS;
113 }
#define PLUS_SUCCESS
Definition: PlusCommon.h:44
int main(int argc, char **argv)
Definition: EnhanceBone.cxx:22
static vtkIGSIOLogger * Instance()