# Reset index of an image after Crop

**URL:** <https://discourse.itk.org/t/reset-index-of-an-image-after-crop/2870>\
**Category:** Engineering\
**Tags:** crop, registration\
**Created:** [March 23, 2020, 10:16am UTC](https://discourse.itk.org/t/reset-index-of-an-image-after-crop/2870 "2020-03-23T10:16:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gabriele\_Belotti](https://discourse.itk.org/user_avatar/discourse.itk.org/gabriele_belotti/32/825_2.png) [@Gabriele\_Belotti](https://discourse.itk.org/u/Gabriele_Belotti)\
**Post date:** [March 23, 2020, 10:16am UTC](https://discourse.itk.org/t/reset-index-of-an-image-after-crop/2870/1 "2020-03-23T10:16:50Z")

</div>

Dear All,

This is a very specific question so I will give some context:  
I’m registering two Images with very different sizes, but the assumption (verified in script) is that the moving image is far smaller and completely contained in the fixed image.

As an initialization step I crop the fixed image around the moving image region to speed up the process and prevent MMI metric errors.

So, at this point I feed the cropped fixed image to the registration method, without success. I must specify that if I write and read the cropped fixed image (as a debug step) the registration is successful.

If I ::Print() the cropped fixed image information the only difference between the written and read back image is that the index is not placed at [0,0,0].

I’m assuming this is the problem, since the origin and all relevant other informations are correct. Do you have any suggestion on why this is happening or if there is a possible solution?

Thanks in advance for your help!

---

<div class="post-metadata">

**Author:** ![flaviu2](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.itk.org/u/flaviu2)\
**Post date:** [March 23, 2020, 11:46am UTC](https://discourse.itk.org/t/reset-index-of-an-image-after-crop/2870/2 "2020-03-23T11:46:07Z")

</div>

I guess you tried sitk::Image::SetOrigin ? BTW, you use simpleitk ? Or ITK only ?

---

<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:** [March 23, 2020, 11:49am UTC](https://discourse.itk.org/t/reset-index-of-an-image-after-crop/2870/3 "2020-03-23T11:49:56Z")

</div>

Try using [itk::RegionOfInterestImageFilter](https://itk.org/Doxygen/html/classitk_1_1RegionOfInterestImageFilter.html). It does the same operation as the Crop filter, but with different parameters and sets the starting index to 0 while recomputing the origin.

---

<div class="post-metadata">

**Author:** ![Gabriele\_Belotti](https://discourse.itk.org/user_avatar/discourse.itk.org/gabriele_belotti/32/825_2.png) [@Gabriele\_Belotti](https://discourse.itk.org/u/Gabriele_Belotti)\
**Post date:** [March 23, 2020, 12:27pm UTC](https://discourse.itk.org/t/reset-index-of-an-image-after-crop/2870/4 "2020-03-23T12:27:59Z")

</div>

I’m using ITK in cpp.  
The origin is already correct. Anyway resetting it doesn’t solve the issue unfortunately.

---

<div class="post-metadata">

**Author:** ![Gabriele\_Belotti](https://discourse.itk.org/user_avatar/discourse.itk.org/gabriele_belotti/32/825_2.png) [@Gabriele\_Belotti](https://discourse.itk.org/u/Gabriele_Belotti)\
**Post date:** [March 23, 2020, 12:28pm UTC](https://discourse.itk.org/t/reset-index-of-an-image-after-crop/2870/5 "2020-03-23T12:28:41Z")

</div>

Ok I will try to implement my cropping using this filter and update you on the results! Thank you!

---

<div class="post-metadata">

**Author:** ![Gabriele\_Belotti](https://discourse.itk.org/user_avatar/discourse.itk.org/gabriele_belotti/32/825_2.png) [@Gabriele\_Belotti](https://discourse.itk.org/u/Gabriele_Belotti)\
**Post date:** [March 25, 2020, 8:45am UTC](https://discourse.itk.org/t/reset-index-of-an-image-after-crop/2870/6 "2020-03-25T08:45:23Z")

</div>

Issue solved with @blowekamp suggestion, changed the parameters given to the filter to a region with desired size and an index corresponding to the origin of the overlapping region.  
Thank you!

---

<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:** [March 25, 2020, 11:00am UTC](https://discourse.itk.org/t/reset-index-of-an-image-after-crop/2870/7 "2020-03-25T11:00:14Z")

</div>

Thanks for reporting that it fixed the problem. It sounds like there is likely a bug.

To clarify your usage, you were using the ExtractImageFilter’s output as input to the ImageRegistrationMethodv4’s Fixed input?

---

<div class="post-metadata">

**Author:** ![Gabriele\_Belotti](https://discourse.itk.org/user_avatar/discourse.itk.org/gabriele_belotti/32/825_2.png) [@Gabriele\_Belotti](https://discourse.itk.org/u/Gabriele_Belotti)\
**Post date:** [March 25, 2020, 11:17am UTC](https://discourse.itk.org/t/reset-index-of-an-image-after-crop/2870/8 "2020-03-25T11:17:52Z")

</div>

Indeed! To be more specific I’m encountered the bug using CropFilterFilter, a child object of ExtractionImageFilter (didn’t mean to correct you, just wanted to be 100% sure we’re on the same page as this could be a bug).

> CropFixedFilterType::Pointer CropFixedFilter = CropFixedFilterType::New();  
> CropFixedFilter-\>SetInput(Image2Crop);  
> CropFixedFilter-\>SetLowerBoundaryCropSize(LowerCrop);  
> CropFixedFilter-\>SetUpperBoundaryCropSize(UpperCrop);  
> CropFixedFilter-\>SetDirectionCollapseToIdentity();  
> try  
> {  
> if (verbose)  
> std::cout \<\< “Cropping Fixed Image to Moving image size\n”;  
> CropFixedFilter-\>Update();  
> }  
> catch(itk::ExceptionObject &err)  
> {  
> std::cerr \<\< “Exception caught while cropping \n”  
> \<\< err \<\< std::endl;  
> return EXIT\_FAILURE;  
> }  
> FixedImage = CropFixedFilter-\>GetOutput();

This is the extract of the code where I connect everything. Again if I save “FixedImage” and read it back, the problem doesn’t occur.

---

<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:** [March 25, 2020, 11:32am UTC](https://discourse.itk.org/t/reset-index-of-an-image-after-crop/2870/9 "2020-03-25T11:32:50Z")

</div>

> [@Gabriele\_Belotti](#):
>
> This is the extract of the code where I connect everything. Again if I save “FixedImage” and read it back, the problem doesn’t occur.

Not surprising. The “starting” index is used in ITK’s image data representation to stream in the pipeline. The image file formats don’t have this notion, so the starting index of a written files is always zero. When writing the origin is adjusted so that the physical location of all pixels remains the same.
