jrkerns / pylinac

An image analysis library for medical physics
https://pylinac.readthedocs.io/en/latest/
MIT License
157 stars 99 forks source link

For equate_images, crop only when it is necessary. #446

Closed LuisOlivaresJ closed 1 year ago

LuisOlivaresJ commented 1 year ago

I am using pylinac 3.11.0

Describe the bug I have a bug (ZeroDivisionError) when I use equate_images for two images that actually have the same physical size but different resolution.

To Reproduce

from pylinac.core.image import equate_images
from pylinac.core.image import load
import numpy as np

image1 = load(np.random.rand(20, 20), dpi = 10)
image2 = load(np.random.rand(10, 10), dpi = 5)

I1, I2 = equate_images(image1, image2)

Captura

Expected behavior I2.shape to be (20,20)

Additional context The problem begins with the crop method. https://github.com/jrkerns/pylinac/blob/73ffc6229e785799c86e751ff0b7397c88a5ce5a/pylinac/core/image.py#L102 It is called even if pixel_height_diff == 0. In other words, when crop is called with the parameter pixels = 0, the method gives unwanted results.

Proposed solution Crop method should be called only when pixel_width_diff > 0

I'll make a pull request for this.

LuisOlivaresJ commented 1 year ago

I have added a proposition inside commit #445.

jrkerns commented 1 year ago

Thanks for the find and the fix. It also appears that the crop method needs to be updated. E.g. doing img.crop(pixels=0, ...) will set the array shape to 0 which is unintended behavior.

I will change this line to be <= 0:

https://github.com/jrkerns/pylinac/blob/0b6d63f1cbb98743fedd3fbcde02ae33f33d3568/pylinac/core/image.py#L430

LuisOlivaresJ commented 1 year ago

Thanks. This issue could be closed if you want.

jrkerns commented 1 year ago

Closing. Thanks for the contribution.