From b5d26905446e4f21cc338d6ca962fac2145a47a9 Mon Sep 17 00:00:00 2001 From: dsarrut Date: Tue, 12 Jul 2011 08:14:10 +0200 Subject: [PATCH] Add clitkAnd, SliceBySliceSetBackgroundFromSingleLine, FillRegionWithValue --- itk/clitkSegmentationUtils.txx | 116 +++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) diff --git a/itk/clitkSegmentationUtils.txx b/itk/clitkSegmentationUtils.txx index b432188..b328315 100644 --- a/itk/clitkSegmentationUtils.txx +++ b/itk/clitkSegmentationUtils.txx @@ -354,6 +354,7 @@ namespace clitk { sliceRelPosFilter->SetIntermediateSpacingFlag((spacing != -1)); sliceRelPosFilter->SetIntermediateSpacing(spacing); sliceRelPosFilter->SetUniqueConnectedComponentBySlice(uniqueConnectedComponent); + sliceRelPosFilter->CCLSelectionFlagOff(); sliceRelPosFilter->SetUseASingleObjectConnectedComponentBySliceFlag(singleObjectCCL); // sliceRelPosFilter->SetInverseOrientationFlag(inverseflag); sliceRelPosFilter->SetAutoCropFlag(autocropFlag); @@ -876,6 +877,7 @@ namespace clitk { } //-------------------------------------------------------------------- + //-------------------------------------------------------------------- template void @@ -904,6 +906,34 @@ namespace clitk { //-------------------------------------------------------------------- + //-------------------------------------------------------------------- + template + void + And(ImageType * input, + const ImageType * object, + typename ImageType::PixelType BG) + { + typename ImageType::Pointer o; + bool resized=false; + if (!clitk::HaveSameSizeAndSpacing(input, object)) { + o = clitk::ResizeImageLike(object, input, BG); + resized = true; + } + + typedef clitk::BooleanOperatorLabelImageFilter BoolFilterType; + typename BoolFilterType::Pointer boolFilter = BoolFilterType::New(); + boolFilter->InPlaceOn(); + boolFilter->SetInput1(input); + if (resized) boolFilter->SetInput2(o); + else boolFilter->SetInput2(object); + boolFilter->SetBackgroundValue1(BG); + boolFilter->SetBackgroundValue2(BG); + boolFilter->SetOperationType(BoolFilterType::And); + boolFilter->Update(); + } + //-------------------------------------------------------------------- + + //-------------------------------------------------------------------- template typename ImageType::Pointer @@ -1095,5 +1125,91 @@ namespace clitk { //-------------------------------------------------------------------- + //-------------------------------------------------------------------- + /* Consider an input object, start at A, for each slice (dim1): + - compute the intersection between the AB line and the current slice + - remove what is at lower or greater according to dim2 of this point + - stop at B + */ + template + typename ImageType::Pointer + SliceBySliceSetBackgroundFromSingleLine(const ImageType * input, + typename ImageType::PixelType BG, + typename ImageType::PointType & A, + typename ImageType::PointType & B, + int dim1, int dim2, bool removeLowerPartFlag) + + { + // Extract slices + typedef typename itk::Image SliceType; + typedef typename SliceType::Pointer SlicePointer; + std::vector slices; + clitk::ExtractSlices(input, dim1, slices); + + // Start at slice that contains A, and stop at B + typename ImageType::IndexType Ap; + typename ImageType::IndexType Bp; + input->TransformPhysicalPointToIndex(A, Ap); + input->TransformPhysicalPointToIndex(B, Bp); + + // Determine slice largest region + typename SliceType::RegionType region = slices[0]->GetLargestPossibleRegion(); + typename SliceType::SizeType size = region.GetSize(); + typename SliceType::IndexType index = region.GetIndex(); + + // Line slope + double a = (Bp[dim2]-Ap[dim2])/(Bp[dim1]-Ap[dim1]); + double b = Ap[dim2]; + + // Loop from slice A to slice B + for(uint i=0; i<(Bp[dim1]-Ap[dim1]); i++) { + // Compute intersection between line AB and current slice for the dim2 + double p = a*i+b; + // Change region (lower than dim2) + if (removeLowerPartFlag) { + size[dim2] = p-Ap[dim2]; + } + else { + size[dim2] = slices[0]->GetLargestPossibleRegion().GetSize()[dim2]-p; + index[dim2] = p; + } + region.SetSize(size); + region.SetIndex(index); + // Fill region with BG (simple region iterator) + FillRegionWithValue(slices[i+Ap[dim1]], BG, region); + /* + typedef itk::ImageRegionIterator IteratorType; + IteratorType iter(slices[i+Ap[dim1]], region); + iter.GoToBegin(); + while (!iter.IsAtEnd()) { + iter.Set(BG); + ++iter; + } + */ + // Loop + } + + // Merge slices + typename ImageType::Pointer output; + output = clitk::JoinSlices(slices, input, dim1); + return output; + } + //-------------------------------------------------------------------- + + //-------------------------------------------------------------------- + template + void + FillRegionWithValue(ImageType * input, typename ImageType::PixelType value, typename ImageType::RegionType & region) + { + typedef itk::ImageRegionIterator IteratorType; + IteratorType iter(input, region); + iter.GoToBegin(); + while (!iter.IsAtEnd()) { + iter.Set(value); + ++iter; + } + } + //-------------------------------------------------------------------- + } // end of namespace -- 2.47.1