How to use Image filter on series images

Hallo everyone

All example i can found about Morphological Image filter are base on single image. I tried some of them it can work on single image of my dataset.

But, Now i import a dicom series and i want to do the same processing on every slice,But it didn`t work. Or can say it run in bad way ,the outputimage is strange.
Here is my image after segmentation,you can see the edge of object is rough. so i want to simplely use opening opreation to smooth the edge. But when I used it, the image got weird.

w1
Code part:

    const unsigned int Dimension = 3;
using PixelType = unsigned short;
using ImageType = itk::Image< PixelType, Dimension >;
using RealPixelType = unsigned char;
using BinarImageType = itk::Image< RealPixelType, Dimension>;
using ReaderType = itk::ImageSeriesReader< ImageType >;
using ImageIOType = itk::GDCMImageIO;
using InputNamesGeneratorType = itk::GDCMSeriesFileNames;

ImageIOType::Pointer gdcmImageIO = ImageIOType::New();
ReaderType::Pointer reader = ReaderType::New();
InputNamesGeneratorType::Pointer inputNames = InputNamesGeneratorType::New();
inputNames->SetUseSeriesDetails(true);
inputNames->SetGlobalWarningDisplay(false);
inputNames->SetInputDirectory(InputPath);
    InputPath=""
const ReaderType::FileNamesContainer & filenames =
inputNames->GetInputFileNames();
reader->SetImageIO(gdcmImageIO);
reader->SetFileNames(filenames);

std::cout << "The input series in directory " << InputPath
	<< " has " << filenames.size() << " files" << std::endl;

try
{
	reader->Update();
}
catch (itk::ExceptionObject &excp)
{
	std::cerr << "Exception thrown while reading the series" << std::endl;
	std::cerr << excp << std::endl;
	return EXIT_FAILURE;
}
if (InputPath !="") {
	std::cout << "Path:" << InputPath;
}

//////
using FilterType = itk::OtsuThresholdImageFilter<ImageType, ImageType >;
FilterType::Pointer filter = FilterType::New();
filter->SetInput(reader->GetOutput());
try
{
	filter->Update();
}
catch (itk::ExceptionObject & excp)
{
	std::cerr << "Exception thrown " << excp << std::endl;
}
int threshold = filter->GetThreshold();
std::cout << "Threshold = " << threshold << std::endl;
//////////////////
using StructuringElementType = itk::BinaryBallStructuringElement<RealPixelType, 3>;
StructuringElementType structuringElement;
structuringElement.SetRadius(1);
structuringElement.CreateStructuringElement();

typedef itk::CastImageFilter< ImageType, BinarImageType >    CastToRealFilterType;
typedef itk::RescaleIntensityImageFilter< BinarImageType, ImageType > RescaleFilterType;
CastToRealFilterType::Pointer toReal = CastToRealFilterType::New();
RescaleFilterType::Pointer    rescale = RescaleFilterType::New();

toReal->SetInput(filter->GetOutput());
toReal->Update();

using BinaryMorphologicalOpeningImageFilterType =
	itk::BinaryMorphologicalOpeningImageFilter<BinarImageType, BinarImageType, StructuringElementType>;
BinaryMorphologicalOpeningImageFilterType::Pointer openingFilter = BinaryMorphologicalOpeningImageFilterType::New();
openingFilter->SetInput(toReal->GetOutput());
openingFilter->SetKernel(structuringElement);
openingFilter->Update();
rescale->SetInput(openingFilter->GetOutput());
rescale->Update();

  ...display in vtk

I think the reason is my StructuringElementType has 3 Dimension and then OpeningImageFilter will compute it in wrong way. but it not be allowed to change it to 2 Because my dataset has 3 Dimension.

i want to konw how to apply it in right way.Or how about treating each image layer separately?
This is very important to me. Thank you in advance.

If you want to apply an operation to each slice independently (and not in a 3D fashion), you could use SliceBySliceImageFilter.

Alternatively, have a for loop of 2D processing. Current pseudo-code:

read all slices
process 3D
visualize

could be changed to:

for all slices:
  read slice
  process 2D
  visualize
1 Like

That is what i want!! Thank you again!