# How to use Image filter on series images

**URL:** https://discourse.itk.org/t/how-to-use-image-filter-on-series-images/3184
**Category:** Beginner Questions
**Created:** [June 13, 2020, 8:17pm UTC](https://discourse.itk.org/t/how-to-use-image-filter-on-series-images/3184 "2020-06-13T20:17:32Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Amnesie](https://discourse.itk.org/user_avatar/discourse.itk.org/amnesie/32/1341_2.png) [@Amnesie](https://discourse.itk.org/u/Amnesie)
#### Post date: [June 13, 2020, 8:17pm UTC](https://discourse.itk.org/t/how-to-use-image-filter-on-series-images/3184/1 "2020-06-13T20:17:32Z")

</div>

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.

 ![o1](https://discourse.itk.org/uploads/default/original/2X/e/ebfc5a37a85b4f8dcd5736d9b41a07a5ce227c8f.png)

![w1](https://discourse.itk.org/uploads/default/original/2X/7/7d286dc0c87ba7f9c17cb5f9841a344a25c65367.png)  
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.

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [June 14, 2020, 3:47pm UTC](https://discourse.itk.org/t/how-to-use-image-filter-on-series-images/3184/2 "2020-06-14T15:47:21Z")

</div>

If you want to apply an operation to each slice independently (and not in a 3D fashion), you could use [SliceBySliceImageFilter](https://itk.org/Doxygen/html/classitk_1_1SliceBySliceImageFilter.html).

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

```auto
read all slices
process 3D
visualize

```

could be changed to:

```auto
for all slices:
  read slice
  process 2D
  visualize
```

---

<div class="post-metadata">

### Author: ![Amnesie](https://discourse.itk.org/user_avatar/discourse.itk.org/amnesie/32/1341_2.png) [@Amnesie](https://discourse.itk.org/u/Amnesie)
#### Post date: [June 16, 2020, 1:16pm UTC](https://discourse.itk.org/t/how-to-use-image-filter-on-series-images/3184/3 "2020-06-16T13:16:31Z")

</div>

> [@dzenanz](#):
>
> [SliceBySliceImageFilter](https://itk.org/Doxygen/html/classitk_1_1SliceBySliceImageFilter.html).

That is what i want!! Thank you again!
