Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image resize is not correct #54

Open
roadwide opened this issue Mar 20, 2023 · 2 comments
Open

Image resize is not correct #54

roadwide opened this issue Mar 20, 2023 · 2 comments

Comments

@roadwide
Copy link

image.py
`
def resizeImage(image, targetResolution):
'''
resize an image (as numpy array) to the target resolution
:param image: numpy array [h, w, 4/3/1]
:param targetResolution: int > 0
:return: numpy array [h, w, 4/3/1]
'''
assert(image is not None and isinstance(image, np.ndarray) and len(image.shape) == 3 and image.shape[-1] == 3 or image.shape[-1] == 4 or image.shape[-1] == 1)
dmax = max(image.shape[0], image.shape[1])

if (dmax > targetResolution):
    print("[INFO] resizing input image to fit:", targetResolution,"px resolution...")
    if (image.shape[0] > image.shape[1]):
        scale = float(targetResolution) / float(image.shape[0])
    else:
        scale = float(targetResolution) / float(image.shape[1])
    img = cv2.resize(image, (int(image.shape[1] * scale), int(image.shape[0] * scale)), interpolation=cv2.INTER_CUBIC )
else:
    return image
return img

and
width = None
height = None
ct = 0

assert (len(filenames) > 0) # no images found in the given directory
for filename in filenames:
if os.path.splitext(filename)[1].lower() in supportedFormats:
image = Image(path + '/' + filename, device, maxRes)

    if width is None:
        width = image.width
        height = image.height
        self.tensor = torch.zeros([len(filenames), height, width, image.channels], device = self.device)
        self.center = torch.zeros([len(filenames), 2], device = self.device)

    assert image.width == width and image.height == height

    self.width = image.width
    self.height = image.height
    self.channels = image.channels
    self.tensor[ct] = image.tensor[0].clone().detach()
    self.center[ct] = image.center[0].clone().detach()
    self.imageNames.append(image.imageName)
    image = None

    ct += 1

`
When the folder is read, width is initialized to None, and the first image is read in with the initial value, and all subsequent images are asserted according to this value.
Suppose all resize is 256 px, and I input an image (not the first one) of 1935 px * 1935 px. scale is 256/1935 = 0.13229974160206717 (probably related to decimal representation precision). Then, int(image.shape[1] * scale) = int(255.999999999999999997) = 255.
assert image.width == width and image.height == height would be an error. Because 255 is not equal to 256.
Maybe it is better to resize to targetResolution directly or use the round function instead of int().

@abdallahdib
Copy link
Owner

Hi

yes what u r suggesting of resizing to the target reosluton could fix that issue

@Wang-Thunder
Copy link

Was there a proposed fix for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants