Fraxinus  16.5.0-fx-rc3
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 #include "cxAlgorithmHelpers.h"
33 
34 #include "cxImage.h"
35 #include "cxTypeConversions.h"
36 #include "itkImageFileReader.h"
37 #include "vtkMetaImageWriter.h"
38 #include "cxSettings.h"
39 #include <QDir>
40 #include "cxUtilHelpers.h"
41 #include "cxDataLocations.h"
42 #include "cxLogger.h"
43 #include <itkGrayscaleFillholeImageFilter.h>
44 
45 namespace cx
46 {
47 
48 //---------------------------------------------------------------------------------------------------------------------
49 itkImageType::ConstPointer AlgorithmHelper::getITKfromVTKImage(vtkImageDataPtr image)
50 {
51  //HACK
52  return AlgorithmHelper::getITKfromVTKImageViaFile(image);
53 }
54 //---------------------------------------------------------------------------------------------------------------------
55 
56 itkImageType::ConstPointer AlgorithmHelper::getITKfromSSCImage(ImagePtr input)
57 {
58  if (!input)
60  else
61  return getITKfromVTKImage(input->getBaseVtkImageData());
62 }
63 //---------------------------------------------------------------------------------------------------------------------
64 
69 itkImageType::ConstPointer AlgorithmHelper::getITKfromVTKImageViaFile(vtkImageDataPtr input)
70 {
71  if(!input)
72  {
73  std::cout << "getITKfromSSCImage(): NO image!!!" << std::endl;
74  return itkImageType::ConstPointer();
75  }
76 
77  double minVal = input->GetScalarRange()[0];
78  double maxVal = input->GetScalarRange()[1];
79 
80  if(maxVal > SHRT_MAX || minVal < SHRT_MIN)
81  reportWarning("Image values out of range. max: " + qstring_cast(maxVal)
82  + " min: " + qstring_cast(minVal) + " See bug #363 if this needs to be fixed");
83 
84  QString tempFolder = DataLocations::getCachePath() + "/vtk2itk/";
85  QDir().mkpath(tempFolder);
86 
87  // write to disk
88  vtkMetaImageWriterPtr writer = vtkMetaImageWriterPtr::New();
89 
90  QString filename = tempFolder + "/"+qstring_cast(writer.GetPointer())+".mhd";
91 
92 
93  writer->SetInputData(input);
94  writer->SetFileName(cstring_cast(filename));
95  writer->SetCompression(false);
96  writer->Write();
97 
98  // read from disk
99  typedef itk::ImageFileReader<itkImageType> ReaderType;
100  ReaderType::Pointer reader = ReaderType::New();
101 #if ITK_VERSION_MAJOR==3
102  reader->SetFileName(cstring_cast(filename));
103 #else
104  reader->SetFileName(string_cast(filename));
105 #endif
106 
108 // reader->SetFileName(string_cast(filename));
109  reader->Update();
110  itkImageType::ConstPointer retval = reader->GetOutput();
111 
112  QFile(filename).remove(); // cleanup
113  QFile(changeExtension(filename, "raw")).remove(); // cleanup
114 
115  return retval;
116 }
117 //---------------------------------------------------------------------------------------------------------------------
118 
119 vtkImageDataPtr AlgorithmHelper::getVTKFromITK(itkImageType::ConstPointer input)
120 {
121  //Convert ITK to VTK
122  itkToVtkFilterType::Pointer itkToVtkFilter = itkToVtkFilterType::New();
123  itkToVtkFilter->SetInput(input);
124  itkToVtkFilter->Update();
125 
126  vtkImageDataPtr rawResult = vtkImageDataPtr::New();
127  rawResult->DeepCopy(itkToVtkFilter->GetOutput());
128  // TODO: possible memory problem here - check debug mem system of itk/vtk
129 
130  return rawResult;
131 }
132 
134 {
135  itkImageType::ConstPointer itkImage = AlgorithmHelper::getITKfromVTKImage(input);
136 
137  typedef itk::GrayscaleFillholeImageFilter<itkImageType, itkImageType> FilterType;
138  FilterType::Pointer filter = FilterType::New();
139 
140  filter->SetInput(itkImage);
141  filter->Update();
142 
143  return AlgorithmHelper::getVTKFromITK(filter->GetOutput());
144 }
145 
146 
147 }
QString qstring_cast(const T &val)
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:48
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:91
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