Fraxinus  16.5.0-fx-rc9
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxDilationFilter.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 
33 #include "cxDilationFilter.h"
34 
35 #include "cxDoubleProperty.h"
36 #include "cxColorProperty.h"
37 #include "cxBoolProperty.h"
38 #include "cxStringProperty.h"
40 
41 #include <itkBinaryDilateImageFilter.h>
42 #include <itkBinaryBallStructuringElement.h>
43 #include "cxAlgorithmHelpers.h"
44 #include <vtkImageCast.h>
45 #include "cxUtilHelpers.h"
46 #include "cxContourFilter.h"
47 #include "cxMesh.h"
48 #include "cxImage.h"
49 #include "cxPatientModelService.h"
50 #include "cxVolumeHelpers.h"
51 #include "cxVisServices.h"
52 
53 
54 namespace cx {
55 
57  FilterImpl(services)
58 {
59 }
60 
61 QString DilationFilter::getName() const
62 {
63  return "Dilation";
64 }
65 
66 QString DilationFilter::getType() const
67 {
68  return "DilationFilter";
69 }
70 
71 QString DilationFilter::getHelp() const
72 {
73  return "<html>"
74  "<h3>Dilation Filter.</h3>"
75  "<p>This filter dilates a binary volume with a given radius in mm.<p>"
76  "<p>The dilation is performed using a ball structuring element<p>"
77  "</html>";
78 }
79 
81 {
82  DoublePropertyPtr retval = DoubleProperty::initialize("Dilation radius (mm)", "",
83  "Set dilation radius in mm", 1, DoubleRange(1, 20, 1), 0,
84  root);
85  retval->setGuiRepresentation(DoublePropertyBase::grSLIDER);
86  return retval;
87 }
88 
90 {
91  BoolPropertyPtr retval = BoolProperty::initialize("Generate Surface", "",
92  "Generate a surface of the output volume", true,
93  root);
94  return retval;
95 }
96 
98 {
99  return ColorProperty::initialize("Color", "",
100  "Color of output model.",
101  QColor("green"), root);
102 }
103 
104 
106 {
109  mOptionsAdapters.push_back(this->getColorOption(mOptions));
110 }
111 
113 {
115 
116  temp = StringPropertySelectImage::New(mServices->patient());
117  temp->setValueName("Input");
118  temp->setHelp("Select segmentation input for dilation");
119  mInputTypes.push_back(temp);
120 }
121 
123 {
125 
126  temp = StringPropertySelectData::New(mServices->patient());
127  temp->setValueName("Output");
128  temp->setHelp("Dilated segmentation image");
129  mOutputTypes.push_back(temp);
130 
131  temp = StringPropertySelectData::New(mServices->patient());
132  temp->setValueName("Contour");
133  temp->setHelp("Output contour generated from dilated segmentation image.");
134  mOutputTypes.push_back(temp);
135 }
136 
138 
139  return FilterImpl::preProcess();
140 }
141 
143  ImagePtr input = this->getCopiedInputImage();
144  if (!input)
145  return false;
146 
147  double radius = this->getDilationRadiusOption(mCopiedOptions)->getValue();
148 
149  // Convert radius in mm to radius in voxels for the structuring element
150  Eigen::Array3d spacing = input->getSpacing();
151  itk::Size<3> radiusInVoxels;
152  radiusInVoxels[0] = radius/spacing(0);
153  radiusInVoxels[1] = radius/spacing(1);
154  radiusInVoxels[2] = radius/spacing(2);
155 
156  itkImageType::ConstPointer itkImage = AlgorithmHelper::getITKfromSSCImage(input);
157 
158  // Create structuring element
159  typedef itk::BinaryBallStructuringElement<unsigned char,3> StructuringElementType;
160  StructuringElementType structuringElement;
161  structuringElement.SetRadius(radiusInVoxels);
162  structuringElement.CreateStructuringElement();
163 
164  // Dilation
165  typedef itk::BinaryDilateImageFilter<itkImageType, itkImageType, StructuringElementType> dilateFilterType;
166  dilateFilterType::Pointer dilationFilter = dilateFilterType::New();
167  dilationFilter->SetInput(itkImage);
168  dilationFilter->SetKernel(structuringElement);
169  dilationFilter->SetDilateValue(1);
170  dilationFilter->Update();
171  itkImage = dilationFilter->GetOutput();
172 
173  //Convert ITK to VTK
174  itkToVtkFilterType::Pointer itkToVtkFilter = itkToVtkFilterType::New();
175  itkToVtkFilter->SetInput(itkImage);
176  itkToVtkFilter->Update();
177 
178  vtkImageDataPtr rawResult = vtkImageDataPtr::New();
179  rawResult->DeepCopy(itkToVtkFilter->GetOutput());
180 
181  vtkImageCastPtr imageCast = vtkImageCastPtr::New();
182  imageCast->SetInputData(rawResult);
183  imageCast->SetOutputScalarTypeToUnsignedChar();
184  imageCast->Update();
185  rawResult = imageCast->GetOutput();
186 
187  // TODO: possible memory problem here - check debug mem system of itk/vtk
188 
189  mRawResult = rawResult;
190 
192  if (generateSurface->getValue())
193  {
194  double threshold = 1;
195  mRawContour = ContourFilter::execute(mRawResult, threshold);
196  }
197 
198  return true;
199 }
200 
202 {
203  if (!mRawResult)
204  return false;
205 
206  ImagePtr input = this->getCopiedInputImage();
207 
208  if (!input)
209  return false;
210 
211  QString uid = input->getUid() + "_seg%1";
212  QString name = input->getName()+" seg%1";
213  ImagePtr output = createDerivedImage(mServices->patient(),
214  uid, name,
215  mRawResult, input);
216  mRawResult = NULL;
217  if (!output)
218  return false;
219 
220  output->resetTransferFunctions();
221  mServices->patient()->insertData(output);
222 
223  // set output
224  mOutputTypes.front()->setValue(output->getUid());
225 
226  // set contour output
227  if (mRawContour!=NULL)
228  {
229  ColorPropertyPtr colorOption = this->getColorOption(mOptions);
230  MeshPtr contour = ContourFilter::postProcess(mServices->patient(), mRawContour, output, colorOption->getValue());
231  mOutputTypes[1]->setValue(contour->getUid());
232  mRawContour = vtkPolyDataPtr();
233  }
234 
235  return true;
236 }
237 
238 
239 
240 } // namespace cx
DilationFilter(VisServicesPtr services)
static BoolPropertyPtr initialize(const QString &uid, QString name, QString help, bool value, QDomNode root=QDomNode())
std::vector< SelectDataStringPropertyBasePtr > mInputTypes
Definition: cxFilterImpl.h:94
QDomElement mCopiedOptions
Definition: cxFilterImpl.h:101
virtual QString getName() const
virtual void createOutputTypes()
DoublePropertyPtr getDilationRadiusOption(QDomElement root)
virtual void createInputTypes()
boost::shared_ptr< class ColorProperty > ColorPropertyPtr
virtual void createOptions()
boost::shared_ptr< class VisServices > VisServicesPtr
Definition: cxMainWindow.h:62
virtual bool preProcess()
static StringPropertySelectDataPtr New(PatientModelServicePtr patientModelService, QString typeRegexp=".*")
Utility class for describing a bounded numeric range.
Definition: cxDoubleRange.h:53
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:48
virtual bool postProcess()
virtual bool execute()
virtual QString getHelp() const
ColorPropertyPtr getColorOption(QDomElement root)
std::vector< PropertyPtr > mOptionsAdapters
Definition: cxFilterImpl.h:96
vtkSmartPointer< class vtkPolyData > vtkPolyDataPtr
VisServicesPtr mServices
Definition: cxFilterImpl.h:103
ImagePtr createDerivedImage(PatientModelServicePtr dataManager, QString uid, QString name, vtkImageDataPtr raw, ImagePtr parent)
boost::shared_ptr< class SelectDataStringPropertyBase > SelectDataStringPropertyBasePtr
virtual bool execute()
ImagePtr getCopiedInputImage(int index=0)
QDomElement mOptions
Definition: cxFilterImpl.h:97
vtkSmartPointer< class vtkImageCast > vtkImageCastPtr
boost::shared_ptr< class DoubleProperty > DoublePropertyPtr
std::vector< SelectDataStringPropertyBasePtr > mOutputTypes
Definition: cxFilterImpl.h:95
static DoublePropertyPtr initialize(const QString &uid, QString name, QString help, double value, DoubleRange range, int decimals, QDomNode root=QDomNode())
virtual QString getType() const
virtual bool postProcess()
static itkImageType::ConstPointer getITKfromSSCImage(ImagePtr image)
static StringPropertySelectImagePtr New(PatientModelServicePtr patientModelService)
static ColorPropertyPtr initialize(const QString &uid, QString name, QString help, QColor value, QDomNode root=QDomNode())
BoolPropertyPtr getGenerateSurfaceOption(QDomElement root)
boost::shared_ptr< class BoolProperty > BoolPropertyPtr
boost::shared_ptr< class Mesh > MeshPtr
vtkSmartPointer< class vtkImageData > vtkImageDataPtr