NorMIT-nav  2023.01.05-dev+develop.0da12
An IGT application
cxBinaryThresholdImageFilter.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 
13 
14 #include "cxAlgorithmHelpers.h"
15 #include <itkBinaryThresholdImageFilter.h>
16 #include <vtkImageCast.h>
17 #include "cxUtilHelpers.h"
19 #include "cxStringProperty.h"
20 #include "cxColorProperty.h"
21 #include "cxBoolProperty.h"
22 #include "cxTypeConversions.h"
23 #include "cxDoublePairProperty.h"
24 #include "cxContourFilter.h"
25 #include "cxMesh.h"
26 #include "cxImage.h"
28 #include "cxPatientModelService.h"
29 #include "cxViewService.h"
30 #include "cxVolumeHelpers.h"
31 #include "cxVisServices.h"
32 
33 namespace cx
34 {
35 
37  FilterImpl(services)
38 {
39 }
40 
42 {
43  return "Segmentation";
44 }
45 
47 {
48  return "binary_threshold_image_filter";
49 }
50 
52 {
53  return "<html>"
54  "<h3>Binary Threshold Image Filter.</h3>"
55  "<p><i>Segment out areas from the selected image using a threshold.</i></p>"
56  "<p>This filter produces an output image whose pixels are either one of two values"
57  "( OutsideValue or InsideValue ), depending on whether the corresponding input"
58  "image pixels lie between the two thresholds ( LowerThreshold and UpperThreshold )."
59  "Values equal to either threshold is considered to be between the thresholds.<p>"
60  "</html>";
61 }
62 
64 {
65  DoublePairPropertyPtr retval = DoublePairProperty::initialize("Thresholds", "",
66  "Select the lower and upper thresholds for the segmentation", DoubleRange(0, 100, 1), 0,
67  root);
68  return retval;
69 }
70 
72 {
73  BoolPropertyPtr retval = BoolProperty::initialize("Generate Surface", "",
74  "Generate a surface of the output volume", true,
75  root);
76  return retval;
77 }
78 
80 {
81  return ColorProperty::initialize("Color", "",
82  "The color of the output model.",
83  QColor("green"), root);
84 }
85 
87 {
88  mThresholdOption = this->getThresholdOption(mOptions);
89  connect(mThresholdOption.get(), &Property::changed, this, &BinaryThresholdImageFilter::thresholdSlot);
90  mOptionsAdapters.push_back(mThresholdOption);
92  mOptionsAdapters.push_back(this->getColorOption(mOptions));
93 }
94 
96 {
98 
99  temp = StringPropertySelectImage::New(mServices->patient());
100  temp->setValueName("Input");
101  temp->setHelp("Select image input for thresholding");
102  connect(temp.get(), SIGNAL(dataChanged(QString)), this, SLOT(imageChangedSlot(QString)));
103  mInputTypes.push_back(temp);
104 }
105 
107 {
109 
110  temp = StringPropertySelectData::New(mServices->patient());
111  temp->setValueName("Output");
112  temp->setHelp("Output thresholded binary image");
113  mOutputTypes.push_back(temp);
114 
115  temp = StringPropertySelectData::New(mServices->patient());
116  temp->setValueName("Contour");
117  temp->setHelp("Output contour generated from thresholded binary image.");
118  mOutputTypes.push_back(temp);
119 }
120 
122 {
124 
125  if (!mActive)
126  this->stopPreview();
127 }
128 
129 void BinaryThresholdImageFilter::imageChangedSlot(QString uid)
130 {
131  this->stopPreview();
132  this->updateThresholdPairFromImageChange(uid, mThresholdOption);
133 }
134 
135 void BinaryThresholdImageFilter::stopPreview()
136 {
137  if(mPreviewImage)
138  mPreviewImage->stopThresholdPreview();
139  mPreviewImage.reset();
140 }
141 
143 {
144  if (mActive)
145  {
146  mPreviewImage = boost::dynamic_pointer_cast<Image>(mInputTypes[0]->getData());
147  if(!mPreviewImage)
148  return;
149  Eigen::Vector2d threshold = Eigen::Vector2d(mThresholdOption->getValue()[0], mThresholdOption->getValue()[1]);
150  mPreviewImage->startThresholdPreview(threshold);
151  }
152 }
153 
155 {
156  this->stopPreview();
157  return FilterImpl::preProcess();
158 
159 }
160 
162 {
163  ImagePtr input = this->getCopiedInputImage();
164  if (!input)
165  return false;
166 
169 
170  itkImageType::ConstPointer itkImage = AlgorithmHelper::getITKfromSSCImage(input);
171 
172  //Binary Thresholding
173  typedef itk::BinaryThresholdImageFilter<itkImageType, itkImageType> thresholdFilterType;
174  thresholdFilterType::Pointer thresholdFilter = thresholdFilterType::New();
175  thresholdFilter->SetInput(itkImage);
176  thresholdFilter->SetOutsideValue(0);
177  thresholdFilter->SetInsideValue(1);
178  thresholdFilter->SetLowerThreshold(thresholds->getValue()[0]);
179  thresholdFilter->SetUpperThreshold(thresholds->getValue()[1]);
180  thresholdFilter->Update();
181  itkImage = thresholdFilter->GetOutput();
182 
183  //Convert ITK to VTK
184  itkToVtkFilterType::Pointer itkToVtkFilter = itkToVtkFilterType::New();
185  itkToVtkFilter->SetInput(itkImage);
186  itkToVtkFilter->Update();
187 
188  vtkImageDataPtr rawResult = vtkImageDataPtr::New();
189  rawResult->DeepCopy(itkToVtkFilter->GetOutput());
190 
191  vtkImageCastPtr imageCast = vtkImageCastPtr::New();
192  imageCast->SetInputData(rawResult);
193  imageCast->SetOutputScalarTypeToUnsignedChar();
194  imageCast->Update();
195  rawResult = imageCast->GetOutput();
196 
197  // TODO: possible memory problem here - check debug mem system of itk/vtk
198 
199  mRawResult = rawResult;
200 
201  if (generateSurface->getValue())
202  {
203  double threshold = 1;
204  mRawContour = ContourFilter::execute(mRawResult, threshold);
205  }
206 
207  return true;
208 }
209 
211 {
212  if (!mRawResult)
213  return false;
214 
215  ImagePtr input = this->getCopiedInputImage();
216 
217  if (!input)
218  return false;
219 
220  QString uid = input->getUid() + "_seg%1";
221  QString name = input->getName()+" seg%1";
222  ImagePtr output = createDerivedImage(mServices->patient(),
223  uid, name,
224  mRawResult, input);
225 
226  mRawResult = NULL;
227 
228  output->setInitialWindowLevel(-1, -1);
229  output->resetTransferFunctions();
230  mServices->patient()->insertData(output);
231 
232  // set output
233  mOutputTypes.front()->setValue(output->getUid());
234 
235  // set contour output
236  if (mRawContour!=NULL)
237  {
238  ColorPropertyPtr colorOption = this->getColorOption(mOptions);
239  MeshPtr contour = ContourFilter::postProcess(mServices->patient(), mRawContour, output, colorOption->getValue());
240  mOutputTypes[1]->setValue(contour->getUid());
241  mRawContour = vtkPolyDataPtr();
242  }
243 
244  return true;
245 }
246 
247 
248 }//namespace cx
cx::FilterImpl
Definition: cxFilterImpl.h:36
cx::BoolProperty::initialize
static BoolPropertyPtr initialize(const QString &uid, QString name, QString help, bool value, QDomNode root=QDomNode())
Definition: cxBoolProperty.cpp:30
cxVolumeHelpers.h
cx::ColorPropertyPtr
boost::shared_ptr< class ColorProperty > ColorPropertyPtr
Definition: cxForwardDeclarations.h:144
cx::BinaryThresholdImageFilter::preProcess
bool preProcess()
Definition: cxBinaryThresholdImageFilter.cpp:154
cxContourFilter.h
cxAlgorithmHelpers.h
cx::StringPropertySelectData::New
static StringPropertySelectDataPtr New(PatientModelServicePtr patientModelService, QString typeRegexp=".*")
Definition: cxSelectDataStringProperty.h:101
cx::BinaryThresholdImageFilter::postProcess
virtual bool postProcess()
Definition: cxBinaryThresholdImageFilter.cpp:210
cx::FilterImpl::setActive
virtual void setActive(bool on)
Definition: cxFilterImpl.cpp:79
cx::BinaryThresholdImageFilter::createOptions
virtual void createOptions()
Definition: cxBinaryThresholdImageFilter.cpp:86
cx::AlgorithmHelper::getITKfromSSCImage
static itkImageType::ConstPointer getITKfromSSCImage(ImagePtr image)
Definition: cxAlgorithmHelpers.cpp:35
cxDoublePairProperty.h
cx
Namespace for all CustusX production code.
Definition: cx_dev_group_definitions.h:13
cx::DoublePairProperty::initialize
static DoublePairPropertyPtr initialize(const QString &uid, QString name, QString help, DoubleRange range, int decimals, QDomNode root=QDomNode())
Definition: cxDoublePairProperty.cpp:22
cxImage.h
cx::Property::changed
void changed()
emit when the underlying data value is changed: The user interface will be updated.
cx::BoolPropertyPtr
boost::shared_ptr< class BoolProperty > BoolPropertyPtr
Definition: cxPlusConnectWidget.h:29
cx::BinaryThresholdImageFilter::getName
virtual QString getName() const
Definition: cxBinaryThresholdImageFilter.cpp:41
cx::BinaryThresholdImageFilter::execute
virtual bool execute()
Definition: cxBinaryThresholdImageFilter.cpp:161
cx::VisServicesPtr
boost::shared_ptr< class VisServices > VisServicesPtr
Definition: cxMainWindow.h:40
cx::ContourFilter::execute
virtual bool execute()
Definition: cxContourFilter.cpp:218
cx::BinaryThresholdImageFilter::getThresholdOption
DoublePairPropertyPtr getThresholdOption(QDomElement root)
Definition: cxBinaryThresholdImageFilter.cpp:63
cxBinaryThresholdImageFilter.h
vtkImageDataPtr
vtkSmartPointer< class vtkImageData > vtkImageDataPtr
Definition: cxVideoConnectionWidget.h:30
cxUtilHelpers.h
cx::FilterImpl::mOutputTypes
std::vector< SelectDataStringPropertyBasePtr > mOutputTypes
Definition: cxFilterImpl.h:74
cx::BinaryThresholdImageFilter::createInputTypes
virtual void createInputTypes()
Definition: cxBinaryThresholdImageFilter.cpp:95
cx::ColorProperty::initialize
static ColorPropertyPtr initialize(const QString &uid, QString name, QString help, QColor value, QDomNode root=QDomNode())
Definition: cxColorProperty.cpp:24
cx::FilterImpl::mInputTypes
std::vector< SelectDataStringPropertyBasePtr > mInputTypes
Definition: cxFilterImpl.h:73
cx::FilterImpl::mOptions
QDomElement mOptions
Definition: cxFilterImpl.h:76
cx::FilterImpl::mCopiedOptions
QDomElement mCopiedOptions
Definition: cxFilterImpl.h:80
cx::FilterImpl::getCopiedInputImage
ImagePtr getCopiedInputImage(int index=0)
Definition: cxFilterImpl.cpp:104
cx::BinaryThresholdImageFilter::mPreviewImage
ImagePtr mPreviewImage
Definition: cxBinaryThresholdImageFilter.h:73
cx::MeshPtr
boost::shared_ptr< class Mesh > MeshPtr
Definition: cxForwardDeclarations.h:48
cx::BinaryThresholdImageFilter::BinaryThresholdImageFilter
BinaryThresholdImageFilter(VisServicesPtr services)
Definition: cxBinaryThresholdImageFilter.cpp:36
cx::BinaryThresholdImageFilter::setActive
virtual void setActive(bool on)
Definition: cxBinaryThresholdImageFilter.cpp:121
cx::StringPropertySelectImage::New
static StringPropertySelectImagePtr New(PatientModelServicePtr patientModelService)
Definition: cxSelectDataStringProperty.h:76
cxViewService.h
cxTypeConversions.h
cx::DoublePairPropertyPtr
boost::shared_ptr< class DoublePairProperty > DoublePairPropertyPtr
Definition: cxForwardDeclarations.h:145
cx::BinaryThresholdImageFilter::getType
virtual QString getType() const
Definition: cxBinaryThresholdImageFilter.cpp:46
cx::BinaryThresholdImageFilter::getHelp
virtual QString getHelp() const
Definition: cxBinaryThresholdImageFilter.cpp:51
cxPatientModelService.h
cx::ImagePtr
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:27
cx::FilterImpl::updateThresholdPairFromImageChange
void updateThresholdPairFromImageChange(QString uid, DoublePairPropertyPtr threshold)
Definition: cxFilterImpl.cpp:129
cx::BinaryThresholdImageFilter::getColorOption
ColorPropertyPtr getColorOption(QDomElement root)
Definition: cxBinaryThresholdImageFilter.cpp:79
cxRegistrationTransform.h
cx::vtkPolyDataPtr
vtkSmartPointer< vtkPolyData > vtkPolyDataPtr
Definition: cxCenterlineRegistration.h:42
cx::BinaryThresholdImageFilter::getGenerateSurfaceOption
BoolPropertyPtr getGenerateSurfaceOption(QDomElement root)
Definition: cxBinaryThresholdImageFilter.cpp:71
cx::createDerivedImage
ImagePtr createDerivedImage(PatientModelServicePtr dataManager, QString uid, QString name, vtkImageDataPtr raw, ImagePtr parent)
Definition: cxVolumeHelpers.cpp:151
cx::ContourFilter::postProcess
virtual bool postProcess()
Definition: cxContourFilter.cpp:327
itk::ImageToVTKImageFilter::Pointer
SmartPointer< Self > Pointer
Definition: itkImageToVTKImageFilter.h:47
cxStringProperty.h
cxSelectDataStringProperty.h
cx::FilterImpl::preProcess
virtual bool preProcess()
Definition: cxFilterImpl.cpp:85
cx::DoubleRange
Utility class for describing a bounded numeric range.
Definition: cxDoubleRange.h:32
cxBoolProperty.h
vtkImageCastPtr
vtkSmartPointer< class vtkImageCast > vtkImageCastPtr
Definition: vtkForwardDeclarations.h:68
cxMesh.h
cxColorProperty.h
cx::FilterImpl::mOptionsAdapters
std::vector< PropertyPtr > mOptionsAdapters
Definition: cxFilterImpl.h:75
cx::SelectDataStringPropertyBasePtr
boost::shared_ptr< class SelectDataStringPropertyBase > SelectDataStringPropertyBasePtr
Definition: cxMeshGlyphsWidget.h:26
cx::BinaryThresholdImageFilter::createOutputTypes
virtual void createOutputTypes()
Definition: cxBinaryThresholdImageFilter.cpp:106
cx::FilterImpl::mActive
bool mActive
Definition: cxFilterImpl.h:81
cx::FilterImpl::mServices
VisServicesPtr mServices
Definition: cxFilterImpl.h:82
cx::BinaryThresholdImageFilter::thresholdSlot
void thresholdSlot()
Definition: cxBinaryThresholdImageFilter.cpp:142
cxVisServices.h