Thank you for the answer.
I have tried the first advice:
auto mask = ITKImgLabelType2D::New();
ITKImgLabelType2D::RegionType region = a_pmask->GetLargestPossibleRegion();
mask->SetRegions(region);
mask->Allocate();
ITKImgLabelType2D::SizeType regionSize = region.GetSize();
itk::ImageRegionIterator imageIterator(mask, region);
while (!imageIterator.IsAtEnd())
{
imageIterator.Set(itk::NumericTraits::max());
++imageIterator;
}
// We create the filter mask
using MaskFilterType = itk::MaskImageFilter<ITKImgLabelType2D, ITKImgLabelType2D, ITKImgLabelType2D>;
MaskFilterType::Pointer maskFilter3D = MaskFilterType::New();
maskFilter3D->SetInput(a_pmask);
maskFilter3D->SetMaskImage(mask);
// Apply a 2D mask to every slice of a 3D image
typedef itk::SliceBySliceImageFilter< ITKImgInType, ITKImgLabelType, MaskFilterType, MaskFilterType, ITKImgLabelType2D, ITKImgLabelType2D > SliceBySliceImageFilter;
SliceBySliceImageFilter::Pointer sliceBySliceFilter = SliceBySliceImageFilter::New();
sliceBySliceFilter->SetFilter(maskFilter3D);
sliceBySliceFilter->SetInput(1, &a_pimage3D);
And I get another error, which is:
itk::ERROR: SliceBySliceImageFilter(0000019653A533C0): Input Primary is required but not set.
I have tried to construct a 3D mask using TileImageFilter
, and use 3D variant of MaskImageFilter
.
Here is my code:
constexpr unsigned int InputDimension = 2;
constexpr unsigned int OutputDimension = 3;
using PixelType = unsigned char;
using InputImageType = itk::Image<PixelType, InputDimension>;
using OutputImageType = itk::Image<PixelType, OutputDimension>;
using FilterType = itk::TileImageFilter<InputImageType, OutputImageType>;
auto filter = FilterType::New();
itk::FixedArray<unsigned int, OutputDimension> layout;
layout[0] = 2;
layout[1] = 2;
layout[2] = 0;
filter->SetLayout(layout);
ITKImgInType::SizeType insize = a_pimage3D.GetRequestedRegion().GetSize();
int nImages = insize[2];
for (int ii = 0; ii <= nImages - 1; ++ii)
{
InputImageType::Pointer input = a_pmask;
filter->SetInput(ii, input);
}
constexpr PixelType defaultValue = 128;
filter->SetDefaultPixelValue(defaultValue);
filter->GetOutput()->SetSpacing(a_pimage3D.GetSpacing());
filter->GetOutput()->SetOrigin(a_pimage3D.GetOrigin());
filter->GetOutput()->SetDirection(a_pimage3D.GetDirection());
if (f_verbose)
{
a_file << "Spacing 3D mask after: " << filter->GetOutput()->GetSpacing() << std::endl;
a_file << "Spacing 3D image: " << a_pimage3D.GetSpacing() << std::endl << std::endl;
a_file << "Origin 3D mask after: " << filter->GetOutput()->GetOrigin() << std::endl;
a_file << "Origin 3D image: " << a_pimage3D.GetOrigin() << std::endl << std::endl;
}
// Apply a 3D mask to a 3D image
using MaskFilterType = itk::MaskImageFilter<ITKImgInType, ITKImgLabelType, ITKImgLabelType>;
MaskFilterType::Pointer maskFilter3D = MaskFilterType::New();
maskFilter3D->SetInput(&a_pimage3D);
maskFilter3D->SetMaskImage(filter->GetOutput());
try
{
maskFilter3D->Update();
}
catch (const itk::ExceptionObject& err)
I donāt understand whatās wrong, because if I print the origin and the spacing of the a_image3D and the filter, as you can see in my code, I get the following information:
Spacing 3D mask after: [0.847656, 0.847656, 2.98663]
Spacing 3D image: [0.847656, 0.847656, 2.98663]
Origin 3D mask after: [-208.208, -251.595, 1148.21]
Origin 3D image: [-208.208, -251.595, 1148.21]
So, the spacing and the origin is the same. But then, when I execute maskFilter3D->Update(); I get the following error:
ExceptionObject caught !Alma::SegmentationLib::ApplyMask3D
C:\Dev\ITK\Modules\Core\Common\include\itkImageToImageFilter.hxx:248:
itk::ERROR: MaskImageFilter(00000231EBE04390): Inputs do not occupy the same physical space!
InputImage Origin: [-2.0820781e+02, -2.5159518e+02, 1.1482096e+03], InputImage_1 Origin: [-2.0820781e+02, -2.5159518e+02, 0.0000000e+00]
Tolerance: 8.4765559e-07
InputImage Spacing: [8.4765559e-01, 8.4765559e-01, 2.9866300e+00], InputImage_1 Spacing: [8.4765559e-01, 8.4765559e-01, 1.0000000e+00]
Tolerance: 8.4765559e-07
What should I do?
Thank you very much,
Cristina