CustusX  2022.05.26-dev+develop.ffefb6
An IGT application
cxAlgorithmHelpers.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 #include "cxAlgorithmHelpers.h"
12 
13 #include "cxImage.h"
14 #include "cxTypeConversions.h"
15 #include "itkImageFileReader.h"
16 #include "vtkMetaImageWriter.h"
17 #include "cxSettings.h"
18 #include <QDir>
19 #include "cxUtilHelpers.h"
20 #include "cxDataLocations.h"
21 #include "cxLogger.h"
22 #include <itkGrayscaleFillholeImageFilter.h>
23 
24 namespace cx
25 {
26 
27 //---------------------------------------------------------------------------------------------------------------------
28 itkImageType::ConstPointer AlgorithmHelper::getITKfromVTKImage(vtkImageDataPtr image)
29 {
30  //HACK
31  return AlgorithmHelper::getITKfromVTKImageViaFile(image);
32 }
33 //---------------------------------------------------------------------------------------------------------------------
34 
35 itkImageType::ConstPointer AlgorithmHelper::getITKfromSSCImage(ImagePtr input)
36 {
37  if (!input)
39  else
40  return getITKfromVTKImage(input->getBaseVtkImageData());
41 }
42 //---------------------------------------------------------------------------------------------------------------------
43 
48 itkImageType::ConstPointer AlgorithmHelper::getITKfromVTKImageViaFile(vtkImageDataPtr input)
49 {
50  if(!input)
51  {
52  std::cout << "getITKfromSSCImage(): NO image!!!" << std::endl;
53  return itkImageType::ConstPointer();
54  }
55 
56  double minVal = input->GetScalarRange()[0];
57  double maxVal = input->GetScalarRange()[1];
58 
59  if(maxVal > SHRT_MAX || minVal < SHRT_MIN)
60  reportWarning("Image values out of range. max: " + qstring_cast(maxVal)
61  + " min: " + qstring_cast(minVal) + " See bug #363 if this needs to be fixed");
62 
63  QString tempFolder = DataLocations::getCachePath() + "/vtk2itk/";
64  QDir().mkpath(tempFolder);
65 
66  // write to disk
67  vtkMetaImageWriterPtr writer = vtkMetaImageWriterPtr::New();
68 
69  QString filename = tempFolder + "/"+qstring_cast(writer.GetPointer())+".mhd";
70 
71 
72  writer->SetInputData(input);
73  writer->SetFileName(cstring_cast(filename));
74  writer->SetCompression(false);
75  writer->Write();
76 
77  // read from disk
78  typedef itk::ImageFileReader<itkImageType> ReaderType;
79  ReaderType::Pointer reader = ReaderType::New();
80 #if ITK_VERSION_MAJOR==3
81  reader->SetFileName(cstring_cast(filename));
82 #else
83  reader->SetFileName(string_cast(filename));
84 #endif
85 
87 // reader->SetFileName(string_cast(filename));
88  reader->Update();
89  itkImageType::ConstPointer retval = reader->GetOutput();
90 
91  QFile(filename).remove(); // cleanup
92  QFile(changeExtension(filename, "raw")).remove(); // cleanup
93 
94  return retval;
95 }
96 //---------------------------------------------------------------------------------------------------------------------
97 
98 vtkImageDataPtr AlgorithmHelper::getVTKFromITK(itkImageType::ConstPointer input)
99 {
100  //Convert ITK to VTK
101  itkToVtkFilterType::Pointer itkToVtkFilter = itkToVtkFilterType::New();
102  itkToVtkFilter->SetInput(input);
103  itkToVtkFilter->Update();
104 
105  vtkImageDataPtr rawResult = vtkImageDataPtr::New();
106  rawResult->DeepCopy(itkToVtkFilter->GetOutput());
107  // TODO: possible memory problem here - check debug mem system of itk/vtk
108 
109  return rawResult;
110 }
111 
113 {
114  itkImageType::ConstPointer itkImage = AlgorithmHelper::getITKfromVTKImage(input);
115 
116  typedef itk::GrayscaleFillholeImageFilter<itkImageType, itkImageType> FilterType;
117  FilterType::Pointer filter = FilterType::New();
118 
119  filter->SetInput(itkImage);
120  filter->Update();
121 
122  return AlgorithmHelper::getVTKFromITK(filter->GetOutput());
123 }
124 
125 
126 }
QString qstring_cast(const T &val)
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:27
cstring_cast_Placeholder cstring_cast(const T &val)
std::string string_cast(const T &val)
static vtkImageDataPtr getVTKFromITK(itkImageType::ConstPointer input)
void reportWarning(QString msg)
Definition: cxLogger.cpp:70
static QString getCachePath()
return path to a folder that is used during execution, will be cleared at start and stop...
vtkSmartPointer< class vtkMetaImageWriter > vtkMetaImageWriterPtr
static itkImageType::ConstPointer getITKfromVTKImage(vtkImageDataPtr image)
QString changeExtension(QString name, QString ext)
static itkImageType::ConstPointer getITKfromSSCImage(ImagePtr image)
static vtkImageDataPtr execute_itk_GrayscaleFillholeImageFilter(vtkImageDataPtr input)
vtkSmartPointer< class vtkImageData > vtkImageDataPtr
Namespace for all CustusX production code.