How to use Image Iterators in right way

Hello everyone,

i have noticed if i use for-loop across the image to get the every pixel value is time-consuming and inefficient,and that is also be mentioned in the user guide.
I read this page
but i still dont know how to get the pixel value and get its coordinates at the same time with using iterator.
Here is my code how can i rewrite them with using iterator.

ImageType::IndexType pixcelIndex;
int image_width = RegionInFilter.GetSize()[0];
int image_height = RegionInFilter.GetSize()[1];
int image_slice = RegionInFilter.GetSize()[2];
for (int k = 0; k < image_slice; ++k) //
for (int i = 0; i < image_height; ++i)//
for (int j = 0; j < image_width; ++j)//
pixcelIndex[0] = j;//
pixcelIndex[1] = i;//
pixcelIndex[2] = k;//
ImageType::PixelType pixel1 = image->GetPixel(pixcelIndex);
if (pixel1 !=0 ) {
slicecoordinates .push_back(pixcelIndex[2]);//
Ycoordinates .push_back(pixcelIndex[1]);//
Xcoordinates .push_back(pixcelIndex[0]);//

now the code is running good but also use too much memory so I want to optimize it .
Thanks in advance!!

it.GetIndex() gives the index, e.g.:


Thank you!! And i want to know how apply image filter on dicom series? All example is base on single image .Now i read a dicom series and i want to do the same processing on every slice,for example opening operation. But it didn`t work. i want to konw how to apply it in right way.

using ImageType = itk::Image< PixelType, 3>;
using FilterType = itk::OtsuThresholdImageFilter<
	ImageType, ImageType >;

FilterType::Pointer filter = FilterType::New();


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<ImageType::PixelType, ImageType::ImageDimension>;
StructuringElementType structuringElement;
  using BinaryMorphologicalOpeningImageFilterType =
	itk::BinaryMorphologicalOpeningImageFilter<ImageType, ImageType, StructuringElementType>;
BinaryMorphologicalOpeningImageFilterType::Pointer openingFilter = BinaryMorphologicalOpeningImageFilterType::New();
catch (itk::ExceptionObject & excp)
	std::cerr << "Exception thrown " << excp << std::endl;

Here is the code ,the otsu part works but the opening part does not work, do you have any advice?

Here is my rewrite code ,now program is faster than before .

using IteratorType = itk::ImageRegionIteratorWithIndex<ImageType>;

IteratorType outputIt(image, image->GetLargestPossibleRegion());

for (outputIt.GoToBegin(); !outputIt.IsAtEnd(); ++outputIt)
	ImageType::IndexType idx = outputIt.GetIndex();
	//idx[0] = requestedIndex[0] + requestedSize[0] - 1 - idx[0];
	if (outputIt.Get() != 0) {
		Ycoordinates .push_back(idx[1]);//
		Xcoordinates .push_back(idx[0]);//

I love Iterator

This might need to be openingFilter->SetInput(filter->GetOutput());

Oh sorry there is my copy mistake. Because I use a lot of filters.I pick out some of the code for the sake of simplicity.
It is indeed openingFilter->SetInput(filter->GetOutput()) in my code;
every single filter does not work except OtsuThresholdImageFilter . I have tried to apply many Morphological operation ,opening closing,Dilate… But the image hasn’t been changed.
also i have changed structuringElement.SetRadius(15); biger or smaller it does not help neither.
What’s special is that my data is a dicom series, so I think something should be changed, but I’m not sure where.

That problem is unrelated to iterators. Please start a new topic, preferably providing a minimal complete example (with just one filter) which can be copy-pasted for easy reproduction.

Thank you ! right that is unrelated to iterators,and my iterator problem is fixed .
i will start a new topic to ask filter problem