1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://www.centreleonberard.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the copyright notices for more information.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
20 #include "clitkCommon.h"
21 #include "clitkBooleanOperatorLabelImageFilter.h"
22 #include "clitkAutoCropFilter.h"
23 #include "clitkResampleImageWithOptionsFilter.h"
24 #include "clitkBooleanOperatorLabelImageFilter.h"
25 #include "clitkCropLikeImageFilter.h"
29 #include <itkStatisticsLabelMapFilter.h>
30 #include <itkLabelImageToStatisticsLabelMapFilter.h>
31 #include <itkRegionOfInterestImageFilter.h>
32 #include <itkBinaryThresholdImageFilter.h>
33 #include <itkBinaryErodeImageFilter.h>
34 #include <itkBinaryBallStructuringElement.h>
35 #include <itkAddImageFilter.h>
36 #if ITK_VERSION_MAJOR >= 4
37 #include <itkDivideImageFilter.h>
39 #include <itkDivideByConstantImageFilter.h>
43 #include "RelativePositionPropImageFilter.h"
45 //--------------------------------------------------------------------
46 template <class ImageType>
47 clitk::AddRelativePositionConstraintToLabelImageFilter<ImageType>::
48 AddRelativePositionConstraintToLabelImageFilter():
50 itk::ImageToImageFilter<ImageType, ImageType>()
52 this->SetNumberOfRequiredInputs(2);
53 SetFuzzyThreshold(0.6);
54 SetBackgroundValue(0);
55 SetObjectBackgroundValue(0);
56 ClearOrientationType();
57 IntermediateSpacingFlagOn();
58 SetIntermediateSpacing(10);
60 InverseOrientationFlagOff();
62 CombineWithOrFlagOff();
65 FuzzyMapOnlyFlagOff();
67 //--------------------------------------------------------------------
70 //--------------------------------------------------------------------
71 template <class ImageType>
73 clitk::AddRelativePositionConstraintToLabelImageFilter<ImageType>::
74 SetInput(const ImageType * image)
76 // Process object is not const-correct so the const casting is required.
77 this->SetNthInput(0, const_cast<ImageType *>(image));
79 //--------------------------------------------------------------------
82 //--------------------------------------------------------------------
83 template <class ImageType>
85 clitk::AddRelativePositionConstraintToLabelImageFilter<ImageType>::
86 SetInputObject(const ImageType * image)
88 // Process object is not const-correct so the const casting is required.
89 this->SetNthInput(1, const_cast<ImageType *>(image));
91 //--------------------------------------------------------------------
94 //--------------------------------------------------------------------
95 template <class ImageType>
97 clitk::AddRelativePositionConstraintToLabelImageFilter<ImageType>::
98 ClearOrientationType()
100 m_OrientationTypeString.clear();
101 m_OrientationType.clear();
105 //--------------------------------------------------------------------
108 //--------------------------------------------------------------------
109 template <class ImageType>
111 clitk::AddRelativePositionConstraintToLabelImageFilter<ImageType>::
114 return m_OrientationType.size();
116 //--------------------------------------------------------------------
119 //--------------------------------------------------------------------
120 template <class ImageType>
122 clitk::AddRelativePositionConstraintToLabelImageFilter<ImageType>::
123 AddOrientationTypeString(std::string t)
125 m_OrientationTypeString.push_back(t);
127 if (t == "LeftTo") { AddOrientationType(LeftTo); return; }
128 if (t == "RightTo") { AddOrientationType(RightTo); return; }
129 if (t == "AntTo") { AddOrientationType(AntTo); return; }
130 if (t == "PostTo") { AddOrientationType(PostTo); return; }
131 if (t == "SupTo") { AddOrientationType(SupTo); return; }
132 if (t == "InfTo") { AddOrientationType(InfTo); return; }
134 if (t == "NotLeftTo") { AddOrientationType(LeftTo); InverseOrientationFlagOn(); return; }
135 if (t == "NotRightTo") { AddOrientationType(RightTo); InverseOrientationFlagOn(); return; }
136 if (t == "NotAntTo") { AddOrientationType(AntTo); InverseOrientationFlagOn(); return; }
137 if (t == "NotPostTo") { AddOrientationType(PostTo); InverseOrientationFlagOn(); return; }
138 if (t == "NotSupTo") { AddOrientationType(SupTo); InverseOrientationFlagOn(); return; }
139 if (t == "NotInfTo") { AddOrientationType(InfTo); InverseOrientationFlagOn(); return; }
141 clitkExceptionMacro("Error, you must provide LeftTo,RightTo or AntTo,PostTo or SupTo,InfTo (or NotLeftTo, NotRightTo etc) but you give " << t);
143 //--------------------------------------------------------------------
146 //--------------------------------------------------------------------
147 template <class ImageType>
149 clitk::AddRelativePositionConstraintToLabelImageFilter<ImageType>::
150 GenerateOutputInformation()
152 ImagePointer input = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(0));
153 ImagePointer outputImage = this->GetOutput(0);
154 outputImage->SetRegions(outputImage->GetLargestPossibleRegion());
156 //--------------------------------------------------------------------
159 //--------------------------------------------------------------------
160 template <class ImageType>
162 clitk::AddRelativePositionConstraintToLabelImageFilter<ImageType>::
163 GenerateInputRequestedRegion()
166 itk::ImageToImageFilter<ImageType, ImageType>::GenerateInputRequestedRegion();
167 // Get input pointers and set requested region to common region
168 ImagePointer input1 = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(0));
169 ImagePointer input2 = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(1));
170 input1->SetRequestedRegion(input1->GetLargestPossibleRegion());
171 input2->SetRequestedRegion(input2->GetLargestPossibleRegion());
173 //--------------------------------------------------------------------
176 //--------------------------------------------------------------------
177 template <class ImageType>
179 clitk::AddRelativePositionConstraintToLabelImageFilter<ImageType>::
180 AddAngles(double a, double b)
182 AddOrientationTypeString("Angle");
183 m_Angle1.push_back(a);
184 m_Angle2.push_back(b);
186 //--------------------------------------------------------------------
189 //--------------------------------------------------------------------
190 template <class ImageType>
192 clitk::AddRelativePositionConstraintToLabelImageFilter<ImageType>::
193 AddOrientationType(OrientationTypeEnumeration orientation)
195 m_OrientationType.push_back(orientation);
196 switch (orientation) {
198 m_Angle1.push_back(clitk::deg2rad(0));
199 m_Angle2.push_back(clitk::deg2rad(0));
202 m_Angle1.push_back(clitk::deg2rad(180));
203 m_Angle2.push_back(clitk::deg2rad(0));
206 m_Angle1.push_back(clitk::deg2rad(90));
207 m_Angle2.push_back(clitk::deg2rad(0));
210 m_Angle1.push_back(clitk::deg2rad(-90));
211 m_Angle2.push_back(clitk::deg2rad(0));
214 m_Angle1.push_back(clitk::deg2rad(0));
215 m_Angle2.push_back(clitk::deg2rad(90));
218 m_Angle1.push_back(clitk::deg2rad(0));
219 m_Angle2.push_back(clitk::deg2rad(-90));
232 //--------------------------------------------------------------------
235 //--------------------------------------------------------------------
236 template <class ImageType>
238 clitk::AddRelativePositionConstraintToLabelImageFilter<ImageType>::
241 DD((int)this->GetBackgroundValue());
242 DD((int)this->GetObjectBackgroundValue());
243 DDV(this->GetOrientationTypeString(), (uint)this->GetNumberOfAngles());
244 DD(this->GetIntermediateSpacingFlag());
245 DD(this->GetIntermediateSpacing());
246 DD(this->GetFuzzyThreshold());
247 DD(this->GetAutoCropFlag());
248 DD(this->GetInverseOrientationFlag());
249 DD(this->GetRemoveObjectFlag());
250 DD(this->GetCombineWithOrFlag());
252 //--------------------------------------------------------------------
255 //--------------------------------------------------------------------
256 template <class ImageType>
258 clitk::AddRelativePositionConstraintToLabelImageFilter<ImageType>::
261 if (GetNumberOfAngles() <1) {
262 clitkExceptionMacro("Add at least one orientation type");
265 if (GetVerboseOptionFlag()) {
266 for(int i=0; i<GetNumberOfAngles(); i++) {
267 std::cout << "Orientation \t:" << GetOrientationTypeString(i) << std::endl;
269 std::cout << "Interm Spacing \t:" << GetIntermediateSpacingFlag() << " " << GetIntermediateSpacing() << "mm" << std::endl;
270 std::cout << "Fuzzy threshold\t:" << GetFuzzyThreshold() << std::endl;
271 std::cout << "AutoCrop \t:" << GetAutoCropFlag() << std::endl;
272 std::cout << "InverseOrient \t:" << GetInverseOrientationFlag() << std::endl;
273 std::cout << "RemoveObject \t:" << GetRemoveObjectFlag() << std::endl;
274 std::cout << "CombineWithOr \t:" << GetCombineWithOrFlag() << std::endl;
278 input = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(0));
279 object = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(1));
280 static const unsigned int dim = ImageType::ImageDimension;
282 // Step 2: object pad to input image -> we want to compute the
283 // relative position for each point belonging to the input image
284 // domain, so we have to extend (pad) the object image to fit the
286 working_image = object;
287 if (!clitk::HaveSameSizeAndSpacing<ImageType, ImageType>(input, working_image)) {
288 StartNewStep("Pad (resize) object to input size");
290 if (0) { // OLD VERSION (TO REMOVE)
291 StartNewStep("Pad object to image size");
292 typename ImageType::Pointer output = ImageType::New();
294 for(unsigned int i=0; i<dim; i++) {
295 size[i] = lrint((input->GetLargestPossibleRegion().GetSize()[i]*
296 input->GetSpacing()[i])/(double)working_image->GetSpacing()[i]);
299 // The index of the input is not necessarily zero, so we have to
300 // take it into account (not done)
302 IndexType index = input->GetLargestPossibleRegion().GetIndex();
303 region.SetSize(size);
304 for(unsigned int i=0; i<dim; i++) {
306 std::cerr << "Index diff from zero : " << index << ". not done yet !" << std::endl;
310 // output->SetLargestPossibleRegion(region);
311 output->SetRegions(region);
312 output->SetSpacing(working_image->GetSpacing());
313 PointType origin = input->GetOrigin();
314 for(unsigned int i=0; i<dim; i++) {
315 origin[i] = index[i]*input->GetSpacing()[i] + input->GetOrigin()[i];
317 output->SetOrigin(origin);
318 // output->SetOrigin(input->GetOrigin());
321 output->FillBuffer(m_BackgroundValue);
322 typename PasteFilterType::Pointer padFilter = PasteFilterType::New();
323 // typename PasteFilterType::InputImageIndexType index;
324 for(unsigned int i=0; i<dim; i++) {
325 index[i] = -index[i]*input->GetSpacing()[i]/(double)working_image->GetSpacing()[i]
326 + lrint((working_image->GetOrigin()[i] - input->GetOrigin()[i])/working_image->GetSpacing()[i]);
328 padFilter->SetSourceImage(working_image);
329 padFilter->SetDestinationImage(output);
330 padFilter->SetDestinationIndex(index);
331 padFilter->SetSourceRegion(working_image->GetLargestPossibleRegion());
333 working_image = padFilter->GetOutput();
336 // Resize object like input
337 working_image = clitk::ResizeImageLike<ImageType>(working_image, input, GetBackgroundValue());
338 StopCurrentStep<ImageType>(working_image);
341 //--------------------------------------------------------------------
342 //--------------------------------------------------------------------
344 if (m_IntermediateSpacingFlag) {
345 StartNewStep("Resample object to intermediate spacing");
346 typedef clitk::ResampleImageWithOptionsFilter<ImageType> ResampleFilterType;
347 typename ResampleFilterType::Pointer resampleFilter = ResampleFilterType::New();
348 resampleFilter->SetInput(working_image);
349 resampleFilter->SetDefaultPixelValue(0);
350 resampleFilter->SetOutputIsoSpacing(m_IntermediateSpacing);
351 resampleFilter->SetGaussianFilteringEnabled(false);
352 // resampleFilter->SetVerboseOptions(true);
353 resampleFilter->Update();
354 working_image = resampleFilter->GetOutput();
355 StopCurrentStep<ImageType>(working_image);
358 // Keep object image (with resampline and pad)
359 object_resampled = working_image;
361 // Step 3: compute rel pos in object
362 StartNewStep("Relative Position Map");
363 typedef itk::RelativePositionPropImageFilter<ImageType, FloatImageType> RelPosFilterType;
364 typename RelPosFilterType::Pointer relPosFilter;
366 for(int i=0; i<GetNumberOfAngles(); i++) {
368 relPosFilter = RelPosFilterType::New();
369 relPosFilter->SetInput(working_image);
370 relPosFilter->SetAlpha1(m_Angle1[i]); // xy plane
371 relPosFilter->SetAlpha2(m_Angle2[i]);
372 relPosFilter->SetK1(M_PI/2.0); // Opening parameter, default = pi/2
373 relPosFilter->SetFast(true);
374 relPosFilter->SetRadius(1); // seems sufficient in this case
375 // relPosFilter->SetVerboseProgress(true);
376 relPosFilter->Update();
377 relPos = relPosFilter->GetOutput();
379 if (GetNumberOfAngles() != 1) {
380 // Creation of the first m_FuzzyMap
382 m_FuzzyMap = clitk::NewImageLike<FloatImageType>(relPos, true);
383 m_FuzzyMap->FillBuffer(0.0);
386 // Add to current fuzzy map
387 typedef itk::AddImageFilter<FloatImageType, FloatImageType, FloatImageType> AddImageFilter;
388 typename AddImageFilter::Pointer addFilter = AddImageFilter::New();
389 addFilter->SetInput1(m_FuzzyMap);
390 addFilter->SetInput2(relPos);
392 m_FuzzyMap = addFilter->GetOutput();
394 else m_FuzzyMap = relPos;
397 // Divide by the number of relpos
398 if (GetNumberOfAngles() != 1) {
399 #if ITK_VERSION_MAJOR >= 4
400 typedef itk::DivideImageFilter<FloatImageType, FloatImageType, FloatImageType> DivideFilter;
401 typename DivideFilter::Pointer divideFilter = DivideFilter::New();
402 divideFilter->SetConstant2(GetNumberOfAngles());
404 typedef itk::DivideByConstantImageFilter<FloatImageType, float, FloatImageType> DivideFilter;
405 typename DivideFilter::Pointer divideFilter = DivideFilter::New();
406 divideFilter->SetConstant(GetNumberOfAngles());
408 divideFilter->SetInput(m_FuzzyMap);
409 divideFilter->Update();
410 m_FuzzyMap = divideFilter->GetOutput();
414 StopCurrentStep<FloatImageType>(relPos);
416 //--------------------------------------------------------------------
417 //--------------------------------------------------------------------
418 StartNewStep("Map Threshold");
420 typedef itk::BinaryThresholdImageFilter<FloatImageType, ImageType> BinaryThresholdImageFilterType;
421 typename BinaryThresholdImageFilterType::Pointer thresholdFilter = BinaryThresholdImageFilterType::New();
422 thresholdFilter->SetInput(relPos);
423 thresholdFilter->SetOutsideValue(m_BackgroundValue);
424 thresholdFilter->SetInsideValue(m_BackgroundValue+1);
425 thresholdFilter->SetLowerThreshold(m_FuzzyThreshold);
426 thresholdFilter->Update();
427 working_image = thresholdFilter->GetOutput();
428 StopCurrentStep<ImageType>(working_image);
430 //--------------------------------------------------------------------
431 //--------------------------------------------------------------------
432 StartNewStep("Post Processing: erosion with initial mask");
433 // Step 2 : erosion with initial mask to exclude pixels that were
434 // inside the resampled version and outside the original mask
435 typedef itk::BinaryBallStructuringElement<unsigned int, ImageDimension> StructuringElementType;
436 StructuringElementType kernel;
438 kernel.CreateStructuringElement();
439 typedef itk::BinaryErodeImageFilter<ImageType, ImageType, StructuringElementType> ErodeFilterType;
440 typename ErodeFilterType::Pointer erodeFilter = ErodeFilterType::New();
441 erodeFilter->SetInput(working_image);
442 erodeFilter->SetKernel(kernel);
443 erodeFilter->SetBackgroundValue(m_BackgroundValue);
444 erodeFilter->SetErodeValue(m_BackgroundValue+1);
445 erodeFilter->Update();
446 working_image = erodeFilter->GetOutput();
447 StopCurrentStep<ImageType>(working_image);
449 //--------------------------------------------------------------------
450 //--------------------------------------------------------------------
451 // Step 5: resample to initial spacing
452 if (m_IntermediateSpacingFlag) {
453 StartNewStep("Resample to come back to the same sampling than input");
454 typedef clitk::ResampleImageWithOptionsFilter<ImageType> ResampleFilterType;
455 typename ResampleFilterType::Pointer resampleFilter = ResampleFilterType::New();
456 resampleFilter->SetDefaultPixelValue(m_BackgroundValue);
457 resampleFilter->SetInput(working_image);
458 resampleFilter->SetOutputSpacing(input->GetSpacing());
459 resampleFilter->SetGaussianFilteringEnabled(false);
460 // resampleFilter->SetVerboseOptions(true);
461 resampleFilter->Update();
462 working_image = resampleFilter->GetOutput();
463 StopCurrentStep<ImageType>(working_image);
466 //--------------------------------------------------------------------
467 //--------------------------------------------------------------------
468 // Pre Step 6: pad if not the same size : it can occur when downsample and upsample
469 //if (!HaveSameSizeAndSpacing(working_image, input)) {
470 if (working_image->GetLargestPossibleRegion() != input->GetLargestPossibleRegion()) {
471 StartNewStep("Pad to get the same size than input");
472 typename ImageType::Pointer temp = ImageType::New();
473 temp->CopyInformation(input);
474 temp->SetRegions(input->GetLargestPossibleRegion()); // Do not forget !!
476 temp->FillBuffer(m_BackgroundValue);
477 typename PasteFilterType::Pointer padFilter2 = PasteFilterType::New();
478 padFilter2->SetSourceImage(working_image);
479 padFilter2->SetDestinationImage(temp);
480 padFilter2->SetDestinationIndex(input->GetLargestPossibleRegion().GetIndex());
481 padFilter2->SetSourceRegion(working_image->GetLargestPossibleRegion());
482 padFilter2->Update();
483 working_image = padFilter2->GetOutput();
484 StopCurrentStep<ImageType>(working_image);
487 //DD("[debug] Rel Pos : no padding after");
490 //--------------------------------------------------------------------
491 //--------------------------------------------------------------------
492 // Step 6: combine input+thresholded relpos
493 StartNewStep("Combine with initial input (boolean And)");
494 typedef clitk::BooleanOperatorLabelImageFilter<ImageType> BoolFilterType;
495 typename BoolFilterType::Pointer combineFilter = BoolFilterType::New();
496 combineFilter->SetBackgroundValue(m_BackgroundValue);
497 combineFilter->SetBackgroundValue1(m_BackgroundValue);
498 combineFilter->SetBackgroundValue2(m_BackgroundValue);
499 combineFilter->SetForegroundValue(m_BackgroundValue+1);
500 combineFilter->SetInput1(input);
501 combineFilter->SetInput2(working_image);
502 if (GetInverseOrientationFlag())
503 combineFilter->SetOperationType(BoolFilterType::AndNot);
505 if (GetCombineWithOrFlag())
506 combineFilter->SetOperationType(BoolFilterType::Or);
508 combineFilter->SetOperationType(BoolFilterType::And);
510 combineFilter->InPlaceOff(); // Do not modify initial input (!)
511 combineFilter->Update();
512 working_image = combineFilter->GetOutput();
514 // Remove (if needed the object from the support)
515 if (GetRemoveObjectFlag()) {
516 combineFilter = BoolFilterType::New();
517 combineFilter->SetInput1(working_image);
518 combineFilter->SetInput2(object);
519 combineFilter->SetOperationType(BoolFilterType::AndNot);
520 combineFilter->InPlaceOn();
521 combineFilter->Update();
522 working_image = combineFilter->GetOutput();
524 // In the other case, we must *add* the initial object to keep it
525 // but not more than the initial support
527 combineFilter = BoolFilterType::New();
528 combineFilter->SetInput1(working_image);
529 combineFilter->SetInput2(object);
530 combineFilter->SetOperationType(BoolFilterType::Or);
531 combineFilter->InPlaceOn();
532 combineFilter->Update();
533 working_image = combineFilter->GetOutput(); // not needed because InPlaceOn ?
534 combineFilter = BoolFilterType::New();
535 combineFilter->SetInput1(working_image);
536 combineFilter->SetInput2(input);
537 combineFilter->SetOperationType(BoolFilterType::And);
538 combineFilter->InPlaceOn();
539 combineFilter->Update();
540 working_image = combineFilter->GetOutput();
543 StopCurrentStep<ImageType>(working_image);
545 //--------------------------------------------------------------------
546 //--------------------------------------------------------------------
548 if (GetAutoCropFlag()) {
549 StartNewStep("Final AutoCrop");
550 typedef clitk::AutoCropFilter<ImageType> CropFilterType;
551 typename CropFilterType::Pointer cropFilter = CropFilterType::New();
552 cropFilter->SetInput(working_image);
553 cropFilter->ReleaseDataFlagOff();
554 cropFilter->Update();
555 working_image = cropFilter->GetOutput();
556 StopCurrentStep<ImageType>(working_image);
559 //--------------------------------------------------------------------
560 //--------------------------------------------------------------------
562 // Final Step -> set output
563 this->SetNthOutput(0, working_image);
564 // this->GraftOutput(working_image);
566 //--------------------------------------------------------------------