+ template<class ImageType>
+ typename ImageType::Pointer
+ LabelizeAndCountNumberOfObjects(const ImageType * input,
+ typename ImageType::PixelType BG,
+ bool isFullyConnected,
+ int minimalComponentSize,
+ int & nb) {
+ // InternalImageType for storing large number of component
+ typedef itk::Image<int, ImageType::ImageDimension> InternalImageType;
+
+ // Connected Component label
+ typedef itk::ConnectedComponentImageFilter<ImageType, InternalImageType> ConnectFilterType;
+ typename ConnectFilterType::Pointer connectFilter = ConnectFilterType::New();
+ // connectFilter->ReleaseDataFlagOn();
+ connectFilter->SetInput(input);
+ connectFilter->SetBackgroundValue(BG);
+ connectFilter->SetFullyConnected(isFullyConnected);
+
+ // Sort by size and remove too small area.
+ typedef itk::RelabelComponentImageFilter<InternalImageType, ImageType> RelabelFilterType;
+ typename RelabelFilterType::Pointer relabelFilter = RelabelFilterType::New();
+ // relabelFilter->ReleaseDataFlagOn(); // if yes, fail when ExplosionControlledThresholdConnectedImageFilter ???
+ relabelFilter->SetInput(connectFilter->GetOutput());
+ relabelFilter->SetMinimumObjectSize(minimalComponentSize);
+ relabelFilter->Update();
+
+ nb = relabelFilter->GetNumberOfObjects();
+ // DD(relabelFilter->GetOriginalNumberOfObjects());
+ // DD(relabelFilter->GetSizeOfObjectsInPhysicalUnits()[0]);
+
+ // Return result
+ typename ImageType::Pointer output = relabelFilter->GetOutput();
+ return output;
+ }
+ //--------------------------------------------------------------------
+
+
+
+ //--------------------------------------------------------------------
+ template<class ImageType>
+ typename ImageType::Pointer
+ RemoveLabels(const ImageType * input,
+ typename ImageType::PixelType BG,
+ std::vector<typename ImageType::PixelType> & labelsToRemove) {
+ assert(labelsToRemove.size() != 0);
+ typename ImageType::Pointer working_image;// = input;
+ for (unsigned int i=0; i <labelsToRemove.size(); i++) {
+ typedef SetBackgroundImageFilter<ImageType, ImageType> SetBackgroundImageFilterType;
+ typename SetBackgroundImageFilterType::Pointer setBackgroundFilter = SetBackgroundImageFilterType::New();
+ setBackgroundFilter->SetInput(input);
+ setBackgroundFilter->SetInput2(input);
+ setBackgroundFilter->SetMaskValue(labelsToRemove[i]);
+ setBackgroundFilter->SetOutsideValue(BG);
+ setBackgroundFilter->Update();
+ working_image = setBackgroundFilter->GetOutput();
+ }
+ return working_image;
+ }
+ //--------------------------------------------------------------------
+
+
+ //--------------------------------------------------------------------
+ template<class ImageType>
+ typename ImageType::Pointer
+ KeepLabels(const ImageType * input,
+ typename ImageType::PixelType BG,
+ typename ImageType::PixelType FG,
+ typename ImageType::PixelType firstKeep,
+ typename ImageType::PixelType lastKeep,
+ bool useLastKeep) {
+ typedef itk::BinaryThresholdImageFilter<ImageType, ImageType> BinarizeFilterType;
+ typename BinarizeFilterType::Pointer binarizeFilter = BinarizeFilterType::New();
+ binarizeFilter->SetInput(input);
+ binarizeFilter->SetLowerThreshold(firstKeep);
+ if (useLastKeep) binarizeFilter->SetUpperThreshold(lastKeep);
+ binarizeFilter->SetInsideValue(FG);
+ binarizeFilter->SetOutsideValue(BG);
+ binarizeFilter->Update();
+ return binarizeFilter->GetOutput();
+ }
+ //--------------------------------------------------------------------
+
+
+ //--------------------------------------------------------------------
+ template<class ImageType>
+ typename ImageType::Pointer
+ LabelizeAndSelectLabels(const ImageType * input,
+ typename ImageType::PixelType BG,
+ typename ImageType::PixelType FG,
+ bool isFullyConnected,
+ int minimalComponentSize,
+ LabelizeParameters<typename ImageType::PixelType> * param)
+ {
+ typename ImageType::Pointer working_image;
+ working_image = Labelize<ImageType>(input, BG, isFullyConnected, minimalComponentSize);
+ working_image = RemoveLabels<ImageType>(working_image, BG, param->GetLabelsToRemove());
+ working_image = KeepLabels<ImageType>(working_image,
+ BG, FG,
+ param->GetFirstKeep(),
+ param->GetLastKeep(),
+ param->GetUseLastKeep());
+ return working_image;
+ }
+ //--------------------------------------------------------------------
+
+
+ //--------------------------------------------------------------------
+ template<class ImageType>
+ typename ImageType::Pointer
+ ResizeImageLike(const ImageType * input,
+ const itk::ImageBase<ImageType::ImageDimension> * like,
+ typename ImageType::PixelType backgroundValue)
+ {
+ typedef CropLikeImageFilter<ImageType> CropFilterType;
+ typename CropFilterType::Pointer cropFilter = CropFilterType::New();
+ cropFilter->SetInput(input);
+ cropFilter->SetCropLikeImage(like);
+ cropFilter->SetBackgroundValue(backgroundValue);
+ cropFilter->Update();
+ return cropFilter->GetOutput();
+ }
+ //--------------------------------------------------------------------
+
+
+ //--------------------------------------------------------------------
+ template<class MaskImageType>
+ typename MaskImageType::Pointer
+ SliceBySliceRelativePosition(const MaskImageType * input,
+ const MaskImageType * object,
+ int direction,
+ double threshold,
+ std::string orientation,
+ bool uniqueConnectedComponent,
+ double spacing,
+ bool inverseflag)
+ {
+ typedef SliceBySliceRelativePositionFilter<MaskImageType> SliceRelPosFilterType;
+ typename SliceRelPosFilterType::Pointer sliceRelPosFilter = SliceRelPosFilterType::New();
+ sliceRelPosFilter->VerboseStepFlagOff();
+ sliceRelPosFilter->WriteStepFlagOff();
+ sliceRelPosFilter->SetInput(input);
+ sliceRelPosFilter->SetInputObject(object);
+ sliceRelPosFilter->SetDirection(direction);
+ sliceRelPosFilter->SetFuzzyThreshold(threshold);
+ sliceRelPosFilter->AddOrientationTypeString(orientation);
+ sliceRelPosFilter->SetIntermediateSpacingFlag((spacing != -1));
+ sliceRelPosFilter->SetIntermediateSpacing(spacing);
+ sliceRelPosFilter->SetUniqueConnectedComponentBySlice(uniqueConnectedComponent);
+ sliceRelPosFilter->SetInverseOrientationFlag(inverseflag);
+ // sliceRelPosFilter->SetAutoCropFlag(true); ??
+ sliceRelPosFilter->Update();
+ return sliceRelPosFilter->GetOutput();
+ }
+ //--------------------------------------------------------------------
+
+ //--------------------------------------------------------------------
+ template<class ImageType>
+ bool
+ FindExtremaPointInAGivenDirection(const ImageType * input,
+ typename ImageType::PixelType bg,
+ int direction, bool opposite,
+ typename ImageType::PointType & point)
+ {
+ typename ImageType::PointType dummy;
+ return FindExtremaPointInAGivenDirection(input, bg, direction,
+ opposite, dummy, 0, point);
+ }
+ //--------------------------------------------------------------------
+
+ //--------------------------------------------------------------------
+ template<class ImageType>
+ bool
+ FindExtremaPointInAGivenDirection(const ImageType * input,
+ typename ImageType::PixelType bg,
+ int direction, bool opposite,
+ typename ImageType::PointType refpoint,
+ double distanceMax,
+ typename ImageType::PointType & point)
+ {
+ /*
+ loop over input pixels, store the index in the fg that is max
+ according to the given direction.
+ */
+ typedef itk::ImageRegionConstIteratorWithIndex<ImageType> IteratorType;
+ IteratorType iter(input, input->GetLargestPossibleRegion());
+ iter.GoToBegin();
+ typename ImageType::IndexType max = input->GetLargestPossibleRegion().GetIndex();
+ if (opposite) max = max+input->GetLargestPossibleRegion().GetSize();
+ bool found=false;
+ while (!iter.IsAtEnd()) {
+ if (iter.Get() != bg) {
+ bool test = iter.GetIndex()[direction] > max[direction];
+ if (opposite) test = !test;
+ if (test) {
+ typename ImageType::PointType p;
+ input->TransformIndexToPhysicalPoint(iter.GetIndex(), p);
+ if ((distanceMax==0) || (p.EuclideanDistanceTo(refpoint) < distanceMax)) {
+ max = iter.GetIndex();
+ found = true;
+ }