# can't iterate over simple image

**URL:** <https://discourse.itk.org/t/cant-iterate-over-simple-image/865>\
**Category:** Beginner Questions\
**Created:** [April 24, 2018, 7:33am UTC](https://discourse.itk.org/t/cant-iterate-over-simple-image/865 "2018-04-24T07:33:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![antoine.letouzey](https://discourse.itk.org/user_avatar/discourse.itk.org/antoine.letouzey/32/57_2.png) [@antoine.letouzey](https://discourse.itk.org/u/antoine.letouzey)\
**Post date:** [April 24, 2018, 7:33am UTC](https://discourse.itk.org/t/cant-iterate-over-simple-image/865/1 "2018-04-24T07:33:03Z")

</div>

I have a 3d boolean image and would like to compute the number of non zero voxels.

I wrote this simple code that is supposed to iterate over the whole image and update a counter variable.

```
using BoolImageType = itk::Image<bool, 3U>;
double computeVolumeFromBinaryImage(const BoolImageType::Pointer& a_Image)
{
	itk::ImageConstIterator<BoolImageType> it(a_Image, a_Image->GetRequestedRegion());
	it.GoToBegin();
	std::size_t numVoxels = 0;
	while (!it.IsAtEnd())
	{
		if (it.Get())
			numVoxels++;
		++it;
	}
	auto size = a_Image->GetSpacing();
	double voxelVolume = size[0] * size[1] * size[2];
	return voxelVolume * numVoxels;
}

```

But I get the following compiler error:  
`error C2675: unary '++' : 'itk::ImageConstIterator<BoolImageType>' does not define this operator or a conversion to a type acceptable to the predefined operator`  
This error points to `++it;`

I get the same error if I remove the custom typedef.  
I’m using visual studio if that helps.

Any idea ?

---

<div class="post-metadata">

**Author:** ![antoine.letouzey](https://discourse.itk.org/user_avatar/discourse.itk.org/antoine.letouzey/32/57_2.png) [@antoine.letouzey](https://discourse.itk.org/u/antoine.letouzey)\
**Post date:** [April 24, 2018, 9:48am UTC](https://discourse.itk.org/t/cant-iterate-over-simple-image/865/2 "2018-04-24T09:48:23Z")

</div>

OK so after reading the documentation more in details I found out that itk::ImageConstIterator does not provide any means of moving the iterator. Same goes for itk::ImageIterator.

I was confused by the first documentation page that pops in google when searching for itk image iterator.  
[https://itk.org/Doxygen/html/ImageIteratorsPage.html](https://itk.org/Doxygen/html/ImageIteratorsPage.html)  
The code samples there, even though they do not represent full programs somehow indicates the use of ImageIterator with the ++ operator.

replacing itk::ImageConstIterator with ImageRegionConstIterator solved my issue.

this piece of code works for me:

```
using BoolImageType = itk::Image<bool, 3U>;
double computeVolumeFromBinaryImage(const BoolImageType::Pointer& a_Image)
{
	itk::ImageRegionConstIterator<BoolImageType> it(a_Image, a_Image->GetLargestPossibleRegion());
	std::size_t numVoxels = 0;
	for (it = it.Begin(); !it.IsAtEnd(); ++it)
	{
		if (it.Get())
			numVoxels++;
	}
	auto size = a_Image->GetSpacing();
	double voxelVolume = size[0] * size[1] * size[2];
	return voxelVolume * numVoxels;
}
```

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [April 24, 2018, 1:49pm UTC](https://discourse.itk.org/t/cant-iterate-over-simple-image/865/3 "2018-04-24T13:49:23Z")

</div>

Thanks for following up your post with the solution!

---

<div class="post-metadata">

**Author:** ![antoine.letouzey](https://discourse.itk.org/user_avatar/discourse.itk.org/antoine.letouzey/32/57_2.png) [@antoine.letouzey](https://discourse.itk.org/u/antoine.letouzey)\
**Post date:** [April 24, 2018, 1:53pm UTC](https://discourse.itk.org/t/cant-iterate-over-simple-image/865/4 "2018-04-24T13:53:57Z")

</div>

The only thing worse than an orphan open question is when the author comes back and say “nevermind, fixed it” and gives no details.

`I'm doing my part`
