NextFace icon indicating copy to clipboard operation
NextFace copied to clipboard

Image resize is not correct

Open roadwide opened this issue 2 years ago • 2 comments
trafficstars

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

roadwide avatar Mar 20 '23 11:03 roadwide

Hi

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

abdallahdib avatar Mar 20 '23 12:03 abdallahdib

Was there a proposed fix for this?

Wang-Thunder avatar May 11 '23 14:05 Wang-Thunder