From: David Sarrut Date: Wed, 5 Oct 2011 09:02:45 +0000 (+0200) Subject: Add "SliceBySliceBuildLineSegmentAccordingToMinimalDistanceBetweenStructures" (and... X-Git-Tag: v1.3.0~204 X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=9ad931d0b92b6f5ae0f4eb4c2c90319eeb8316ef;p=clitk.git Add "SliceBySliceBuildLineSegmentAccordingToMinimalDistanceBetweenStructures" (and win the best price for the longest function name in clitk) Add DistanceMap Add ComputeClosestPoint Correct JoinSlices (with ImageBase instead of Image as option) --- diff --git a/itk/clitkSegmentationUtils.h b/itk/clitkSegmentationUtils.h index f58b7c4..70388fe 100644 --- a/itk/clitkSegmentationUtils.h +++ b/itk/clitkSegmentationUtils.h @@ -247,7 +247,7 @@ namespace clitk { typename ImageType::Pointer JoinSlices(std::vector::Pointer > & slices, - const ImageType * input, + const itk::ImageBase * input, //const ImageType * input, int direction) { typedef typename itk::Image SliceType; typedef itk::JoinSeriesImageFilter JoinSeriesFilterType; @@ -419,6 +419,14 @@ namespace clitk { double margin, std::vector & A, std::vector & B); + template + void + SliceBySliceBuildLineSegmentAccordingToMinimalDistanceBetweenStructures(const ImageType * S1, + const ImageType * S2, + typename ImageType::PixelType BG, + int sliceDimension, + std::vector & A, + std::vector & B); //-------------------------------------------------------------------- @@ -476,7 +484,25 @@ namespace clitk { typename ImageType::PointType & max); //-------------------------------------------------------------------- -} + + //-------------------------------------------------------------------- + template + typename itk::Image::Pointer//void + DistanceMap(const ImageType * input, typename ImageType::PixelType BG);//, + //-------------------------------------------------------------------- + + + //-------------------------------------------------------------------- + template + typename ImageType::PointType + ComputeClosestPoint(const ImageType * input, + const itk::Image * dmap, + typename ImageType::PixelType & BG); + //-------------------------------------------------------------------- + + + +} // end clitk namespace #include "clitkSegmentationUtils.txx" diff --git a/itk/clitkSegmentationUtils.txx b/itk/clitkSegmentationUtils.txx index edb208a..adca988 100644 --- a/itk/clitkSegmentationUtils.txx +++ b/itk/clitkSegmentationUtils.txx @@ -34,6 +34,7 @@ #include #include #include +#include namespace clitk { @@ -1327,5 +1328,129 @@ namespace clitk { } //-------------------------------------------------------------------- + + //-------------------------------------------------------------------- + template + typename itk::Image::Pointer + DistanceMap(const ImageType * input, typename ImageType::PixelType BG)//, + // typename itk::Image::Pointer dmap) + { + typedef itk::Image FloatImageType; + typedef itk::SignedMaurerDistanceMapImageFilter DistanceMapFilterType; + typename DistanceMapFilterType::Pointer filter = DistanceMapFilterType::New(); + filter->SetInput(input); + filter->SetUseImageSpacing(true); + filter->SquaredDistanceOff(); + filter->SetBackgroundValue(BG); + filter->Update(); + return filter->GetOutput(); + } + //-------------------------------------------------------------------- + + + //-------------------------------------------------------------------- + template + void + SliceBySliceBuildLineSegmentAccordingToMinimalDistanceBetweenStructures(const ImageType * S1, + const ImageType * S2, + typename ImageType::PixelType BG, + int sliceDimension, + std::vector & A, + std::vector & B) + { + // Extract slices + typedef typename itk::Image SliceType; + typedef typename SliceType::Pointer SlicePointer; + std::vector slices_s1; + std::vector slices_s2; + clitk::ExtractSlices(S1, sliceDimension, slices_s1); + clitk::ExtractSlices(S2, sliceDimension, slices_s2); + + assert(slices_s1.size() == slices_s2.size()); + + // Prepare dmap + typedef itk::Image FloatImageType; + typedef itk::SignedMaurerDistanceMapImageFilter DistanceMapFilterType; + std::vector dmaps1; + std::vector dmaps2; + typename FloatImageType::Pointer dmap; + + // loop on slices + for(uint i=0; i(slices_s1[i], BG); + dmaps1.push_back(dmap); + writeImage(dmap, "dmap1.mha"); + // Compute dmap for S2 + dmap = clitk::DistanceMap(slices_s2[i], BG); + dmaps2.push_back(dmap); + writeImage(dmap, "dmap2.mha"); + + // Look in S2 for the point the closest to S1 + typename SliceType::PointType p = ComputeClosestPoint(slices_s1[i], dmaps2[i], BG); + typename ImageType::PointType p3D; + clitk::PointsUtils::Convert2DTo3D(p, S1, i, p3D); + A.push_back(p3D); + + // Look in S2 for the point the closest to S1 + p = ComputeClosestPoint(slices_s2[i], dmaps1[i], BG); + clitk::PointsUtils::Convert2DTo3D(p, S2, i, p3D); + B.push_back(p3D); + + } + + // Debug dmap + /* + typedef itk::Image FT; + FT::Pointer f = FT::New(); + typename FT::Pointer d1 = clitk::JoinSlices(dmaps1, S1, 2); + typename FT::Pointer d2 = clitk::JoinSlices(dmaps2, S2, 2); + writeImage(d1, "d1.mha"); + writeImage(d2, "d2.mha"); + */ + } + //-------------------------------------------------------------------- + + + //-------------------------------------------------------------------- + template + typename ImageType::PointType + ComputeClosestPoint(const ImageType * input, + const itk::Image * dmap, + typename ImageType::PixelType & BG) + { + // Loop dmap + S2, if FG, get min + typedef itk::Image FloatImageType; + typedef itk::ImageRegionConstIteratorWithIndex ImageIteratorType; + typedef itk::ImageRegionConstIterator DMapIteratorType; + ImageIteratorType iter1(input, input->GetLargestPossibleRegion()); + DMapIteratorType iter2(dmap, dmap->GetLargestPossibleRegion()); + + iter1.GoToBegin(); + iter2.GoToBegin(); + double dmin = 100000.0; + typename ImageType::IndexType indexmin; + while (!iter1.IsAtEnd()) { + if (iter1.Get() != BG) { + double d = iter2.Get(); + if (dTransformIndexToPhysicalPoint(indexmin, p); + return p; + } + //-------------------------------------------------------------------- + + + + } // end of namespace