CustusX  16.5
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxConnectedThresholdImageFilter.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 
34 
35 #include "itkConnectedThresholdImageFilter.h"
36 #include "cxLogger.h"
37 #include "cxTypeConversions.h"
38 #include "cxAlgorithmHelpers.h"
40 #include "cxImage.h"
41 #include "cxPatientModelService.h"
42 #include "cxVolumeHelpers.h"
43 #include "cxVisServices.h"
44 
45 namespace cx
46 {
47 
49  ThreadedTimedAlgorithm<vtkImageDataPtr>("segmenting", 10),
50  mServices(services)
51 {
52 }
53 
55 {
56 }
57 
58 void ConnectedThresholdImageFilter::setInput(ImagePtr image, QString outputBasePath, float lowerThreshold, float upperThreshold, int replaceValue, itkImageType::IndexType seed)
59 {
60  mInput = image;
61  mOutputBasePath = outputBasePath;
62 
63  mLowerThreshold = lowerThreshold;
64  mUpperTheshold = upperThreshold;
65  mReplaceValue = replaceValue;
66  mSeed = seed;
67 
68  this->generate();
69 }
70 
72 {
73  return mOutput;
74 }
75 
76 void ConnectedThresholdImageFilter::postProcessingSlot()
77 {
78  //get the result from the thread
79  vtkImageDataPtr rawResult = this->getResult();
80 
81  if(!rawResult)
82  {
83  reportError("Segmentation failed.");
84  return;
85  }
86 
87  //generate a new name and unique id for the newly created object
88  QString uid = mInput->getUid() + "_seg%1";
89  QString name = mInput->getName()+" seg%1";
90 
91  //create a Image
92  ImagePtr result = createDerivedImage(mServices->patient(),
93  uid, name,
94  rawResult, mInput);
95  mOutput->resetTransferFunctions();
96 
97  mServices->patient()->insertData(mOutput);
98 
99  //let the user know you are finished
100  reportSuccess("Done segmenting: \"" + mOutput->getName()+"\"");
101 
102  //let the system know you're finished
103  // emit finished();
104 }
105 
106 vtkImageDataPtr ConnectedThresholdImageFilter::calculate()
107 {
108  //Connected Thresholding
109 
110  //Documentation:
111  // http://www.na-mic.org/svn/Slicer3-lib-mirrors/trunk/Insight/Examples/Segmentation/ConnectedThresholdImageFilter.cxx
112 
113  // The ConnectedThresholdImageFilter has two main parameters to be
114  // defined. They are the lower and upper thresholds of the interval in
115  // which intensity values should fall in order to be included in the
116  // region. Setting these two values too close will not allow enough
117  // flexibility for the region to grow. Setting them too far apart will
118  // result in a region that engulfs the image.
119 
120  // The output of this filter is a binary image with zero-value pixels
121  // everywhere except on the extracted region. The intensity value set
122  // inside the region is selected with the method SetReplaceValue()
123 
124  // The initialization of the algorithm requires the user to provide a seed
125  // point. It is convenient to select this point to be placed in a
126  // typical region of the anatomical structure to be segmented. The
127  // seed is passed in the form of a IndexType to the SetSeed()
128  // method.
129 
130  // Another option for segmenting regions is to take advantage of the
131  // functionality provided by the ConnectedThresholdImageFilter for
132  // managing multiple seeds. The seeds can be passed one by one to the
133  // filter using the AddSeed() method. You could imagine a user
134  // interface in which an operator clicks on multiple points of the object
135  // to be segmented and each selected point is passed as a seed to this
136  // filter.
137 
138  itkImageType::ConstPointer itkImage = AlgorithmHelper::getITKfromSSCImage(mInput);
139 
140  typedef itk::ConnectedThresholdImageFilter<itkImageType, itkImageType> thresholdFilterType;
141  thresholdFilterType::Pointer thresholdFilter = thresholdFilterType::New();
142  thresholdFilter->SetInput(itkImage);
143 
144  //set thresholds
145  thresholdFilter->SetLower(mLowerThreshold);
146  thresholdFilter->SetUpper(mUpperTheshold);
147  thresholdFilter->SetReplaceValue(mReplaceValue);
148 
149  //set seeds
150  thresholdFilter->SetSeed(mSeed);
151 
152  //calculate
153  try
154  {
155  thresholdFilter->Update();
156  }
157  catch( itk::ExceptionObject & excep )
158  {
159  reportError("Error when setting seed for Connected Threshold Image Filter:");
160  reportError(qstring_cast(excep.GetDescription()));
161  }
162 
163  itkImage = thresholdFilter->GetOutput();
164 
165  //Convert ITK to VTK
166  itkToVtkFilterType::Pointer itkToVtkFilter = itkToVtkFilterType::New();
167  itkToVtkFilter->SetInput(itkImage);
168  itkToVtkFilter->Update();
169 
170  vtkImageDataPtr rawResult = vtkImageDataPtr::New();
171  rawResult->DeepCopy(itkToVtkFilter->GetOutput());
172  // TODO: possible memory problem here - check debug mem system of itk/vtk
173 
174  return rawResult;
175 }
176 
177 }
QString qstring_cast(const T &val)
void reportError(QString msg)
Definition: cxLogger.cpp:92
Base class for algorithms that wants to thread and time their execution. T is the return type of the ...
boost::shared_ptr< class VisServices > VisServicesPtr
Definition: cxMainWindow.h:62
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:48
ImagePtr createDerivedImage(PatientModelServicePtr dataManager, QString uid, QString name, vtkImageDataPtr raw, ImagePtr parent)
void reportSuccess(QString msg)
Definition: cxLogger.cpp:93
static itkImageType::ConstPointer getITKfromSSCImage(ImagePtr image)
vtkSmartPointer< class vtkImageData > vtkImageDataPtr
void setInput(ImagePtr image, QString outputBasePath, float lowerThreshold, float upperThreshold, int replaceValue, itkImageType::IndexType seed)