X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=itk%2FclitkSliceBySliceRelativePositionFilter.txx;h=76b0fec9cb35281aa8b8860a1f30313c1902013a;hb=2da8484f233bcfd3378c4368933bddcd6fa16ae2;hp=bdffecb71bc295f1b9590e3b046a229634dfc31a;hpb=a89d8140714c44dd29d96f6482dd90c40ad175b4;p=clitk.git diff --git a/itk/clitkSliceBySliceRelativePositionFilter.txx b/itk/clitkSliceBySliceRelativePositionFilter.txx index bdffecb..76b0fec 100644 --- a/itk/clitkSliceBySliceRelativePositionFilter.txx +++ b/itk/clitkSliceBySliceRelativePositionFilter.txx @@ -28,18 +28,19 @@ template clitk::SliceBySliceRelativePositionFilter:: SliceBySliceRelativePositionFilter(): - clitk::FilterBase(), - itk::ImageToImageFilter() + clitk::AddRelativePositionConstraintToLabelImageFilter() { - this->SetNumberOfRequiredInputs(2); SetDirection(2); - SetObjectBackgroundValue(0); - SetFuzzyThreshold(0.6); - SetOrientationTypeString("Left"); - SetIntermediateSpacing(10); - ResampleBeforeRelativePositionFilterOff(); UniqueConnectedComponentBySliceOff(); - NotFlagOff(); + SetIgnoreEmptySliceObjectFlag(false); + UseASingleObjectConnectedComponentBySliceFlagOn(); + this->VerboseStepFlagOff(); + this->WriteStepFlagOff(); + this->SetCombineWithOrFlag(false); + CCLSelectionFlagOn(); + SetCCLSelectionDimension(0); + SetCCLSelectionDirection(1); + CCLSelectionIgnoreSingleCCLFlagOff(); } //-------------------------------------------------------------------- @@ -68,6 +69,27 @@ SetInputObject(const ImageType * image) //-------------------------------------------------------------------- +//-------------------------------------------------------------------- +template +void +clitk::SliceBySliceRelativePositionFilter:: +PrintOptions() +{ + DD(this->GetDirection()); + DD((int)this->GetObjectBackgroundValue()); + DDV(this->GetOrientationTypeString(), (uint)this->GetNumberOfAngles()); + DD(this->GetIntermediateSpacingFlag()); + DD(this->GetIntermediateSpacing()); + DD(this->GetFuzzyThreshold()); + DD(this->GetUniqueConnectedComponentBySlice()); + DD(this->GetAutoCropFlag()); + DD(this->GetInverseOrientationFlag()); + DD(this->GetRemoveObjectFlag()); + DD(this->GetCombineWithOrFlag()); +} +//-------------------------------------------------------------------- + + //-------------------------------------------------------------------- template void @@ -91,6 +113,10 @@ void clitk::SliceBySliceRelativePositionFilter:: GenerateOutputInformation() { + if (this->GetVerboseOptionFlag()) { + PrintOptions(); + } + // Get input pointer input = dynamic_cast(itk::ProcessObject::GetInput(0)); object = dynamic_cast(itk::ProcessObject::GetInput(1)); @@ -98,9 +124,9 @@ GenerateOutputInformation() //-------------------------------------------------------------------- // Resample object to the same spacing than input if (!clitk::HaveSameSpacing(object, input)) { - StartNewStep("Resample object to the same spacing than input"); + this->StartNewStep("Resample object to the same spacing than input"); m_working_object = clitk::ResampleImageSpacing(object, input->GetSpacing()); - StopCurrentStep(m_working_object); + this->template StopCurrentStep(m_working_object); } else { m_working_object = object; @@ -109,11 +135,11 @@ GenerateOutputInformation() //-------------------------------------------------------------------- // Pad object to the same size than input if (!clitk::HaveSameSizeAndSpacing(m_working_object, input)) { - StartNewStep("Pad object to the same size than input"); + this->StartNewStep("Pad object to the same size than input"); m_working_object = clitk::ResizeImageLike(m_working_object, - input, - GetObjectBackgroundValue()); - StopCurrentStep(m_working_object); + input, + this->GetObjectBackgroundValue()); + this->template StopCurrentStep(m_working_object); } else { } @@ -128,7 +154,7 @@ GenerateOutputInformation() //-------------------------------------------------------------------- // Extract input slices - StartNewStep("Extract input slices"); + this->StartNewStep("Extract input slices"); typedef clitk::ExtractSliceFilter ExtractSliceFilterType; typename ExtractSliceFilterType::Pointer extractSliceFilter = ExtractSliceFilterType::New(); extractSliceFilter->SetInput(input); @@ -137,74 +163,123 @@ GenerateOutputInformation() typedef typename ExtractSliceFilterType::SliceType SliceType; std::vector mInputSlices; extractSliceFilter->GetOutputSlices(mInputSlices); - StopCurrentStep(mInputSlices[0]); + this->template StopCurrentStep(mInputSlices[0]); //-------------------------------------------------------------------- // Extract object slices - StartNewStep("Extract object slices"); + this->StartNewStep("Extract object slices"); extractSliceFilter = ExtractSliceFilterType::New(); extractSliceFilter->SetInput(m_working_object);//object); extractSliceFilter->SetDirection(GetDirection()); extractSliceFilter->Update(); std::vector mObjectSlices; extractSliceFilter->GetOutputSlices(mObjectSlices); - StopCurrentStep(mObjectSlices[0]); + this->template StopCurrentStep(mObjectSlices[0]); //-------------------------------------------------------------------- // Perform slice by slice relative position - StartNewStep("Perform slice by slice relative position"); + this->StartNewStep("Perform slice by slice relative position"); for(unsigned int i=0; i(mObjectSlices[i], 0, true, 1); - mObjectSlices[i] = KeepLabels(mObjectSlices[i], 0, 1, 1, 1, true); - - // Relative position - typedef clitk::AddRelativePositionConstraintToLabelImageFilter RelPosFilterType; - typename RelPosFilterType::Pointer relPosFilter = RelPosFilterType::New(); - relPosFilter->VerboseStepOff(); - relPosFilter->WriteStepOff(); - relPosFilter->SetCurrentStepBaseId(this->GetCurrentStepId()); - relPosFilter->SetInput(mInputSlices[i]); - relPosFilter->SetInputObject(mObjectSlices[i]); - relPosFilter->SetNotFlag(GetNotFlag()); - relPosFilter->SetOrientationTypeString(this->GetOrientationTypeString()); - relPosFilter->SetIntermediateSpacing(this->GetIntermediateSpacing()); - relPosFilter->SetResampleBeforeRelativePositionFilter(this->GetResampleBeforeRelativePositionFilter()); - relPosFilter->SetFuzzyThreshold(this->GetFuzzyThreshold()); - relPosFilter->AutoCropFlagOff(); // important ! because we join the slices after this loop - relPosFilter->Update(); - mInputSlices[i] = relPosFilter->GetOutput(); - - // Select main CC if needed - if (GetUniqueConnectedComponentBySlice()) { - mInputSlices[i] = Labelize(mInputSlices[i], 0, true, 1); - mInputSlices[i] = KeepLabels(mInputSlices[i], 0, 1, 1, 1, true); - } + + // Count the number of CCL (allow to ignore empty slice) + int nb=0; + mObjectSlices[i] = LabelizeAndCountNumberOfObjects(mObjectSlices[i], 0, true, 1, nb); + if ((!GetIgnoreEmptySliceObjectFlag()) || (nb!=0)) { - } + // Select or not a single CCL ? + if (GetUseASingleObjectConnectedComponentBySliceFlag()) { + mObjectSlices[i] = KeepLabels(mObjectSlices[i], 0, 1, 1, 1, true); + } - typedef itk::JoinSeriesImageFilter JoinSeriesFilterType; - typename JoinSeriesFilterType::Pointer joinFilter = JoinSeriesFilterType::New(); - joinFilter->SetOrigin(input->GetOrigin()[GetDirection()]); - joinFilter->SetSpacing(input->GetSpacing()[GetDirection()]); - for(unsigned int i=0; iPushBackInput(mInputSlices[i]); + // Select a single according to a position if more than one CCL + if (GetCCLSelectionFlag()) { + // if several CCL, choose the most extrema according a direction, + // if not -> should we consider this slice ? + if (nb<2) { + if (GetCCLSelectionIgnoreSingleCCLFlag()) { + mObjectSlices[i] = SetBackground(mObjectSlices[i], mObjectSlices[i], + 1, this->GetBackgroundValue(), + true); + } + } + int dim = GetCCLSelectionDimension(); + int direction = GetCCLSelectionDirection(); + std::vector centroids; + ComputeCentroids(mObjectSlices[i], this->GetBackgroundValue(), centroids); + uint index=1; + for(uint j=1; j centroids[index][dim]) index = j; + } + else { + if (centroids[j][dim] < centroids[index][dim]) index = j; + } + } + for(uint v=1; v(mObjectSlices[i], mObjectSlices[i], + (char)v, this->GetBackgroundValue(), + true); + } + } + } + + // Relative position + typedef clitk::AddRelativePositionConstraintToLabelImageFilter RelPosFilterType; + typename RelPosFilterType::Pointer relPosFilter = RelPosFilterType::New(); + + relPosFilter->VerboseStepFlagOff(); + relPosFilter->WriteStepFlagOff(); + relPosFilter->SetBackgroundValue(this->GetBackgroundValue()); + relPosFilter->SetInput(mInputSlices[i]); + relPosFilter->SetInputObject(mObjectSlices[i]); + relPosFilter->SetRemoveObjectFlag(this->GetRemoveObjectFlag()); + for(int j=0; jGetNumberOfAngles(); j++) { + relPosFilter->AddOrientationTypeString(this->GetOrientationTypeString(j)); + } + relPosFilter->SetInverseOrientationFlag(this->GetInverseOrientationFlag()); + //relPosFilter->SetOrientationType(this->GetOrientationType()); + relPosFilter->SetIntermediateSpacing(this->GetIntermediateSpacing()); + relPosFilter->SetIntermediateSpacingFlag(this->GetIntermediateSpacingFlag()); + relPosFilter->SetFuzzyThreshold(this->GetFuzzyThreshold()); + relPosFilter->AutoCropFlagOff(); // important ! because we join the slices after this loop + relPosFilter->SetCombineWithOrFlag(this->GetCombineWithOrFlag()); + relPosFilter->Update(); + mInputSlices[i] = relPosFilter->GetOutput(); + + // Select main CC if needed + if (GetUniqueConnectedComponentBySlice()) { + mInputSlices[i] = Labelize(mInputSlices[i], 0, true, 1); + mInputSlices[i] = KeepLabels(mInputSlices[i], 0, 1, 1, 1, true); + } + + /* + // Select unique CC according to the most in a given direction + if (GetUniqueConnectedComponentBySliceAccordingToADirection()) { + int nb; + mInputSlices[i] = LabelizeAndCountNumberOfObjects(mInputSlices[i], 0, true, 1, nb); + std::vector & centroids; + ComputeCentroids + } + */ + } } - joinFilter->Update(); - m_working_input = joinFilter->GetOutput(); - StopCurrentStep(m_working_input); + + // Join the slices + m_working_input = clitk::JoinSlices(mInputSlices, input, GetDirection()); + this->template StopCurrentStep(m_working_input); //-------------------------------------------------------------------- // Step 7: autocrop - if (GetAutoCropFlag()) { - StartNewStep("Final AutoCrop"); + if (this->GetAutoCropFlag()) { + this->StartNewStep("Final AutoCrop"); typedef clitk::AutoCropFilter CropFilterType; typename CropFilterType::Pointer cropFilter = CropFilterType::New(); cropFilter->SetInput(m_working_input); cropFilter->ReleaseDataFlagOff(); cropFilter->Update(); m_working_input = cropFilter->GetOutput(); - StopCurrentStep(m_working_input); + this->template StopCurrentStep(m_working_input); } // Update output info