fw4spl
SThreshold.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "opImageFilter/SThreshold.hpp"
8 
9 #include <fwCom/Signal.hpp>
10 #include <fwCom/Signal.hxx>
11 
12 #include <fwData/Image.hpp>
13 
14 #include <fwDataTools/helper/Image.hpp>
15 #include <fwDataTools/helper/ImageGetter.hpp>
16 
17 #include <fwMedData/ImageSeries.hpp>
18 
19 #include <fwServices/macros.hpp>
20 
21 #include <fwTools/Dispatcher.hpp>
22 #include <fwTools/DynamicTypeKeyTypeMapping.hpp>
23 #include <fwTools/fwID.hpp>
24 #include <fwTools/IntrinsicTypes.hpp>
25 
26 namespace opImageFilter
27 {
28 
29 //-----------------------------------------------------------------------------
30 
31 static const std::string s_IMAGE_INPUT = "source";
32 static const std::string s_IMAGE_OUTPUT = "target";
33 
34 //-----------------------------------------------------------------------------
35 
36 SThreshold::SThreshold() noexcept :
37  m_threshold(50.0)
38 {
39 }
40 
41 //-----------------------------------------------------------------------------
42 
43 SThreshold::~SThreshold() noexcept
44 {
45 }
46 
47 //-----------------------------------------------------------------------------
48 
50 {
51 }
52 
53 //-----------------------------------------------------------------------------
54 
56 {
57 }
58 
59 //-----------------------------------------------------------------------------
60 
62 {
63  const ::fwServices::IService::ConfigType& srvConfig = this->getConfigTree();
64 
65  SLM_ASSERT("You must have one <config/> element.", srvConfig.count("config") == 1 );
66 
67  const ::fwServices::IService::ConfigType& config = srvConfig.get_child("config");
68 
69  SLM_ASSERT("You must have one <threshold/> element.", config.count("threshold") == 1);
70  const ::fwServices::IService::ConfigType& thresholdCfg = config.get_child("threshold");
71  m_threshold = thresholdCfg.get_value<double>();
72 }
73 
74 //-----------------------------------------------------------------------------
75 
85 {
86  struct Parameter
87  {
88  double thresholdValue;
89  ::fwData::Image::csptr imageIn;
90  ::fwData::Image::sptr imageOut;
91  };
92 
97  template<class PIXELTYPE>
98  void operator()(Parameter& param)
99  {
100  const PIXELTYPE thresholdValue = static_cast<PIXELTYPE>(param.thresholdValue);
101  ::fwData::Image::csptr imageIn = param.imageIn;
102  ::fwData::Image::sptr imageOut = param.imageOut;
103  SLM_ASSERT("Sorry, image must be 3D", imageIn->getNumberOfDimensions() == 3 );
104  imageOut->copyInformation(imageIn); // Copy image size, type... without copying the buffer
105  imageOut->allocate(); // Allocate the image buffer
106 
107  ::fwDataTools::helper::ImageGetter imageInHelper(imageIn); // helper used to access the image source buffer
108  ::fwDataTools::helper::Image imageOutHelper(imageOut); // helper used to access the image target buffer
109 
110  // Get image buffers
111  const PIXELTYPE* buffer1 = (PIXELTYPE*)imageInHelper.getBuffer();
112  PIXELTYPE* buffer2 = (PIXELTYPE*)imageOutHelper.getBuffer();
113 
114  // Get number of pixels
115  const size_t NbPixels = imageIn->getSize()[0] * imageIn->getSize()[1] * imageIn->getSize()[2];
116 
117  // Fill the target buffer considering the thresholding
118  for( size_t i = 0; i < NbPixels; ++i, ++buffer1, ++buffer2 )
119  {
120  * buffer2 = ( *buffer1 < thresholdValue ) ? 0 : std::numeric_limits<PIXELTYPE>::max();
121  }
122  }
123 };
124 
125 //-----------------------------------------------------------------------------
126 
128 {
129  ThresholdFilter::Parameter param; // filter parameters: threshold value, image source, image target
130 
131  ::fwData::Object::csptr input = this->getInput< ::fwData::Object >(s_IMAGE_INPUT);
132  ::fwMedData::ImageSeries::csptr imageSeriesSrc = ::fwMedData::ImageSeries::dynamicConstCast(input);
133  ::fwData::Image::csptr imageSrc = ::fwData::Image::dynamicConstCast(input);
134  ::fwData::Object::sptr output;
135 
136  // Get source/target image
137  if(imageSeriesSrc)
138  {
139  param.imageIn = imageSeriesSrc->getImage();
140  ::fwMedData::ImageSeries::sptr imageSeriesDest = ::fwMedData::ImageSeries::New();
141 
142  ::fwData::Object::DeepCopyCacheType cache;
143  imageSeriesDest->::fwMedData::Series::cachedDeepCopy(imageSeriesSrc, cache);
144  imageSeriesDest->setDicomReference(imageSeriesSrc->getDicomReference());
145 
146  ::fwData::Image::sptr imageOut = ::fwData::Image::New();
147  imageSeriesDest->setImage(imageOut);
148  param.imageOut = imageOut;
149  output = imageSeriesDest;
150  }
151  else if(imageSrc)
152  {
153  param.imageIn = imageSrc;
154  ::fwData::Image::sptr imageOut = ::fwData::Image::New();
155  param.imageOut = imageOut;
156  output = imageOut;
157  }
158  else
159  {
160  FW_RAISE("Wrong type: source type must be an ImageSeries or an Image");
161  }
162 
163  param.thresholdValue = m_threshold;
164 
165  /*
166  * The dispatcher allows to apply the filter on any type of image.
167  * It invokes the template functor ThresholdFilter using the image type.
168  */
169  ::fwTools::DynamicType type = param.imageIn->getPixelType(); // image type
170 
171  // Invoke filter functor
173 
174  this->setOutput(s_IMAGE_OUTPUT, output);
175 }
176 
177 //-----------------------------------------------------------------------------
178 
179 } // namespace opImageFilter
double thresholdValue
threshold value.
Definition: SThreshold.cpp:88
The namespace opImageFilter contains several operators on image.
OPIMAGEFILTER_API void configuring() override
Configure the service.
Definition: SThreshold.cpp:61
FWDATATOOLS_API void * getBuffer() const
Returns image buffer.
Definition: ImageGetter.cpp:41
Class defining an elementary C++ type aka unsigned char, signed char, .... signed long...
Definition: DynamicType.hpp:31
Defines an helper to modify an fwData::Image by adding few medical fields and create in parallel the ...
FWSERVICES_API void setOutput(const ::fwServices::IService::KeyType &key, const ::fwData::Object::sptr &object, size_t index=0)
Register an output object at a given key in the OSR, replacing it if it already exists.
Definition: IService.cpp:80
::fwData::Image::sptr imageOut
image target: contains the result of the filter
Definition: SThreshold.cpp:90
OPIMAGEFILTER_API void stopping() override
Uninitialize the service activity. The stop() method is always invoked before destroying a service...
Definition: SThreshold.cpp:55
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
static void invoke()
Instanciate and invoke all functors.
Definition: Dispatcher.hpp:99
OPIMAGEFILTER_API void starting() override
Initialize the service activity.
Definition: SThreshold.cpp:49
::fwData::Image::csptr imageIn
image source
Definition: SThreshold.cpp:89
Defines an helper to modify an fwData::Image by adding few medical fields and create in parallel the ...
Definition: ImageGetter.hpp:23
OPIMAGEFILTER_API void updating() override
Apply the threshold.
Definition: SThreshold.cpp:127
FWDATATOOLS_API void * getBuffer()
Returns image buffer.
void operator()(Parameter &param)
Applies the filter.
Definition: SThreshold.cpp:98
FWSERVICES_API ConfigType getConfigTree() const
Return the configuration, in an boost property tree.
Definition: IService.cpp:247