X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=itk%2FclitkAddRelativePositionConstraintToLabelImageFilter.txx;h=54f431109728bd8b31b4bdbd8b5d09cc9679e441;hb=4fdccd07e42ee7c089cbcadf854e3a505c17bcfb;hp=5e7011ad37f861ef900a7ecf6d083378f657261f;hpb=38786c4da19b87319bbe3cecc145e3d1771d10da;p=clitk.git diff --git a/itk/clitkAddRelativePositionConstraintToLabelImageFilter.txx b/itk/clitkAddRelativePositionConstraintToLabelImageFilter.txx index 5e7011a..54f4311 100644 --- a/itk/clitkAddRelativePositionConstraintToLabelImageFilter.txx +++ b/itk/clitkAddRelativePositionConstraintToLabelImageFilter.txx @@ -3,7 +3,7 @@ Authors belong to: - University of LYON http://www.universite-lyon.fr/ - - Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr + - Léon Bérard cancer center http://www.centreleonberard.fr - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr This software is distributed WITHOUT ANY WARRANTY; without even @@ -14,7 +14,7 @@ - BSD See included LICENSE.txt file - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html - ======================================================================-====*/ + ===========================================================================**/ // clitk #include "clitkCommon.h" @@ -25,12 +25,18 @@ // itk #include -#include "itkStatisticsLabelMapFilter.h" -#include "itkLabelImageToStatisticsLabelMapFilter.h" -#include "itkRegionOfInterestImageFilter.h" -#include "itkBinaryThresholdImageFilter.h" -#include "itkBinaryErodeImageFilter.h" -#include "itkBinaryBallStructuringElement.h" +#include +#include +#include +#include +#include +#include +#include +#if ITK_VERSION_MAJOR >= 4 + #include +#else + #include +#endif // itk [Bloch et al] #include "RelativePositionPropImageFilter.h" @@ -46,10 +52,15 @@ AddRelativePositionConstraintToLabelImageFilter(): SetFuzzyThreshold(0.6); SetBackgroundValue(0); SetObjectBackgroundValue(0); - SetOrientationType(LeftTo); - ResampleBeforeRelativePositionFilterOn(); + ClearOrientationType(); + IntermediateSpacingFlagOn(); SetIntermediateSpacing(10); - AutoCropOn(); + AutoCropFlagOn(); + InverseOrientationFlagOff(); + RemoveObjectFlagOn(); + CombineWithOrFlagOff(); + VerboseStepFlagOff(); + WriteStepFlagOff(); } //-------------------------------------------------------------------- @@ -78,6 +89,58 @@ SetInputObject(const ImageType * image) //-------------------------------------------------------------------- +//-------------------------------------------------------------------- +template +void +clitk::AddRelativePositionConstraintToLabelImageFilter:: +ClearOrientationType() +{ + m_OrientationTypeString.clear(); + m_OrientationType.clear(); + m_Angle1.clear(); + m_Angle2.clear(); +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +template +int +clitk::AddRelativePositionConstraintToLabelImageFilter:: +GetNumberOfAngles() +{ + return m_OrientationType.size(); +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +template +void +clitk::AddRelativePositionConstraintToLabelImageFilter:: +AddOrientationTypeString(std::string t) +{ + m_OrientationTypeString.push_back(t); + switch (t[0]) { + case 'L' : AddOrientationType(AtLeftTo); break; + case 'R' : AddOrientationType(AtRightTo);break; + case 'A' : AddOrientationType(AntTo);break; + case 'P' : AddOrientationType(PostTo);break; + case 'S' : AddOrientationType(SupTo);break; + case 'I' : AddOrientationType(InfTo);break; + case 'N': + if (t == "NotLeftTo") { AddOrientationType(AtLeftTo); InverseOrientationFlagOn(); break; } + if (t == "NotRightTo") { AddOrientationType(AtRightTo); InverseOrientationFlagOn(); break; } + if (t == "NotAntTo") { AddOrientationType(AntTo); InverseOrientationFlagOn(); break; } + if (t == "NotPostTo") { AddOrientationType(PostTo); InverseOrientationFlagOn(); break; } + if (t == "NotSupTo") { AddOrientationType(SupTo); InverseOrientationFlagOn(); break; } + if (t == "NotInfTo") { AddOrientationType(InfTo); InverseOrientationFlagOn(); break; } + default: clitkExceptionMacro("Error, you must provide L,R or A,P or S,I (or NotLeftTo, NotRightTo etc)"); + } +} +//-------------------------------------------------------------------- + + //-------------------------------------------------------------------- template void @@ -112,22 +175,11 @@ GenerateInputRequestedRegion() template void clitk::AddRelativePositionConstraintToLabelImageFilter:: -SetAngle1(double a) -{ - SetOrientationType(Angle); - m_Angle1 = a; -} -//-------------------------------------------------------------------- - - -//-------------------------------------------------------------------- -template -void -clitk::AddRelativePositionConstraintToLabelImageFilter:: -SetAngle2(double a) +AddAngles(double a, double b) { - SetOrientationType(Angle); - m_Angle2 = a; + AddOrientationTypeString("Angle"); + m_Angle1.push_back(a); + m_Angle2.push_back(b); } //-------------------------------------------------------------------- @@ -136,17 +188,35 @@ SetAngle2(double a) template void clitk::AddRelativePositionConstraintToLabelImageFilter:: -SetOrientationType(OrientationTypeEnumeration orientation) +AddOrientationType(OrientationTypeEnumeration orientation) { - m_OrientationType = orientation; - switch (m_OrientationType) { - case LeftTo: m_Angle1 = clitk::deg2rad(0); m_Angle2 = clitk::deg2rad(0); break; - case RightTo: m_Angle1 = clitk::deg2rad(180); m_Angle2 = clitk::deg2rad(0); break; - case AntTo: m_Angle1 = clitk::deg2rad(90); m_Angle2 = clitk::deg2rad(0); break; - case PostTo: m_Angle1 = clitk::deg2rad(-90); m_Angle2 = clitk::deg2rad(0); break; - case InfTo: m_Angle1 = clitk::deg2rad(0); m_Angle2 = clitk::deg2rad(90); break; - case SupTo: m_Angle1 = clitk::deg2rad(0); m_Angle2 = clitk::deg2rad(-90); break; - case Angle: break; + m_OrientationType.push_back(orientation); + switch (orientation) { + case AtRightTo: + m_Angle1.push_back(clitk::deg2rad(0)); + m_Angle2.push_back(clitk::deg2rad(0)); + break; + case AtLeftTo: + m_Angle1.push_back(clitk::deg2rad(180)); + m_Angle2.push_back(clitk::deg2rad(0)); + break; + case AntTo: + m_Angle1.push_back(clitk::deg2rad(90)); + m_Angle2.push_back(clitk::deg2rad(0)); + break; + case PostTo: + m_Angle1.push_back(clitk::deg2rad(-90)); + m_Angle2.push_back(clitk::deg2rad(0)); + break; + case InfTo: + m_Angle1.push_back(clitk::deg2rad(0)); + m_Angle2.push_back(clitk::deg2rad(90)); + break; + case SupTo: + m_Angle1.push_back(clitk::deg2rad(0)); + m_Angle2.push_back(clitk::deg2rad(-90)); + break; + case Angle: break; } /* A1 A2 Left 0 0 @@ -166,79 +236,148 @@ void clitk::AddRelativePositionConstraintToLabelImageFilter:: GenerateData() { + if (GetNumberOfAngles() <1) { + clitkExceptionMacro("Add at least one orientation type"); + } + // Get input pointer input = dynamic_cast(itk::ProcessObject::GetInput(0)); object = dynamic_cast(itk::ProcessObject::GetInput(1)); + static const unsigned int dim = ImageType::ImageDimension; + + // Step 2: object pad to input image -> we want to compute the + // relative position for each point belonging to the input image + // domain, so we have to extend (pad) the object image to fit the + // domain size + working_image = object; + if (!clitk::HaveSameSizeAndSpacing(input, working_image)) { + StartNewStep("Pad (resize) object to input size"); + + if (0) { // OLD VERSION (TO REMOVE) + StartNewStep("Pad object to image size"); + typename ImageType::Pointer output = ImageType::New(); + SizeType size; + for(unsigned int i=0; iGetLargestPossibleRegion().GetSize()[i]* + input->GetSpacing()[i])/(double)working_image->GetSpacing()[i]); + } + + // The index of the input is not necessarily zero, so we have to + // take it into account (not done) + RegionType region; + IndexType index = input->GetLargestPossibleRegion().GetIndex(); + region.SetSize(size); + for(unsigned int i=0; iSetLargestPossibleRegion(region); + output->SetRegions(region); + output->SetSpacing(working_image->GetSpacing()); + PointType origin = input->GetOrigin(); + for(unsigned int i=0; iGetSpacing()[i] + input->GetOrigin()[i]; + } + output->SetOrigin(origin); + // output->SetOrigin(input->GetOrigin()); + + output->Allocate(); + output->FillBuffer(m_BackgroundValue); + typename PasteFilterType::Pointer padFilter = PasteFilterType::New(); + // typename PasteFilterType::InputImageIndexType index; + for(unsigned int i=0; iGetSpacing()[i]/(double)working_image->GetSpacing()[i] + + lrint((working_image->GetOrigin()[i] - input->GetOrigin()[i])/working_image->GetSpacing()[i]); + } + padFilter->SetSourceImage(working_image); + padFilter->SetDestinationImage(output); + padFilter->SetDestinationIndex(index); + padFilter->SetSourceRegion(working_image->GetLargestPossibleRegion()); + padFilter->Update(); + working_image = padFilter->GetOutput(); + } + + // Resize object like input + working_image = clitk::ResizeImageLike(working_image, input, GetBackgroundValue()); + StopCurrentStep(working_image); + } //-------------------------------------------------------------------- //-------------------------------------------------------------------- - static const unsigned int dim = ImageType::ImageDimension; - StartNewStep("Initial resample and pad"); // Step 1 : resample - if (m_ResampleBeforeRelativePositionFilter) { + if (m_IntermediateSpacingFlag) { + StartNewStep("Resample object to intermediate spacing"); typedef clitk::ResampleImageWithOptionsFilter ResampleFilterType; typename ResampleFilterType::Pointer resampleFilter = ResampleFilterType::New(); - resampleFilter->SetInput(object); + resampleFilter->SetInput(working_image); + resampleFilter->SetDefaultPixelValue(0); resampleFilter->SetOutputIsoSpacing(m_IntermediateSpacing); resampleFilter->SetGaussianFilteringEnabled(false); - // resampleFilter->SetVerboseOptions(true); + // resampleFilter->SetVerboseOptions(true); resampleFilter->Update(); working_image = resampleFilter->GetOutput(); - } - else { - working_image = object; + StopCurrentStep(working_image); } - // Step 2: object pad to input image -> we want to compute the - // relative position for each point belonging to the input image - // domain, so we have to extend (pad) the object image to fit the - // domain size - if (!clitk::HaveSameSizeAndSpacing(input, working_image)) { - typename ImageType::Pointer output = ImageType::New(); - SizeType size; - for(unsigned int i=0; iGetLargestPossibleRegion().GetSize()[i]*input->GetSpacing()[i])/(double)working_image->GetSpacing()[i]); - } - RegionType region; - region.SetSize(size); - // output->SetLargestPossibleRegion(region); - output->SetRegions(region); - output->SetSpacing(working_image->GetSpacing()); - output->SetOrigin(input->GetOrigin()); - output->Allocate(); - output->FillBuffer(m_BackgroundValue); - typename PadFilterType::Pointer padFilter = PadFilterType::New(); - typename PadFilterType::InputImageIndexType index; - for(unsigned int i=0; iGetOrigin()[i] - input->GetOrigin()[i])/working_image->GetSpacing()[i]); - } - padFilter->SetSourceImage(working_image); - padFilter->SetDestinationImage(output); - padFilter->SetDestinationIndex(index); - padFilter->SetSourceRegion(working_image->GetLargestPossibleRegion()); - padFilter->Update(); - working_image = padFilter->GetOutput(); - } - else { - // DD("[debug] RelPos : same size and spacing : no padding"); - } // Keep object image (with resampline and pad) object_resampled = working_image; - StopCurrentStep(working_image); // Step 3: compute rel pos in object StartNewStep("Relative Position Map"); typedef itk::RelativePositionPropImageFilter RelPosFilterType; - typename RelPosFilterType::Pointer relPosFilter = RelPosFilterType::New(); - relPosFilter->SetInput(working_image); - relPosFilter->SetAlpha1(m_Angle1); // xy plane - relPosFilter->SetAlpha2(m_Angle2); - relPosFilter->SetK1(M_PI/2.0); // Opening parameter, default = pi/2 - relPosFilter->SetFast(true); - relPosFilter->SetRadius(1); // seems sufficient in this cas - // relPosFilter->SetVerboseProgress(true); - relPosFilter->Update(); - relPos = relPosFilter->GetOutput(); + typename RelPosFilterType::Pointer relPosFilter; + + typename FloatImageType::Pointer m_FuzzyMap; + for(int i=0; iSetInput(working_image); + relPosFilter->SetAlpha1(m_Angle1[i]); // xy plane + relPosFilter->SetAlpha2(m_Angle2[i]); + relPosFilter->SetK1(M_PI/2.0); // Opening parameter, default = pi/2 + relPosFilter->SetFast(true); + relPosFilter->SetRadius(1); // seems sufficient in this case + // relPosFilter->SetVerboseProgress(true); + relPosFilter->Update(); + relPos = relPosFilter->GetOutput(); + + if (GetNumberOfAngles() != 1) { + // Creation of the first m_FuzzyMap + if (i==0) { + m_FuzzyMap = clitk::NewImageLike(relPos, true); + m_FuzzyMap->FillBuffer(0.0); + } + + // Add to current fuzzy map + typedef itk::AddImageFilter AddImageFilter; + typename AddImageFilter::Pointer addFilter = AddImageFilter::New(); + addFilter->SetInput1(m_FuzzyMap); + addFilter->SetInput2(relPos); + addFilter->Update(); + m_FuzzyMap = addFilter->GetOutput(); + } + else m_FuzzyMap = relPos; + } + + // Divide by the number of relpos + if (GetNumberOfAngles() != 1) { +#if ITK_VERSION_MAJOR >= 4 + typedef itk::DivideImageFilter DivideFilter; + typename DivideFilter::Pointer divideFilter = DivideFilter::New(); + divideFilter->SetConstant2(GetNumberOfAngles()); +#else + typedef itk::DivideByConstantImageFilter DivideFilter; + typename DivideFilter::Pointer divideFilter = DivideFilter::New(); + divideFilter->SetConstant(GetNumberOfAngles()); +#endif + divideFilter->SetInput(m_FuzzyMap); + divideFilter->Update(); + m_FuzzyMap = divideFilter->GetOutput(); + } + + relPos = m_FuzzyMap; StopCurrentStep(relPos); //-------------------------------------------------------------------- @@ -277,8 +416,8 @@ GenerateData() //-------------------------------------------------------------------- //-------------------------------------------------------------------- // Step 5: resample to initial spacing - if (m_ResampleBeforeRelativePositionFilter) { - StartNewStep("Resample to get the same sampling than input"); + if (m_IntermediateSpacingFlag) { + StartNewStep("Resample to come back to the same sampling than input"); typedef clitk::ResampleImageWithOptionsFilter ResampleFilterType; typename ResampleFilterType::Pointer resampleFilter = ResampleFilterType::New(); resampleFilter->SetDefaultPixelValue(m_BackgroundValue); @@ -288,20 +427,21 @@ GenerateData() // resampleFilter->SetVerboseOptions(true); resampleFilter->Update(); working_image = resampleFilter->GetOutput(); - StopCurrentStep(working_image); + StopCurrentStep(working_image); } //-------------------------------------------------------------------- //-------------------------------------------------------------------- // Pre Step 6: pad if not the same size : it can occur when downsample and upsample + //if (!HaveSameSizeAndSpacing(working_image, input)) { if (working_image->GetLargestPossibleRegion() != input->GetLargestPossibleRegion()) { StartNewStep("Pad to get the same size than input"); typename ImageType::Pointer temp = ImageType::New(); temp->CopyInformation(input); temp->SetRegions(input->GetLargestPossibleRegion()); // Do not forget !! temp->Allocate(); - temp->FillBuffer(m_BackgroundValue); - typename PadFilterType::Pointer padFilter2 = PadFilterType::New(); // if yes : redo relpos + temp->FillBuffer(m_BackgroundValue); + typename PasteFilterType::Pointer padFilter2 = PasteFilterType::New(); padFilter2->SetSourceImage(working_image); padFilter2->SetDestinationImage(temp); padFilter2->SetDestinationIndex(input->GetLargestPossibleRegion().GetIndex()); @@ -320,34 +460,59 @@ GenerateData() StartNewStep("Combine with initial input (boolean And)"); typedef clitk::BooleanOperatorLabelImageFilter BoolFilterType; typename BoolFilterType::Pointer combineFilter = BoolFilterType::New(); - writeImage(input, "i.mhd"); - writeImage(working_image, "w.mhd"); combineFilter->SetBackgroundValue(m_BackgroundValue); combineFilter->SetBackgroundValue1(m_BackgroundValue); combineFilter->SetBackgroundValue2(m_BackgroundValue); combineFilter->SetForegroundValue(m_BackgroundValue+1); combineFilter->SetInput1(input); combineFilter->SetInput2(working_image); - combineFilter->SetOperationType(BoolFilterType::And); - combineFilter->InPlaceOn(); + if (GetInverseOrientationFlag()) + combineFilter->SetOperationType(BoolFilterType::AndNot); + else { + if (GetCombineWithOrFlag()) + combineFilter->SetOperationType(BoolFilterType::Or); + else + combineFilter->SetOperationType(BoolFilterType::And); + } + combineFilter->InPlaceOff(); // Do not modify initial input (!) combineFilter->Update(); working_image = combineFilter->GetOutput(); - // writeImage(working_image, "res.mhd"); - - combineFilter = BoolFilterType::New(); - combineFilter->SetInput1(working_image); - combineFilter->SetInput2(object); - combineFilter->SetOperationType(BoolFilterType::AndNot); - combineFilter->InPlaceOn(); - combineFilter->Update(); - working_image = combineFilter->GetOutput(); + // Remove (if needed the object from the support) + if (GetRemoveObjectFlag()) { + combineFilter = BoolFilterType::New(); + combineFilter->SetInput1(working_image); + combineFilter->SetInput2(object); + combineFilter->SetOperationType(BoolFilterType::AndNot); + combineFilter->InPlaceOn(); + combineFilter->Update(); + working_image = combineFilter->GetOutput(); + } + // In the other case, we must *add* the initial object to keep it + // but not more than the initial support + else { + combineFilter = BoolFilterType::New(); + combineFilter->SetInput1(working_image); + combineFilter->SetInput2(object); + combineFilter->SetOperationType(BoolFilterType::Or); + combineFilter->InPlaceOn(); + combineFilter->Update(); + working_image = combineFilter->GetOutput(); // not needed because InPlaceOn ? + combineFilter = BoolFilterType::New(); + combineFilter->SetInput1(working_image); + combineFilter->SetInput2(input); + combineFilter->SetOperationType(BoolFilterType::And); + combineFilter->InPlaceOn(); + combineFilter->Update(); + working_image = combineFilter->GetOutput(); + } + StopCurrentStep(working_image); //-------------------------------------------------------------------- //-------------------------------------------------------------------- // Step 7: autocrop - if (GetAutoCrop()) { + if (GetAutoCropFlag()) { StartNewStep("Final AutoCrop"); typedef clitk::AutoCropFilter CropFilterType; typename CropFilterType::Pointer cropFilter = CropFilterType::New(); @@ -363,7 +528,7 @@ GenerateData() // Final Step -> set output this->SetNthOutput(0, working_image); - return; + // this->GraftOutput(working_image); } //--------------------------------------------------------------------