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 "clitkSetBackgroundImageFilter.h"
21 #include "clitkSliceBySliceRelativePositionFilter.h"
22 #include "clitkCropLikeImageFilter.h"
23 #include "clitkMemoryUsage.h"
26 #include <itkConnectedComponentImageFilter.h>
27 #include <itkRelabelComponentImageFilter.h>
28 #include <itkBinaryThresholdImageFilter.h>
29 #include <itkPasteImageFilter.h>
30 #include <itkStatisticsLabelMapFilter.h>
31 #include <itkBinaryBallStructuringElement.h>
32 #include <itkBinaryDilateImageFilter.h>
33 #include <itkConstantPadImageFilter.h>
34 #include <itkImageSliceIteratorWithIndex.h>
35 #include <itkBinaryMorphologicalOpeningImageFilter.h>
36 #include <itkImageDuplicator.h>
37 #include <itkSignedMaurerDistanceMapImageFilter.h>
41 //--------------------------------------------------------------------
42 template<class ImageType, class TMaskImageType>
43 typename ImageType::Pointer
44 SetBackground(const ImageType * input,
45 const TMaskImageType * mask,
46 typename TMaskImageType::PixelType maskBG,
47 typename ImageType::PixelType outValue,
49 typedef SetBackgroundImageFilter<ImageType, TMaskImageType, ImageType>
50 SetBackgroundImageFilterType;
51 typename SetBackgroundImageFilterType::Pointer setBackgroundFilter
52 = SetBackgroundImageFilterType::New();
53 // if (inPlace) setBackgroundFilter->ReleaseDataFlagOn(); // No seg fault
54 setBackgroundFilter->SetInPlace(inPlace); // This is important to keep memory low
55 setBackgroundFilter->SetInput(input);
56 setBackgroundFilter->SetInput2(mask);
57 setBackgroundFilter->SetMaskValue(maskBG);
58 setBackgroundFilter->SetOutsideValue(outValue);
59 setBackgroundFilter->Update();
60 return setBackgroundFilter->GetOutput();
62 //--------------------------------------------------------------------
65 //--------------------------------------------------------------------
66 template<class ImageType>
67 int GetNumberOfConnectedComponentLabels(const ImageType * input,
68 typename ImageType::PixelType BG,
69 bool isFullyConnected) {
70 // Connected Component label
71 typedef itk::ConnectedComponentImageFilter<ImageType, ImageType> ConnectFilterType;
72 typename ConnectFilterType::Pointer connectFilter = ConnectFilterType::New();
73 connectFilter->SetInput(input);
74 connectFilter->SetBackgroundValue(BG);
75 connectFilter->SetFullyConnected(isFullyConnected);
76 connectFilter->Update();
79 return connectFilter->GetObjectCount();
81 //--------------------------------------------------------------------
83 //--------------------------------------------------------------------
85 Warning : in this cas, we consider outputType like inputType, not
86 InternalImageType. Be sure it fits.
88 template<class ImageType>
89 typename ImageType::Pointer
90 Labelize(const ImageType * input,
91 typename ImageType::PixelType BG,
92 bool isFullyConnected,
93 int minimalComponentSize) {
94 // InternalImageType for storing large number of component
95 typedef itk::Image<int, ImageType::ImageDimension> InternalImageType;
97 // Connected Component label
98 typedef itk::ConnectedComponentImageFilter<ImageType, InternalImageType> ConnectFilterType;
99 typename ConnectFilterType::Pointer connectFilter = ConnectFilterType::New();
100 // connectFilter->ReleaseDataFlagOn();
101 connectFilter->SetInput(input);
102 connectFilter->SetBackgroundValue(BG);
103 connectFilter->SetFullyConnected(isFullyConnected);
105 // Sort by size and remove too small area.
106 typedef itk::RelabelComponentImageFilter<InternalImageType, ImageType> RelabelFilterType;
107 typename RelabelFilterType::Pointer relabelFilter = RelabelFilterType::New();
108 // relabelFilter->ReleaseDataFlagOn(); // if yes, fail when ExplosionControlledThresholdConnectedImageFilter ???
109 relabelFilter->SetInput(connectFilter->GetOutput());
110 relabelFilter->SetMinimumObjectSize(minimalComponentSize);
111 relabelFilter->Update();
114 typename ImageType::Pointer output = relabelFilter->GetOutput();
117 //--------------------------------------------------------------------
120 //--------------------------------------------------------------------
122 Warning : in this cas, we consider outputType like inputType, not
123 InternalImageType. Be sure it fits.
125 template<class ImageType>
126 typename ImageType::Pointer
127 LabelizeAndCountNumberOfObjects(const ImageType * input,
128 typename ImageType::PixelType BG,
129 bool isFullyConnected,
130 int minimalComponentSize,
132 // InternalImageType for storing large number of component
133 typedef itk::Image<int, ImageType::ImageDimension> InternalImageType;
135 // Connected Component label
136 typedef itk::ConnectedComponentImageFilter<ImageType, InternalImageType> ConnectFilterType;
137 typename ConnectFilterType::Pointer connectFilter = ConnectFilterType::New();
138 // connectFilter->ReleaseDataFlagOn();
139 connectFilter->SetInput(input);
140 connectFilter->SetBackgroundValue(BG);
141 connectFilter->SetFullyConnected(isFullyConnected);
143 // Sort by size and remove too small area.
144 typedef itk::RelabelComponentImageFilter<InternalImageType, ImageType> RelabelFilterType;
145 typename RelabelFilterType::Pointer relabelFilter = RelabelFilterType::New();
146 // relabelFilter->ReleaseDataFlagOn(); // if yes, fail when ExplosionControlledThresholdConnectedImageFilter ???
147 relabelFilter->SetInput(connectFilter->GetOutput());
148 relabelFilter->SetMinimumObjectSize(minimalComponentSize);
149 relabelFilter->Update();
151 nb = relabelFilter->GetNumberOfObjects();
152 // DD(relabelFilter->GetOriginalNumberOfObjects());
153 // DD(relabelFilter->GetSizeOfObjectsInPhysicalUnits()[0]);
156 typename ImageType::Pointer output = relabelFilter->GetOutput();
159 //--------------------------------------------------------------------
163 //--------------------------------------------------------------------
164 template<class ImageType>
165 typename ImageType::Pointer
166 RemoveLabels(const ImageType * input,
167 typename ImageType::PixelType BG,
168 std::vector<typename ImageType::PixelType> & labelsToRemove) {
169 assert(labelsToRemove.size() != 0);
170 typename ImageType::Pointer working_image;// = input;
171 for (unsigned int i=0; i <labelsToRemove.size(); i++) {
172 typedef SetBackgroundImageFilter<ImageType, ImageType> SetBackgroundImageFilterType;
173 typename SetBackgroundImageFilterType::Pointer setBackgroundFilter = SetBackgroundImageFilterType::New();
174 setBackgroundFilter->SetInput(input);
175 setBackgroundFilter->SetInput2(input);
176 setBackgroundFilter->SetMaskValue(labelsToRemove[i]);
177 setBackgroundFilter->SetOutsideValue(BG);
178 setBackgroundFilter->Update();
179 working_image = setBackgroundFilter->GetOutput();
181 return working_image;
183 //--------------------------------------------------------------------
186 //--------------------------------------------------------------------
187 template<class ImageType>
188 typename ImageType::Pointer
189 KeepLabels(const ImageType * input,
190 typename ImageType::PixelType BG,
191 typename ImageType::PixelType FG,
192 typename ImageType::PixelType firstKeep,
193 typename ImageType::PixelType lastKeep,
195 typedef itk::BinaryThresholdImageFilter<ImageType, ImageType> BinarizeFilterType;
196 typename BinarizeFilterType::Pointer binarizeFilter = BinarizeFilterType::New();
197 binarizeFilter->SetInput(input);
198 binarizeFilter->SetLowerThreshold(firstKeep);
199 if (useLastKeep) binarizeFilter->SetUpperThreshold(lastKeep);
200 binarizeFilter->SetInsideValue(FG);
201 binarizeFilter->SetOutsideValue(BG);
202 binarizeFilter->Update();
203 return binarizeFilter->GetOutput();
205 //--------------------------------------------------------------------
208 //--------------------------------------------------------------------
209 template<class ImageType>
210 typename ImageType::Pointer
211 LabelizeAndSelectLabels(const ImageType * input,
212 typename ImageType::PixelType BG,
213 typename ImageType::PixelType FG,
214 bool isFullyConnected,
215 int minimalComponentSize,
216 LabelizeParameters<typename ImageType::PixelType> * param)
218 typename ImageType::Pointer working_image;
219 working_image = Labelize<ImageType>(input, BG, isFullyConnected, minimalComponentSize);
220 if (param->GetLabelsToRemove().size() != 0)
221 working_image = RemoveLabels<ImageType>(working_image, BG, param->GetLabelsToRemove());
222 working_image = KeepLabels<ImageType>(working_image,
224 param->GetFirstKeep(),
225 param->GetLastKeep(),
226 param->GetUseLastKeep());
227 return working_image;
229 //--------------------------------------------------------------------
232 //--------------------------------------------------------------------
233 template<class MaskImageType>
234 typename MaskImageType::Pointer
235 SliceBySliceRelativePosition(const MaskImageType * input,
236 const MaskImageType * object,
239 std::string orientation,
240 bool uniqueConnectedComponent,
243 bool singleObjectCCL)
245 typedef clitk::SliceBySliceRelativePositionFilter<MaskImageType> SliceRelPosFilterType;
246 typename SliceRelPosFilterType::Pointer sliceRelPosFilter = SliceRelPosFilterType::New();
247 sliceRelPosFilter->VerboseStepFlagOff();
248 sliceRelPosFilter->WriteStepFlagOff();
249 sliceRelPosFilter->SetInput(input);
250 sliceRelPosFilter->SetInputObject(object);
251 sliceRelPosFilter->SetDirection(direction);
252 sliceRelPosFilter->SetFuzzyThreshold(threshold);
253 sliceRelPosFilter->AddOrientationTypeString(orientation);
254 sliceRelPosFilter->SetIntermediateSpacingFlag((spacing != -1));
255 sliceRelPosFilter->SetIntermediateSpacing(spacing);
256 sliceRelPosFilter->SetUniqueConnectedComponentBySliceFlag(uniqueConnectedComponent);
257 sliceRelPosFilter->ObjectCCLSelectionFlagOff();
258 sliceRelPosFilter->SetUseTheLargestObjectCCLFlag(singleObjectCCL);
259 // sliceRelPosFilter->SetInverseOrientationFlag(inverseflag);
260 sliceRelPosFilter->SetAutoCropFlag(autocropFlag);
261 sliceRelPosFilter->IgnoreEmptySliceObjectFlagOn();
262 sliceRelPosFilter->Update();
263 return sliceRelPosFilter->GetOutput();
265 //--------------------------------------------------------------------
268 //--------------------------------------------------------------------
269 template<class MaskImageType>
270 typename MaskImageType::Pointer
271 SliceBySliceRelativePosition(const MaskImageType * input,
272 const MaskImageType * object,
277 bool uniqueConnectedComponent,
280 bool singleObjectCCL)
282 typedef clitk::SliceBySliceRelativePositionFilter<MaskImageType> SliceRelPosFilterType;
283 typename SliceRelPosFilterType::Pointer sliceRelPosFilter = SliceRelPosFilterType::New();
284 sliceRelPosFilter->VerboseStepFlagOff();
285 sliceRelPosFilter->WriteStepFlagOff();
286 sliceRelPosFilter->SetInput(input);
287 sliceRelPosFilter->SetInputObject(object);
288 sliceRelPosFilter->SetDirection(direction);
289 sliceRelPosFilter->SetFuzzyThreshold(threshold);
290 // sliceRelPosFilter->AddOrientationTypeString(orientation);
291 sliceRelPosFilter->AddAnglesInRad(angle, 0.0);
292 sliceRelPosFilter->SetIntermediateSpacingFlag((spacing != -1));
293 sliceRelPosFilter->SetIntermediateSpacing(spacing);
294 sliceRelPosFilter->SetUniqueConnectedComponentBySliceFlag(uniqueConnectedComponent);
295 sliceRelPosFilter->ObjectCCLSelectionFlagOff();
296 sliceRelPosFilter->SetUseTheLargestObjectCCLFlag(singleObjectCCL);
297 sliceRelPosFilter->SetInverseOrientationFlag(inverseflag);
298 sliceRelPosFilter->SetAutoCropFlag(autocropFlag);
299 sliceRelPosFilter->IgnoreEmptySliceObjectFlagOn();
300 sliceRelPosFilter->Update();
301 return sliceRelPosFilter->GetOutput();
303 //--------------------------------------------------------------------
306 //--------------------------------------------------------------------
307 template<class ImageType>
309 FindExtremaPointInAGivenDirection(const ImageType * input,
310 typename ImageType::PixelType bg,
311 int direction, bool opposite,
312 typename ImageType::PointType & point)
314 typename ImageType::PointType dummy;
315 return FindExtremaPointInAGivenDirection(input, bg, direction,
316 opposite, dummy, 0, point);
318 //--------------------------------------------------------------------
321 //--------------------------------------------------------------------
322 template<class ImageType>
324 FindExtremaPointInAGivenDirection(const ImageType * input,
325 typename ImageType::PixelType bg,
326 int direction, bool opposite,
327 typename ImageType::PointType refpoint,
329 typename ImageType::PointType & point)
332 loop over input pixels, store the index in the fg that is max
333 according to the given direction.
335 typedef itk::ImageRegionConstIteratorWithIndex<ImageType> IteratorType;
336 IteratorType iter(input, input->GetLargestPossibleRegion());
338 typename ImageType::IndexType max = input->GetLargestPossibleRegion().GetIndex();
339 if (opposite) max = max+input->GetLargestPossibleRegion().GetSize();
341 while (!iter.IsAtEnd()) {
342 if (iter.Get() != bg) {
343 bool test = iter.GetIndex()[direction] > max[direction];
344 if (opposite) test = !test;
346 typename ImageType::PointType p;
347 input->TransformIndexToPhysicalPoint(iter.GetIndex(), p);
348 if ((distanceMax==0) || (p.EuclideanDistanceTo(refpoint) < distanceMax)) {
349 max = iter.GetIndex();
356 if (!found) return false;
357 input->TransformIndexToPhysicalPoint(max, point);
360 //--------------------------------------------------------------------
363 //--------------------------------------------------------------------
364 template<class ImageType>
365 typename ImageType::Pointer
366 CropImageRemoveGreaterThan(const ImageType * image,
367 int dim, double min, bool autoCrop,
368 typename ImageType::PixelType BG)
370 return CropImageAlongOneAxis<ImageType>(image, dim,
371 image->GetOrigin()[dim],
375 //--------------------------------------------------------------------
378 //--------------------------------------------------------------------
379 template<class ImageType>
380 typename ImageType::Pointer
381 CropImageRemoveLowerThan(const ImageType * image,
382 int dim, double max, bool autoCrop,
383 typename ImageType::PixelType BG)
385 typename ImageType::PointType p;
386 image->TransformIndexToPhysicalPoint(image->GetLargestPossibleRegion().GetIndex()+
387 image->GetLargestPossibleRegion().GetSize(), p);
388 // Add GetSpacing because remove Lower or equal than
391 // DD(max+image->GetSpacing()[dim]);
392 return CropImageAlongOneAxis<ImageType>(image, dim, max+image->GetSpacing()[dim], p[dim], autoCrop, BG);
394 //--------------------------------------------------------------------
397 //--------------------------------------------------------------------
398 template<class ImageType>
399 typename ImageType::Pointer
400 CropImageAlongOneAxis(const ImageType * image,
401 int dim, double min, double max,
402 bool autoCrop, typename ImageType::PixelType BG)
404 // Compute region size
405 typename ImageType::RegionType region;
406 typename ImageType::SizeType size = image->GetLargestPossibleRegion().GetSize();
407 typename ImageType::PointType p = image->GetOrigin();
408 if (min > p[dim]) p[dim] = min; // Check if not outside the image
409 typename ImageType::IndexType start;
410 image->TransformPhysicalPointToIndex(p, start);
411 double m = image->GetOrigin()[dim] + size[dim]*image->GetSpacing()[dim];
412 if (max > m) p[dim] = m; // Check if not outside the image
414 typename ImageType::IndexType end;
415 image->TransformPhysicalPointToIndex(p, end);
416 size[dim] = abs(end[dim]-start[dim]);
417 region.SetIndex(start);
418 region.SetSize(size);
421 typedef itk::RegionOfInterestImageFilter<ImageType, ImageType> CropFilterType;
422 typename CropFilterType::Pointer cropFilter = CropFilterType::New();
423 cropFilter->SetInput(image);
424 cropFilter->SetRegionOfInterest(region);
425 cropFilter->Update();
426 typename ImageType::Pointer result = cropFilter->GetOutput();
430 result = AutoCrop<ImageType>(result, BG);
434 //--------------------------------------------------------------------
437 //--------------------------------------------------------------------
438 template<class ImageType>
440 ComputeCentroids(const ImageType * image,
441 typename ImageType::PixelType BG,
442 std::vector<typename ImageType::PointType> & centroids)
444 typedef long LabelType;
445 static const unsigned int Dim = ImageType::ImageDimension;
446 typedef itk::ShapeLabelObject< LabelType, Dim > LabelObjectType;
447 typedef itk::LabelMap< LabelObjectType > LabelMapType;
448 typedef itk::LabelImageToLabelMapFilter<ImageType, LabelMapType> ImageToMapFilterType;
449 typename ImageToMapFilterType::Pointer imageToLabelFilter = ImageToMapFilterType::New();
450 typedef itk::ShapeLabelMapFilter<LabelMapType, ImageType> ShapeFilterType;
451 typename ShapeFilterType::Pointer statFilter = ShapeFilterType::New();
452 imageToLabelFilter->SetBackgroundValue(BG);
453 imageToLabelFilter->SetInput(image);
454 statFilter->SetInput(imageToLabelFilter->GetOutput());
455 statFilter->Update();
456 typename LabelMapType::Pointer labelMap = statFilter->GetOutput();
459 typename ImageType::PointType dummy;
460 centroids.push_back(dummy); // label 0 -> no centroid, use dummy point for BG
461 //DS FIXME (not useful ! to change ..)
462 for(uint i=0; i<labelMap->GetNumberOfLabelObjects(); i++) {
463 int label = labelMap->GetLabels()[i];
464 centroids.push_back(labelMap->GetLabelObject(label)->GetCentroid());
467 //--------------------------------------------------------------------
470 //--------------------------------------------------------------------
471 template<class ImageType, class LabelType>
472 typename itk::LabelMap< itk::ShapeLabelObject<LabelType, ImageType::ImageDimension> >::Pointer
473 ComputeLabelMap(const ImageType * image,
474 typename ImageType::PixelType BG,
475 bool computePerimeterFlag)
477 static const unsigned int Dim = ImageType::ImageDimension;
478 typedef itk::ShapeLabelObject< LabelType, Dim > LabelObjectType;
479 typedef itk::LabelMap< LabelObjectType > LabelMapType;
480 typedef itk::LabelImageToLabelMapFilter<ImageType, LabelMapType> ImageToMapFilterType;
481 typename ImageToMapFilterType::Pointer imageToLabelFilter = ImageToMapFilterType::New();
482 typedef itk::ShapeLabelMapFilter<LabelMapType, ImageType> ShapeFilterType;
483 typename ShapeFilterType::Pointer statFilter = ShapeFilterType::New();
484 imageToLabelFilter->SetBackgroundValue(BG);
485 imageToLabelFilter->SetInput(image);
486 statFilter->SetInput(imageToLabelFilter->GetOutput());
487 statFilter->SetComputePerimeter(computePerimeterFlag);
488 statFilter->Update();
489 return statFilter->GetOutput();
491 //--------------------------------------------------------------------
494 //--------------------------------------------------------------------
495 template<class ImageType>
497 ComputeCentroids2(const ImageType * image,
498 typename ImageType::PixelType BG,
499 std::vector<typename ImageType::PointType> & centroids)
501 typedef long LabelType;
502 static const unsigned int Dim = ImageType::ImageDimension;
503 typedef itk::ShapeLabelObject< LabelType, Dim > LabelObjectType;
504 typedef itk::LabelMap< LabelObjectType > LabelMapType;
505 typedef itk::LabelImageToLabelMapFilter<ImageType, LabelMapType> ImageToMapFilterType;
506 typename ImageToMapFilterType::Pointer imageToLabelFilter = ImageToMapFilterType::New();
507 typedef itk::ShapeLabelMapFilter<LabelMapType, ImageType> ShapeFilterType;
508 typename ShapeFilterType::Pointer statFilter = ShapeFilterType::New();
509 imageToLabelFilter->SetBackgroundValue(BG);
510 imageToLabelFilter->SetInput(image);
511 statFilter->SetInput(imageToLabelFilter->GetOutput());
512 statFilter->Update();
513 typename LabelMapType::Pointer labelMap = statFilter->GetOutput();
516 typename ImageType::PointType dummy;
517 centroids.push_back(dummy); // label 0 -> no centroid, use dummy point
518 for(uint i=1; i<labelMap->GetNumberOfLabelObjects()+1; i++) {
519 centroids.push_back(labelMap->GetLabelObject(i)->GetCentroid());
522 for(uint i=1; i<labelMap->GetNumberOfLabelObjects()+1; i++) {
523 DD(labelMap->GetLabelObject(i)->GetBinaryPrincipalAxes());
524 DD(labelMap->GetLabelObject(i)->GetBinaryFlatness());
525 DD(labelMap->GetLabelObject(i)->GetRoundness ());
527 // search for the point on the boundary alog PA
532 //--------------------------------------------------------------------
535 //--------------------------------------------------------------------
536 template<class ImageType>
538 PointsUtils<ImageType>::Convert2DTo3D(const PointType2D & p2D,
539 const ImageType * image,
544 index3D[0] = index3D[1] = 0;
545 index3D[2] = image->GetLargestPossibleRegion().GetIndex()[2]+slice;
546 image->TransformIndexToPhysicalPoint(index3D, p3D);
549 // p3D[2] = p[2];//(image->GetLargestPossibleRegion().GetIndex()[2]+slice)*image->GetSpacing()[2]
550 // + image->GetOrigin()[2];
552 //--------------------------------------------------------------------
555 //--------------------------------------------------------------------
556 template<class ImageType>
558 PointsUtils<ImageType>::Convert2DMapTo3DList(const MapPoint2DType & map,
559 const ImageType * image,
560 VectorPoint3DType & list)
562 typename MapPoint2DType::const_iterator iter = map.begin();
563 while (iter != map.end()) {
565 Convert2DTo3D(iter->second, image, iter->first, p);
570 //--------------------------------------------------------------------
573 //--------------------------------------------------------------------
574 template<class ImageType>
576 PointsUtils<ImageType>::Convert2DListTo3DList(const VectorPoint2DType & p2D,
578 const ImageType * image,
579 VectorPoint3DType & list)
581 for(uint i=0; i<p2D.size(); i++) {
583 Convert2DTo3D(p2D[i], image, slice, p);
587 //--------------------------------------------------------------------
590 //--------------------------------------------------------------------
591 template<class ImageType>
593 WriteListOfLandmarks(std::vector<typename ImageType::PointType> points,
594 std::string filename)
597 openFileForWriting(os, filename);
598 os << "LANDMARKS1" << std::endl;
599 for(uint i=0; i<points.size(); i++) {
600 const typename ImageType::PointType & p = points[i];
601 // Write it in the file
602 os << i << " " << p[0] << " " << p[1] << " " << p[2] << " 0 0 " << std::endl;
606 //--------------------------------------------------------------------
609 //--------------------------------------------------------------------
610 template<class ImageType>
611 typename ImageType::Pointer
612 Dilate(const ImageType * image, double radiusInMM,
613 typename ImageType::PixelType BG,
614 typename ImageType::PixelType FG,
617 typename ImageType::SizeType r;
618 for(uint i=0; i<ImageType::ImageDimension; i++)
619 r[i] = (uint)lrint(radiusInMM/image->GetSpacing()[i]);
620 return Dilate<ImageType>(image, r, BG, FG, extendSupport);
622 //--------------------------------------------------------------------
625 //--------------------------------------------------------------------
626 template<class ImageType>
627 typename ImageType::Pointer
628 Dilate(const ImageType * image, typename ImageType::PointType radiusInMM,
629 typename ImageType::PixelType BG,
630 typename ImageType::PixelType FG,
633 typename ImageType::SizeType r;
634 for(uint i=0; i<ImageType::ImageDimension; i++)
635 r[i] = (uint)lrint(radiusInMM[i]/image->GetSpacing()[i]);
636 return Dilate<ImageType>(image, r, BG, FG, extendSupport);
638 //--------------------------------------------------------------------
641 //--------------------------------------------------------------------
642 template<class ImageType>
643 typename ImageType::Pointer
644 Dilate(const ImageType * image, typename ImageType::SizeType radius,
645 typename ImageType::PixelType BG,
646 typename ImageType::PixelType FG,
649 // Create kernel for dilatation
650 typedef itk::BinaryBallStructuringElement<typename ImageType::PixelType,
651 ImageType::ImageDimension> KernelType;
652 KernelType structuringElement;
653 structuringElement.SetRadius(radius);
654 structuringElement.CreateStructuringElement();
656 typename ImageType::Pointer output;
658 typedef itk::ConstantPadImageFilter<ImageType, ImageType> PadFilterType;
659 typename PadFilterType::Pointer padFilter = PadFilterType::New();
660 padFilter->SetInput(image);
661 typename ImageType::SizeType lower;
662 typename ImageType::SizeType upper;
663 for(uint i=0; i<3; i++) {
664 lower[i] = upper[i] = 2*(radius[i]+1);
666 padFilter->SetPadLowerBound(lower);
667 padFilter->SetPadUpperBound(upper);
669 output = padFilter->GetOutput();
673 typedef itk::BinaryDilateImageFilter<ImageType, ImageType , KernelType> DilateFilterType;
674 typename DilateFilterType::Pointer dilateFilter = DilateFilterType::New();
675 dilateFilter->SetBackgroundValue(BG);
676 dilateFilter->SetForegroundValue(FG);
677 dilateFilter->SetBoundaryToForeground(false);
678 dilateFilter->SetKernel(structuringElement);
679 if (extendSupport) dilateFilter->SetInput(output);
680 else dilateFilter->SetInput(image);
681 dilateFilter->Update();
682 return dilateFilter->GetOutput();
684 //--------------------------------------------------------------------
687 //--------------------------------------------------------------------
688 template<class ImageType>
689 typename ImageType::Pointer
690 Opening(const ImageType * image, typename ImageType::SizeType radius,
691 typename ImageType::PixelType BG,
692 typename ImageType::PixelType FG)
695 typedef itk::BinaryBallStructuringElement<typename ImageType::PixelType,
696 ImageType::ImageDimension> KernelType;
697 KernelType structuringElement;
698 structuringElement.SetRadius(radius);
699 structuringElement.CreateStructuringElement();
702 typedef itk::BinaryMorphologicalOpeningImageFilter<ImageType, ImageType , KernelType> OpeningFilterType;
703 typename OpeningFilterType::Pointer open = OpeningFilterType::New();
704 open->SetInput(image);
705 open->SetBackgroundValue(BG);
706 open->SetForegroundValue(FG);
707 open->SetKernel(structuringElement);
709 return open->GetOutput();
711 //--------------------------------------------------------------------
715 //--------------------------------------------------------------------
716 template<class ValueType, class VectorType>
717 void ConvertOption(std::string optionName, uint given,
718 ValueType * values, VectorType & p,
719 uint dim, bool required)
721 if (required && (given == 0)) {
722 clitkExceptionMacro("The option --" << optionName << " must be set and have 1 or "
723 << dim << " values.");
726 for(uint i=0; i<dim; i++) p[i] = values[0];
730 for(uint i=0; i<dim; i++) p[i] = values[i];
733 if (given == 0) return;
734 clitkExceptionMacro("The option --" << optionName << " must have 1 or "
735 << dim << " values.");
737 //--------------------------------------------------------------------
740 //--------------------------------------------------------------------
742 http://www.gamedev.net/community/forums/topic.asp?topic_id=542870
743 Assuming the points are (Ax,Ay) (Bx,By) and (Cx,Cy), you need to compute:
744 (Bx - Ax) * (Cy - Ay) - (By - Ay) * (Cx - Ax)
745 This will equal zero if the point C is on the line formed by
746 points A and B, and will have a different sign depending on the
747 side. Which side this is depends on the orientation of your (x,y)
748 coordinates, but you can plug test values for A,B and C into this
749 formula to determine whether negative values are to the left or to
751 => to accelerate, start with formula, when change sign -> stop and fill
753 offsetToKeep = is used to determine which side of the line we
754 keep. The point along the mainDirection but 'offsetToKeep' mm away
758 template<class ImageType>
760 SliceBySliceSetBackgroundFromLineSeparation(ImageType * input,
761 std::vector<typename ImageType::PointType> & lA,
762 std::vector<typename ImageType::PointType> & lB,
763 typename ImageType::PixelType BG,
767 assert((mainDirection==0) || (mainDirection==1));
768 typedef itk::ImageSliceIteratorWithIndex<ImageType> SliceIteratorType;
769 SliceIteratorType siter = SliceIteratorType(input, input->GetLargestPossibleRegion());
770 siter.SetFirstDirection(0);
771 siter.SetSecondDirection(1);
774 typename ImageType::PointType A;
775 typename ImageType::PointType B;
776 typename ImageType::PointType C;
777 assert(lA.size() == lB.size());
778 while ((i<lA.size()) && (!siter.IsAtEnd())) {
779 // Check that the current slice correspond to the current point
780 input->TransformIndexToPhysicalPoint(siter.GetIndex(), C);
781 if ((fabs(C[2] - lA[i][2]))>0.01) { // is !equal with a tolerance of 0.01 mm
784 // Define A,B,C points
788 // Check that the line is not a point (A=B)
789 bool p = (A[0] == B[0]) && (A[1] == B[1]);
792 C[mainDirection] += offsetToKeep; // I know I must keep this point
793 double s = (B[0] - A[0]) * (C[1] - A[1]) - (B[1] - A[1]) * (C[0] - A[0]);
794 bool isPositive = s<0;
795 while (!siter.IsAtEndOfSlice()) {
796 while (!siter.IsAtEndOfLine()) {
797 // Very slow, I know ... but image should be very small
798 input->TransformIndexToPhysicalPoint(siter.GetIndex(), C);
799 double s = (B[0] - A[0]) * (C[1] - A[1]) - (B[1] - A[1]) * (C[0] - A[0]);
800 if (s == 0) siter.Set(BG); // on the line, we decide to remove
802 if (s > 0) siter.Set(BG);
805 if (s < 0) siter.Set(BG);
814 } // End of current slice
818 //--------------------------------------------------------------------
821 //--------------------------------------------------------------------
822 template<class ImageType>
824 AndNot(ImageType * input,
825 const ImageType * object,
826 typename ImageType::PixelType BG)
828 typename ImageType::Pointer o;
830 if (!clitk::HaveSameSizeAndSpacing<ImageType, ImageType>(input, object)) {
831 o = clitk::ResizeImageLike<ImageType>(object, input, BG);
835 typedef clitk::BooleanOperatorLabelImageFilter<ImageType> BoolFilterType;
836 typename BoolFilterType::Pointer boolFilter = BoolFilterType::New();
837 boolFilter->InPlaceOn();
838 boolFilter->SetInput1(input);
839 if (resized) boolFilter->SetInput2(o);
840 else boolFilter->SetInput2(object);
841 boolFilter->SetBackgroundValue1(BG);
842 boolFilter->SetBackgroundValue2(BG);
843 boolFilter->SetOperationType(BoolFilterType::AndNot);
844 boolFilter->Update();
846 //--------------------------------------------------------------------
849 //--------------------------------------------------------------------
850 template<class ImageType>
852 And(ImageType * input,
853 const ImageType * object,
854 typename ImageType::PixelType BG)
856 typename ImageType::Pointer o;
858 if (!clitk::HaveSameSizeAndSpacing<ImageType, ImageType>(input, object)) {
859 o = clitk::ResizeImageLike<ImageType>(object, input, BG);
863 typedef clitk::BooleanOperatorLabelImageFilter<ImageType> BoolFilterType;
864 typename BoolFilterType::Pointer boolFilter = BoolFilterType::New();
865 boolFilter->InPlaceOn();
866 boolFilter->SetInput1(input);
867 if (resized) boolFilter->SetInput2(o);
868 else boolFilter->SetInput2(object);
869 boolFilter->SetBackgroundValue1(BG);
870 boolFilter->SetBackgroundValue2(BG);
871 boolFilter->SetOperationType(BoolFilterType::And);
872 boolFilter->Update();
874 //--------------------------------------------------------------------
877 //--------------------------------------------------------------------
878 template<class ImageType>
880 Or(ImageType * input,
881 const ImageType * object,
882 typename ImageType::PixelType BG)
884 typename ImageType::Pointer o;
886 if (!clitk::HaveSameSizeAndSpacing<ImageType, ImageType>(input, object)) {
887 o = clitk::ResizeImageLike<ImageType>(object, input, BG);
891 typedef clitk::BooleanOperatorLabelImageFilter<ImageType> BoolFilterType;
892 typename BoolFilterType::Pointer boolFilter = BoolFilterType::New();
893 boolFilter->InPlaceOn();
894 boolFilter->SetInput1(input);
895 if (resized) boolFilter->SetInput2(o);
896 else boolFilter->SetInput2(object);
897 boolFilter->SetBackgroundValue1(BG);
898 boolFilter->SetBackgroundValue2(BG);
899 boolFilter->SetOperationType(BoolFilterType::Or);
900 boolFilter->Update();
902 //--------------------------------------------------------------------
905 //--------------------------------------------------------------------
906 template<class ImageType>
907 typename ImageType::Pointer
908 Binarize(const ImageType * input,
909 typename ImageType::PixelType lower,
910 typename ImageType::PixelType upper,
911 typename ImageType::PixelType BG,
912 typename ImageType::PixelType FG)
914 typedef itk::BinaryThresholdImageFilter<ImageType, ImageType> BinaryThresholdFilterType;
915 typename BinaryThresholdFilterType::Pointer binarizeFilter = BinaryThresholdFilterType::New();
916 binarizeFilter->SetInput(input);
917 binarizeFilter->InPlaceOff();
918 binarizeFilter->SetLowerThreshold(lower);
919 binarizeFilter->SetUpperThreshold(upper);
920 binarizeFilter->SetInsideValue(FG);
921 binarizeFilter->SetOutsideValue(BG);
922 binarizeFilter->Update();
923 return binarizeFilter->GetOutput();
925 //--------------------------------------------------------------------
928 //--------------------------------------------------------------------
929 template<class ImageType>
931 GetMinMaxPointPosition(const ImageType * input,
932 typename ImageType::PointType & min,
933 typename ImageType::PointType & max)
935 typename ImageType::IndexType index = input->GetLargestPossibleRegion().GetIndex();
936 input->TransformIndexToPhysicalPoint(index, min);
937 index = index+input->GetLargestPossibleRegion().GetSize();
938 input->TransformIndexToPhysicalPoint(index, max);
940 //--------------------------------------------------------------------
943 //--------------------------------------------------------------------
944 template<class ImageType>
945 typename ImageType::PointType
946 FindExtremaPointInAGivenLine(const ImageType * input,
949 typename ImageType::PointType p,
950 typename ImageType::PixelType BG,
953 // Which direction ? Increasing or decreasing.
957 // Transform to pixel index
958 typename ImageType::IndexType index;
959 input->TransformPhysicalPointToIndex(p, index);
961 // Loop while inside the mask;
962 while (input->GetPixel(index) != BG) {
963 index[dimension] += d;
966 // Transform back to Physical Units
967 typename ImageType::PointType result;
968 input->TransformIndexToPhysicalPoint(index, result);
970 // Check that is is not too far away
971 double distance = p.EuclideanDistanceTo(result);
972 if (distance > distanceMax) {
973 result = p; // Get back to initial value
978 //--------------------------------------------------------------------
981 //--------------------------------------------------------------------
982 template<class PointType>
984 IsOnTheSameLineSide(PointType C, PointType A, PointType B, PointType like)
986 // Look at the position of point 'like' according to the AB line
987 double s = (B[0] - A[0]) * (like[1] - A[1]) - (B[1] - A[1]) * (like[0] - A[0]);
990 // Look the C position
991 s = (B[0] - A[0]) * (C[1] - A[1]) - (B[1] - A[1]) * (C[0] - A[0]);
993 if (negative && (s<=0)) return true;
994 if (!negative && (s>=0)) return true;
997 //--------------------------------------------------------------------
1000 //--------------------------------------------------------------------
1001 /* Consider an input object, for each slice, find the extrema
1002 position according to a given direction and build a line segment
1003 passing throught this point in a given direction. Output is a
1004 vector of line (from point A to B), for each slice;
1006 template<class ImageType>
1008 SliceBySliceBuildLineSegmentAccordingToExtremaPosition(const ImageType * input,
1009 typename ImageType::PixelType BG,
1011 int extremaDirection,
1012 bool extremaOppositeFlag,
1015 std::vector<typename ImageType::PointType> & A,
1016 std::vector<typename ImageType::PointType> & B)
1019 typedef typename itk::Image<typename ImageType::PixelType, ImageType::ImageDimension-1> SliceType;
1021 // Build the list of slices
1022 std::vector<typename SliceType::Pointer> slices;
1023 clitk::ExtractSlices<ImageType>(input, sliceDimension, slices);
1025 // Build the list of 2D points
1026 std::map<int, typename SliceType::PointType> position2D;
1027 for(uint i=0; i<slices.size(); i++) {
1028 typename SliceType::PointType p;
1030 clitk::FindExtremaPointInAGivenDirection<SliceType>(slices[i], BG,
1031 extremaDirection, extremaOppositeFlag, p);
1037 // Convert 2D points in slice into 3D points
1038 clitk::PointsUtils<ImageType>::Convert2DMapTo3DList(position2D, input, A);
1040 // Create additional point just right to the previous ones, on the
1041 // given lineDirection, in order to create a horizontal/vertical line.
1042 for(uint i=0; i<A.size(); i++) {
1043 typename ImageType::PointType p = A[i];
1044 p[lineDirection] += 10;
1047 A[i][extremaDirection] += margin;
1048 B[i][extremaDirection] += margin;
1052 //--------------------------------------------------------------------
1055 //--------------------------------------------------------------------
1056 template<class ImageType>
1057 typename ImageType::Pointer
1058 SliceBySliceKeepMainCCL(const ImageType * input,
1059 typename ImageType::PixelType BG,
1060 typename ImageType::PixelType FG) {
1063 const int d = ImageType::ImageDimension-1;
1064 typedef typename itk::Image<typename ImageType::PixelType, d> SliceType;
1065 std::vector<typename SliceType::Pointer> slices;
1066 clitk::ExtractSlices<ImageType>(input, d, slices);
1068 // Labelize and keep the main one
1069 std::vector<typename SliceType::Pointer> o;
1070 for(uint i=0; i<slices.size(); i++) {
1071 o.push_back(clitk::Labelize<SliceType>(slices[i], BG, false, 1));
1072 o[i] = clitk::KeepLabels<SliceType>(o[i], BG, FG, 1, 1, true);
1076 typename ImageType::Pointer output;
1077 output = clitk::JoinSlices<ImageType>(o, input, d);
1080 //--------------------------------------------------------------------
1083 //--------------------------------------------------------------------
1084 template<class ImageType>
1085 typename ImageType::Pointer
1086 Clone(const ImageType * input) {
1087 typedef itk::ImageDuplicator<ImageType> DuplicatorType;
1088 typename DuplicatorType::Pointer duplicator = DuplicatorType::New();
1089 duplicator->SetInputImage(input);
1090 duplicator->Update();
1091 return duplicator->GetOutput();
1093 //--------------------------------------------------------------------
1096 //--------------------------------------------------------------------
1097 /* Consider an input object, start at A, for each slice (dim1):
1098 - compute the intersection between the AB line and the current slice
1099 - remove what is at lower or greater according to dim2 of this point
1102 template<class ImageType>
1103 typename ImageType::Pointer
1104 SliceBySliceSetBackgroundFromSingleLine(const ImageType * input,
1105 typename ImageType::PixelType BG,
1106 typename ImageType::PointType & A,
1107 typename ImageType::PointType & B,
1108 int dim1, int dim2, bool removeLowerPartFlag)
1112 typedef typename itk::Image<typename ImageType::PixelType, ImageType::ImageDimension-1> SliceType;
1113 typedef typename SliceType::Pointer SlicePointer;
1114 std::vector<SlicePointer> slices;
1115 clitk::ExtractSlices<ImageType>(input, dim1, slices);
1117 // Start at slice that contains A, and stop at B
1118 typename ImageType::IndexType Ap;
1119 typename ImageType::IndexType Bp;
1120 input->TransformPhysicalPointToIndex(A, Ap);
1121 input->TransformPhysicalPointToIndex(B, Bp);
1123 // Determine slice largest region
1124 typename SliceType::RegionType region = slices[0]->GetLargestPossibleRegion();
1125 typename SliceType::SizeType size = region.GetSize();
1126 typename SliceType::IndexType index = region.GetIndex();
1129 double a = (Bp[dim2]-Ap[dim2])/(Bp[dim1]-Ap[dim1]);
1130 double b = Ap[dim2];
1132 // Loop from slice A to slice B
1133 for(uint i=0; i<(Bp[dim1]-Ap[dim1]); i++) {
1134 // Compute intersection between line AB and current slice for the dim2
1136 // Change region (lower than dim2)
1137 if (removeLowerPartFlag) {
1138 size[dim2] = p-Ap[dim2];
1141 size[dim2] = slices[0]->GetLargestPossibleRegion().GetSize()[dim2]-p;
1144 region.SetSize(size);
1145 region.SetIndex(index);
1146 // Fill region with BG (simple region iterator)
1147 FillRegionWithValue<SliceType>(slices[i+Ap[dim1]], BG, region);
1149 typedef itk::ImageRegionIterator<SliceType> IteratorType;
1150 IteratorType iter(slices[i+Ap[dim1]], region);
1152 while (!iter.IsAtEnd()) {
1161 typename ImageType::Pointer output;
1162 output = clitk::JoinSlices<ImageType>(slices, input, dim1);
1165 //--------------------------------------------------------------------
1167 //--------------------------------------------------------------------
1168 /* Consider an input object, slice by slice, use the point A and set
1169 pixel to BG according to their position relatively to A
1171 template<class ImageType>
1172 typename ImageType::Pointer
1173 SliceBySliceSetBackgroundFromPoints(const ImageType * input,
1174 typename ImageType::PixelType BG,
1176 std::vector<typename ImageType::PointType> & A,
1177 bool removeGreaterThanXFlag,
1178 bool removeGreaterThanYFlag)
1182 typedef typename itk::Image<typename ImageType::PixelType, ImageType::ImageDimension-1> SliceType;
1183 typedef typename SliceType::Pointer SlicePointer;
1184 std::vector<SlicePointer> slices;
1185 clitk::ExtractSlices<ImageType>(input, sliceDim, slices);
1187 // Start at slice that contains A
1188 typename ImageType::IndexType Ap;
1190 // Determine slice largest region
1191 typename SliceType::RegionType region = slices[0]->GetLargestPossibleRegion();
1192 typename SliceType::SizeType size = region.GetSize();
1193 typename SliceType::IndexType index = region.GetIndex();
1195 // Loop from slice A to slice B
1196 for(uint i=0; i<A.size(); i++) {
1197 input->TransformPhysicalPointToIndex(A[i], Ap);
1198 uint sliceIndex = Ap[2] - input->GetLargestPossibleRegion().GetIndex()[2];
1199 if ((sliceIndex < 0) || (sliceIndex >= slices.size())) {
1200 continue; // do not consider this slice
1203 // Compute region for BG
1204 if (removeGreaterThanXFlag) {
1206 size[0] = region.GetSize()[0]-(index[0]-region.GetIndex()[0]);
1209 index[0] = region.GetIndex()[0];
1210 size[0] = Ap[0] - index[0];
1213 if (removeGreaterThanYFlag) {
1215 size[1] = region.GetSize()[1]-(index[1]-region.GetIndex()[1]);
1218 index[1] = region.GetIndex()[1];
1219 size[1] = Ap[1] - index[1];
1223 region.SetSize(size);
1224 region.SetIndex(index);
1226 // Fill region with BG (simple region iterator)
1227 FillRegionWithValue<SliceType>(slices[sliceIndex], BG, region);
1232 typename ImageType::Pointer output;
1233 output = clitk::JoinSlices<ImageType>(slices, input, sliceDim);
1236 //--------------------------------------------------------------------
1239 //--------------------------------------------------------------------
1240 template<class ImageType>
1242 FillRegionWithValue(ImageType * input, typename ImageType::PixelType value, typename ImageType::RegionType & region)
1244 typedef itk::ImageRegionIterator<ImageType> IteratorType;
1245 IteratorType iter(input, region);
1247 while (!iter.IsAtEnd()) {
1252 //--------------------------------------------------------------------
1255 //--------------------------------------------------------------------
1256 template<class ImageType>
1258 GetMinMaxBoundary(ImageType * input, typename ImageType::PointType & min,
1259 typename ImageType::PointType & max)
1261 typedef typename ImageType::PointType PointType;
1262 typedef typename ImageType::IndexType IndexType;
1263 IndexType min_i, max_i;
1264 min_i = input->GetLargestPossibleRegion().GetIndex();
1265 for(uint i=0; i<ImageType::ImageDimension; i++)
1266 max_i[i] = input->GetLargestPossibleRegion().GetSize()[i] + min_i[i];
1267 input->TransformIndexToPhysicalPoint(min_i, min);
1268 input->TransformIndexToPhysicalPoint(max_i, max);
1270 //--------------------------------------------------------------------
1273 //--------------------------------------------------------------------
1274 template<class ImageType>
1275 typename itk::Image<float, ImageType::ImageDimension>::Pointer
1276 DistanceMap(const ImageType * input, typename ImageType::PixelType BG)//,
1277 // typename itk::Image<float, ImageType::ImageDimension>::Pointer dmap)
1279 typedef itk::Image<float,ImageType::ImageDimension> FloatImageType;
1280 typedef itk::SignedMaurerDistanceMapImageFilter<ImageType, FloatImageType> DistanceMapFilterType;
1281 typename DistanceMapFilterType::Pointer filter = DistanceMapFilterType::New();
1282 filter->SetInput(input);
1283 filter->SetUseImageSpacing(true);
1284 filter->SquaredDistanceOff();
1285 filter->SetBackgroundValue(BG);
1287 return filter->GetOutput();
1289 //--------------------------------------------------------------------
1292 //--------------------------------------------------------------------
1293 template<class ImageType>
1295 SliceBySliceBuildLineSegmentAccordingToMinimalDistanceBetweenStructures(const ImageType * S1,
1296 const ImageType * S2,
1297 typename ImageType::PixelType BG,
1299 std::vector<typename ImageType::PointType> & A,
1300 std::vector<typename ImageType::PointType> & B)
1303 typedef typename itk::Image<typename ImageType::PixelType, 2> SliceType;
1304 typedef typename SliceType::Pointer SlicePointer;
1305 std::vector<SlicePointer> slices_s1;
1306 std::vector<SlicePointer> slices_s2;
1307 clitk::ExtractSlices<ImageType>(S1, sliceDimension, slices_s1);
1308 clitk::ExtractSlices<ImageType>(S2, sliceDimension, slices_s2);
1310 assert(slices_s1.size() == slices_s2.size());
1313 typedef itk::Image<float,2> FloatImageType;
1314 typedef itk::SignedMaurerDistanceMapImageFilter<SliceType, FloatImageType> DistanceMapFilterType;
1315 std::vector<typename FloatImageType::Pointer> dmaps1;
1316 std::vector<typename FloatImageType::Pointer> dmaps2;
1317 typename FloatImageType::Pointer dmap;
1320 for(uint i=0; i<slices_s1.size(); i++) {
1321 // Compute dmap for S1 *TO PUT IN FONCTION*
1322 dmap = clitk::DistanceMap<SliceType>(slices_s1[i], BG);
1323 dmaps1.push_back(dmap);
1324 writeImage<FloatImageType>(dmap, "dmap1.mha");
1325 // Compute dmap for S2
1326 dmap = clitk::DistanceMap<SliceType>(slices_s2[i], BG);
1327 dmaps2.push_back(dmap);
1328 writeImage<FloatImageType>(dmap, "dmap2.mha");
1330 // Look in S2 for the point the closest to S1
1331 typename SliceType::PointType p = ComputeClosestPoint<SliceType>(slices_s1[i], dmaps2[i], BG);
1332 typename ImageType::PointType p3D;
1333 clitk::PointsUtils<ImageType>::Convert2DTo3D(p, S1, i, p3D);
1336 // Look in S2 for the point the closest to S1
1337 p = ComputeClosestPoint<SliceType>(slices_s2[i], dmaps1[i], BG);
1338 clitk::PointsUtils<ImageType>::Convert2DTo3D(p, S2, i, p3D);
1345 typedef itk::Image<float,3> FT;
1346 FT::Pointer f = FT::New();
1347 typename FT::Pointer d1 = clitk::JoinSlices<FT>(dmaps1, S1, 2);
1348 typename FT::Pointer d2 = clitk::JoinSlices<FT>(dmaps2, S2, 2);
1349 writeImage<FT>(d1, "d1.mha");
1350 writeImage<FT>(d2, "d2.mha");
1353 //--------------------------------------------------------------------
1356 //--------------------------------------------------------------------
1357 template<class ImageType>
1358 typename ImageType::PointType
1359 ComputeClosestPoint(const ImageType * input,
1360 const itk::Image<float, ImageType::ImageDimension> * dmap,
1361 typename ImageType::PixelType & BG)
1363 // Loop dmap + S2, if FG, get min
1364 typedef itk::Image<float,ImageType::ImageDimension> FloatImageType;
1365 typedef itk::ImageRegionConstIteratorWithIndex<ImageType> ImageIteratorType;
1366 typedef itk::ImageRegionConstIterator<FloatImageType> DMapIteratorType;
1367 ImageIteratorType iter1(input, input->GetLargestPossibleRegion());
1368 DMapIteratorType iter2(dmap, dmap->GetLargestPossibleRegion());
1372 double dmin = 100000.0;
1373 typename ImageType::IndexType indexmin;
1375 while (!iter1.IsAtEnd()) {
1376 if (iter1.Get() != BG) {
1377 double d = iter2.Get();
1379 indexmin = iter1.GetIndex();
1388 typename ImageType::PointType p;
1389 input->TransformIndexToPhysicalPoint(indexmin, p);
1392 //--------------------------------------------------------------------
1397 } // end of namespace