# How to use Image Iterators in right way

**URL:** <https://discourse.itk.org/t/how-to-use-image-iterators-in-right-way/3171>\
**Category:** Beginner Questions\
**Created:** [June 12, 2020, 12:41am UTC](https://discourse.itk.org/t/how-to-use-image-iterators-in-right-way/3171 "2020-06-12T00:41:08Z")\
**Posts on this page:** 8\
**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 12, 2020, 12:41am UTC](https://discourse.itk.org/t/how-to-use-image-iterators-in-right-way/3171/1 "2020-06-12T00:41:08Z")

</div>

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 [https://itk.org/Doxygen50/html/ImageIteratorsPage.html](https://itk.org/Doxygen50/html/ImageIteratorsPage.html)  
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!!

---

<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 12, 2020, 12:57am UTC](https://discourse.itk.org/t/how-to-use-image-iterators-in-right-way/3171/2 "2020-06-12T00:57:32Z")

</div>

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

> <https://github.com/InsightSoftwareConsortium/ITK/blob/d9e73e9a8b447cb8f3a19301fbfeaa141fa98b91/Examples/Iterators/ImageRegionIteratorWithIndex.cxx#L146>

---

<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 12, 2020, 3:56pm UTC](https://discourse.itk.org/t/how-to-use-image-iterators-in-right-way/3171/3 "2020-06-12T15:56:51Z")

</div>

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();

filter->SetInput(reader->GetOutput());

try
{
	filter->Update();
}
catch (itk::ExceptionObject & excp)
{
	std::cerr << "Exception thrown " << excp << std::endl;
}
filter->SetOutsideValue(0);
filter->SetInsideValue(1000);
int threshold = filter->GetThreshold();
std::cout << "Threshold = " << threshold << std::endl;
//////////////////
using StructuringElementType = itk::BinaryBallStructuringElement<ImageType::PixelType, ImageType::ImageDimension>;
StructuringElementType structuringElement;
structuringElement.SetRadius(15);
structuringElement.CreateStructuringElement();
  using BinaryMorphologicalOpeningImageFilterType =
	itk::BinaryMorphologicalOpeningImageFilter<ImageType, ImageType, StructuringElementType>;
BinaryMorphologicalOpeningImageFilterType::Pointer openingFilter = BinaryMorphologicalOpeningImageFilterType::New();
openingFilter->SetInput(mathOperationFilter->GetOutput());
openingFilter->SetKernel(structuringElement);
try
{
	openingFilter->Update();
}
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?

---

<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 12, 2020, 4:54pm UTC](https://discourse.itk.org/t/how-to-use-image-iterators-in-right-way/3171/4 "2020-06-12T16:54:37Z")

</div>

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) {
	
		slicecoordinates.push_back(idx[2]);//
		Ycoordinates .push_back(idx[1]);//
		Xcoordinates .push_back(idx[0]);//
	
}

```

I love Iterator

---

<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 12, 2020, 5:19pm UTC](https://discourse.itk.org/t/how-to-use-image-iterators-in-right-way/3171/5 "2020-06-12T17:19:32Z")

</div>

> [@Amnesie](#):
>
> `openingFilter->SetInput(mathOperationFilter->GetOutput());`

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

---

<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 12, 2020, 8:32pm UTC](https://discourse.itk.org/t/how-to-use-image-iterators-in-right-way/3171/6 "2020-06-12T20:32:34Z")

</div>

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.

---

<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 12, 2020, 9:02pm UTC](https://discourse.itk.org/t/how-to-use-image-iterators-in-right-way/3171/7 "2020-06-12T21:02:56Z")

</div>

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.

---

<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, 2:06pm UTC](https://discourse.itk.org/t/how-to-use-image-iterators-in-right-way/3171/8 "2020-06-13T14:06:48Z")

</div>

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