PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
vtkPlusVolumeReconstructor.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 #include "vtkPlusSequenceIO.h"
11 
12 // VTK includes
13 #include <vtkImageFlip.h>
14 #include <vtkObjectFactory.h>
15 #include <vtkPNGReader.h>
16 
18 
19 //----------------------------------------------------------------------------
21 {
22 }
23 
24 //----------------------------------------------------------------------------
26 {
27 }
28 
29 //----------------------------------------------------------------------------
30 void vtkPlusVolumeReconstructor::PrintSelf(ostream& os, vtkIndent indent)
31 {
32  this->Superclass::PrintSelf(os, indent);
33 }
34 
35 //----------------------------------------------------------------------------
36 igsioStatus vtkPlusVolumeReconstructor::SaveReconstructedVolumeToFile(const std::string& filename, bool accumulation/*=false*/, bool useCompression/*=true*/)
37 {
38  return vtkPlusVolumeReconstructor::SaveReconstructedVolumeToFile(filename, accumulation, useCompression, nullptr, nullptr);
39 }
40 
41 //----------------------------------------------------------------------------
42 igsioStatus vtkPlusVolumeReconstructor::SaveReconstructedVolumeToFile(const std::string& filename, bool accumulation/*=false*/, bool useCompression/*=true*/, std::vector<std::string>* customFields/*=nullptr*/, std::vector<std::string>* customValues/*=nullptr*/)
43 {
44  vtkSmartPointer<vtkImageData> volumeToSave = vtkSmartPointer<vtkImageData>::New();
45  if (accumulation)
46  {
47  if (this->ExtractAccumulation(volumeToSave) != PLUS_SUCCESS)
48  {
49  LOG_ERROR("Extracting accumulation buffer failed!");
50  return PLUS_FAIL;
51  }
52  }
53  else
54  {
55  if (this->ExtractGrayLevels(volumeToSave) != PLUS_SUCCESS)
56  {
57  LOG_ERROR("Extracting gray channel failed!");
58  return PLUS_FAIL;
59  }
60  }
61  return vtkPlusVolumeReconstructor::SaveReconstructedVolumeToFile(volumeToSave, filename, useCompression, customFields, customValues);
62 }
63 
64 //----------------------------------------------------------------------------
65 PlusStatus vtkPlusVolumeReconstructor::SaveReconstructedVolumeToFile(vtkImageData* volumeToSave, const std::string& filename, bool useCompression/*=true*/, std::vector<std::string>* customFields/*=nullptr*/, std::vector<std::string>* customValues/*=nullptr*/)
66 {
67  if (volumeToSave == NULL)
68  {
69  LOG_ERROR("vtkPlusVolumeReconstructor::SaveReconstructedVolumeToMetafile: invalid input image");
70  return PLUS_FAIL;
71  }
72 
73  int dims[3];
74  volumeToSave->GetDimensions(dims);
75  FrameSizeType frameSize = { static_cast<unsigned int>(dims[0]), static_cast<unsigned int>(dims[1]), static_cast<unsigned int>(dims[2]) };
76 
77  vtkNew<vtkIGSIOTrackedFrameList> list;
78  igsioTrackedFrame frame;
79  igsioVideoFrame image;
80  image.AllocateFrame(frameSize, volumeToSave->GetScalarType(), volumeToSave->GetNumberOfScalarComponents());
81  image.GetImage()->DeepCopy(volumeToSave);
82  image.SetImageOrientation(US_IMG_ORIENT_MFA);
83  image.SetImageType(US_IMG_BRIGHTNESS);
84  frame.SetImageData(image);
85  list->AddTrackedFrame(&frame);
86  if (customFields != nullptr && customValues != nullptr)
87  {
88  for (unsigned i=0; i < customFields->size(); ++i)
89  {
90  list->SetCustomString(customFields->at(i), customValues->at(i));
91  }
92  }
93  if (vtkPlusSequenceIO::Write(filename, list.GetPointer(), US_IMG_ORIENT_MF, useCompression) != PLUS_SUCCESS)
94  {
95  LOG_ERROR("Failed to save reconstructed volume in sequence metafile!");
96  return PLUS_FAIL;
97  }
98 
99  return PLUS_SUCCESS;
100 }
101 
102 //----------------------------------------------------------------------------
104 {
105  if (this->ImportanceMaskFilename == this->ImportanceMaskFilenameInReconstructor)
106  {
107  // no change
108  return PLUS_SUCCESS;
109  }
110  this->ImportanceMaskFilenameInReconstructor = this->ImportanceMaskFilename;
111 
112  if (!this->ImportanceMaskFilename.empty())
113  {
114  std::string importanceMaskFilePath;
115  if (vtkPlusConfig::GetInstance()->FindImagePath(this->ImportanceMaskFilename, importanceMaskFilePath) == PLUS_FAIL)
116  {
117  LOG_ERROR("Cannot get importance mask from file: " << this->ImportanceMaskFilename);
118  return PLUS_FAIL;
119  }
120  vtkSmartPointer<vtkPNGReader> reader = vtkSmartPointer<vtkPNGReader>::New();
121  reader->SetFileName(importanceMaskFilePath.c_str());
122  reader->Update();
123  if (reader->GetOutput() == NULL)
124  {
125  LOG_ERROR("Failed to read importance image from file: " << importanceMaskFilePath);
126  return PLUS_FAIL;
127  }
128  vtkSmartPointer<vtkImageFlip> flipYFilter = vtkSmartPointer<vtkImageFlip>::New();
129  flipYFilter->SetFilteredAxis(1); // flip y axis
130  flipYFilter->SetInputConnection(reader->GetOutputPort());
131  flipYFilter->Update();
132  this->Reconstructor->SetImportanceMask(flipYFilter->GetOutput());
133  }
134  else
135  {
136  this->Reconstructor->SetImportanceMask(NULL);
137  }
138  return PLUS_SUCCESS;
139 }
igsioStatus PlusStatus
Definition: PlusCommon.h:40
virtual PlusStatus SaveReconstructedVolumeToFile(const std::string &filename, bool accumulation=false, bool useCompression=true) override
for i
#define PLUS_FAIL
Definition: PlusCommon.h:43
static vtkPlusConfig * GetInstance()
virtual PlusStatus UpdateImportanceMask() override
static igsioStatus Write(const std::string &filename, igsioTrackedFrame *frame, US_IMAGE_ORIENTATION orientationInFile=US_IMG_ORIENT_MF, bool useCompression=true, bool EnableImageDataWrite=true)
PlusStatus FindImagePath(const std::string &aImagePath, std::string &aFoundAbsolutePath)
#define PLUS_SUCCESS
Definition: PlusCommon.h:44
Reconstructs a volume from tracked frames.
virtual void PrintSelf(ostream &os, vtkIndent indent) override
vtkStandardNewMacro(vtkPlusVolumeReconstructor)