Dear all,
I have a 3D volume, I want to iterate over all 2D slices, apply a convolutional filter on each slice and then put them again together as a 3D volume and store this 3D volume.
Somehow, I am failing to do so and I do not know what might be the problem. I wanted to iterate over the image slice by slice and consequently update the image. But it fails at the second slice/loop Thats what I have so far:
//create the convolutional kernel (that works, should be OK, it gives me a 3D image with size (5x5x1)
ImageType::Pointer kernel = ImageType::New();
CreateKernel(kernel)
using PasteFilterType = itk::PasteImageFilter<ImageType>;
//extract an image slice
using ExtractFilterType = itk::ExtractImageFilter<ImageType, ImageType>;
ExtractFilterType::Pointer extractFilter = ExtractFilterType::New();
extractFilter->SetDirectionCollapseToSubmatrix();
//image to change
ImageType * inputImage = image;
const typename ImageType::RegionType& imageRegion = image->GetLargestPossibleRegion();
const typename ImageType::SizeType& imageRegionSize = imageRegion.GetSize();
ImageType::RegionType inputRegion = inputImage->GetBufferedRegion();
ImageType::SizeType size = inputRegion.GetSize();
size[2] = 1; // we extract along z direction
ImageType::IndexType start = inputRegion.GetIndex();
//start at slice 0
unsigned int sliceNumber = 0;
//use convolutional filter
using FilterType = itk::ConvolutionImageFilter<ImageType>;
ImageType::RegionType desiredRegion;
FilterType::Pointer convolutionFilter = FilterType::New();
//iterate over all slices
for (int depth = 0; depth < imageRegionSize[2]; depth++) {
//set slice nr
sliceNumber = depth;
start[2] = sliceNumber;
//get 2D slice
desiredRegion.SetSize(size);
desiredRegion.SetIndex(start);
extractFilter->SetExtractionRegion(desiredRegion);
//paste filtered slice
PasteFilterType::Pointer pasteFilter = PasteFilterType::New();
extractFilter->SetInput(inputImage);
//convolve image
convolutionFilter->SetInput(extractFilter->GetOutput());
convolutionFilter->SetKernelImage(kernel);
//insert filtered slice
pasteFilter->SetSourceImage(convolutionFilter->GetOutput());
pasteFilter->SetDestinationImage(inputImage);
pasteFilter->SetDestinationIndex(start);
pasteFilter->SetSourceRegion(convolutionFilter->GetOutput()->GetBufferedRegion());
pasteFilter->Update();
inputImage= pasteFilter->GetOutput();
}
writer->SetFileName("E:/IBSI2/ibsi_2_digital_phantom/gabor1.nii");
writer->SetInput(inputImage);
writer->Update();
}
Thanks a lot for the help!