From 4ad12936dd561ea34869367d66f839372c341eb2 Mon Sep 17 00:00:00 2001 From: David Sarrut Date: Fri, 18 Nov 2011 16:58:25 +0100 Subject: [PATCH] Change behavior : resize the image according to their union area (not like input). --- ...litkSliceBySliceRelativePositionFilter.txx | 101 ++++++++++++------ 1 file changed, 67 insertions(+), 34 deletions(-) diff --git a/itk/clitkSliceBySliceRelativePositionFilter.txx b/itk/clitkSliceBySliceRelativePositionFilter.txx index 0df2987..449ffff 100644 --- a/itk/clitkSliceBySliceRelativePositionFilter.txx +++ b/itk/clitkSliceBySliceRelativePositionFilter.txx @@ -127,9 +127,13 @@ GenerateOutputInformation() PrintOptions(); } + if (this->GetFuzzyMapOnlyFlag()) this->ComputeFuzzyMapFlagOn(); + // Get input pointer input = dynamic_cast(itk::ProcessObject::GetInput(0)); object = dynamic_cast(itk::ProcessObject::GetInput(1)); + m_working_object = object; + m_working_input = input; //-------------------------------------------------------------------- // Resample object to the same spacing than input @@ -138,36 +142,60 @@ GenerateOutputInformation() m_working_object = clitk::ResampleImageSpacing(object, input->GetSpacing()); this->template StopCurrentStep(m_working_object); } - else { - m_working_object = object; - } //-------------------------------------------------------------------- - // Pad object to the same size than input + // Resize image according to common area (except in Z) if (!clitk::HaveSameSizeAndSpacing(m_working_object, input)) { + this->StartNewStep("Resize images (union in XY and like input in Z)"); + + /* OLD STUFF this->StartNewStep("Pad object to the same size than input"); m_working_object = clitk::ResizeImageLike(m_working_object, - input, - this->GetObjectBackgroundValue()); + input, + this->GetObjectBackgroundValue()); this->template StopCurrentStep(m_working_object); + */ + + // Compute union of bounding boxes in X and Y + static const unsigned int dim = ImageType::ImageDimension; + typedef itk::BoundingBox BBType; + typename BBType::Pointer bb1 = BBType::New(); + ComputeBBFromImageRegion(m_working_object, m_working_object->GetLargestPossibleRegion(), bb1); + typename BBType::Pointer bb2 = BBType::New(); + ComputeBBFromImageRegion(input, input->GetLargestPossibleRegion(), bb2); + typename BBType::Pointer bbo = BBType::New(); + ComputeBBUnion(bbo, bb1, bb2); + + //We set Z BB like input + typename ImageType::PointType maxs = bbo->GetMaximum(); + typename ImageType::PointType mins = bbo->GetMinimum(); + maxs[2] = bb2->GetMaximum()[2]; + mins[2] = bb2->GetMinimum()[2]; + bbo->SetMaximum(maxs); + bbo->SetMinimum(mins); + + // Crop + m_working_input = clitk::ResizeImageLike(input, bbo, this->GetBackgroundValue()); + m_working_object = clitk::ResizeImageLike(m_working_object, + m_working_input, + this->GetObjectBackgroundValue()); + this->template StopCurrentStep(m_working_input); } - else { - } - - /* + + //-------------------------------------------------------------------- + /* Steps : - extract vector of slices in input, in object - slice by slice rel position - joint result - post process */ - //-------------------------------------------------------------------- // Extract input slices this->StartNewStep("Extract input slices"); typedef clitk::ExtractSliceFilter ExtractSliceFilterType; typename ExtractSliceFilterType::Pointer extractSliceFilter = ExtractSliceFilterType::New(); - extractSliceFilter->SetInput(input); + extractSliceFilter->SetInput(m_working_input); extractSliceFilter->SetDirection(GetDirection()); extractSliceFilter->Update(); typedef typename ExtractSliceFilterType::SliceType SliceType; @@ -179,7 +207,7 @@ GenerateOutputInformation() // Extract object slices this->StartNewStep("Extract object slices"); extractSliceFilter = ExtractSliceFilterType::New(); - extractSliceFilter->SetInput(m_working_object);//object); + extractSliceFilter->SetInput(m_working_object); extractSliceFilter->SetDirection(GetDirection()); extractSliceFilter->Update(); std::vector mObjectSlices; @@ -200,18 +228,18 @@ GenerateOutputInformation() int nb=0; mObjectSlices[i] = LabelizeAndCountNumberOfObjects(mObjectSlices[i], 0, true, 1, nb); - // If no object and empty slices : - if ((nb==0) && (this->GetFuzzyMapOnlyFlag())) { + // If no object and empty slices and if we need the full fuzzy map, create a dummy one. + if ((nb==0) && (this->GetComputeFuzzyMapFlag())) { typename FloatSliceType::Pointer one = FloatSliceType::New(); one->CopyInformation(mObjectSlices[0]); one->SetRegions(mObjectSlices[0]->GetLargestPossibleRegion()); one->Allocate(); one->FillBuffer(2.0); mFuzzyMapSlices[i] = one; - } + } // End nb==0 && GetComputeFuzzyMapFlag else { if ((!GetIgnoreEmptySliceObjectFlag()) || (nb!=0)) { - + // Select or not a single CCL ? if (GetUseTheLargestObjectCCLFlag()) { mObjectSlices[i] = KeepLabels(mObjectSlices[i], 0, 1, 1, 1, true); @@ -256,20 +284,20 @@ GenerateOutputInformation() relPosFilter->VerboseStepFlagOff(); relPosFilter->WriteStepFlagOff(); + // relPosFilter->VerboseMemoryFlagOn(); + relPosFilter->SetCurrentStepBaseId(this->GetCurrentStepId()+"-"+toString(i)); + relPosFilter->SetBackgroundValue(this->GetBackgroundValue()); relPosFilter->SetInput(mInputSlices[i]); relPosFilter->SetInputObject(mObjectSlices[i]); relPosFilter->SetRemoveObjectFlag(this->GetRemoveObjectFlag()); + // This flag (InverseOrientation) *must* be set before // AddOrientation because AddOrientation can change it. relPosFilter->SetInverseOrientationFlag(this->GetInverseOrientationFlag()); for(int j=0; jGetNumberOfAngles(); j++) { - // relPosFilter->AddOrientationTypeString(this->GetOrientationTypeString(j)); relPosFilter->AddAnglesInRad(this->GetAngle1InRad(j), this->GetAngle2InRad(j)); - // DD(this->GetOrientationTypeString(j)); } - // DD(this->GetInverseOrientationFlag()); - //relPosFilter->SetOrientationType(this->GetOrientationType()); relPosFilter->SetIntermediateSpacing(this->GetIntermediateSpacing()); relPosFilter->SetIntermediateSpacingFlag(this->GetIntermediateSpacingFlag()); relPosFilter->SetFuzzyThreshold(this->GetFuzzyThreshold()); @@ -278,26 +306,29 @@ GenerateOutputInformation() // should we stop after fuzzy map ? relPosFilter->SetFuzzyMapOnlyFlag(this->GetFuzzyMapOnlyFlag()); + relPosFilter->SetComputeFuzzyMapFlag(this->GetComputeFuzzyMapFlag()); // Go ! relPosFilter->Update(); // If we stop after the fuzzy map, store the fuzzy slices - if (this->GetFuzzyMapOnlyFlag()) { + if (this->GetComputeFuzzyMapFlag()) { mFuzzyMapSlices[i] = relPosFilter->GetFuzzyMap(); // writeImage(mFuzzyMapSlices[i], "slice_"+toString(i)+".mha"); } - else { + + // Set input slices + if (!this->GetFuzzyMapOnlyFlag()) { mInputSlices[i] = relPosFilter->GetOutput(); // Select main CC if needed if (GetUniqueConnectedComponentBySliceFlag()) { 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()) { @@ -307,20 +338,22 @@ GenerateOutputInformation() ComputeCentroids } */ - } - } - // Join the fuzzy map if needed - if (this->GetFuzzyMapOnlyFlag()) { - this->m_FuzzyMap = clitk::JoinSlices(mFuzzyMapSlices, input, GetDirection()); - this->template StopCurrentStep(this->m_FuzzyMap); - return; - } + } // End nb!=0 || GetComputeFuzzyMapFlagOFF + + } // end for i mInputSlices // Join the slices - m_working_input = clitk::JoinSlices(mInputSlices, input, GetDirection()); + m_working_input = clitk::JoinSlices(mInputSlices, m_working_input, GetDirection()); this->template StopCurrentStep(m_working_input); + // Join the fuzzy map if needed + if (this->GetComputeFuzzyMapFlag()) { + this->m_FuzzyMap = clitk::JoinSlices(mFuzzyMapSlices, m_working_input, GetDirection()); + this->template StopCurrentStep(this->m_FuzzyMap); + if (this->GetFuzzyMapOnlyFlag()) return; + } + //-------------------------------------------------------------------- // Step 7: autocrop if (this->GetAutoCropFlag()) { -- 2.47.1