imresize function same as MATLAB. It now only supports bicubic. The same scale applies for both height and width. Args: img (Tensor | Numpy array): Tensor: Input image with shape (c, h, w), [0, 1] range. Numpy: Input image with shape (h, w, c), [0, 1] ra
(img, scale, antialiasing=True)
| 84 | |
| 85 | @torch.no_grad() |
| 86 | def imresize(img, scale, antialiasing=True): |
| 87 | """imresize function same as MATLAB. |
| 88 | |
| 89 | It now only supports bicubic. |
| 90 | The same scale applies for both height and width. |
| 91 | |
| 92 | Args: |
| 93 | img (Tensor | Numpy array): |
| 94 | Tensor: Input image with shape (c, h, w), [0, 1] range. |
| 95 | Numpy: Input image with shape (h, w, c), [0, 1] range. |
| 96 | scale (float): Scale factor. The same scale applies for both height |
| 97 | and width. |
| 98 | antialisaing (bool): Whether to apply anti-aliasing when downsampling. |
| 99 | Default: True. |
| 100 | |
| 101 | Returns: |
| 102 | Tensor: Output image with shape (c, h, w), [0, 1] range, w/o round. |
| 103 | """ |
| 104 | squeeze_flag = False |
| 105 | if type(img).__module__ == np.__name__: # numpy type |
| 106 | numpy_type = True |
| 107 | if img.ndim == 2: |
| 108 | img = img[:, :, None] |
| 109 | squeeze_flag = True |
| 110 | img = torch.from_numpy(img.transpose(2, 0, 1)).float() |
| 111 | else: |
| 112 | numpy_type = False |
| 113 | if img.ndim == 2: |
| 114 | img = img.unsqueeze(0) |
| 115 | squeeze_flag = True |
| 116 | |
| 117 | in_c, in_h, in_w = img.size() |
| 118 | out_h, out_w = math.ceil(in_h * scale), math.ceil(in_w * scale) |
| 119 | kernel_width = 4 |
| 120 | kernel = 'cubic' |
| 121 | |
| 122 | # get weights and indices |
| 123 | weights_h, indices_h, sym_len_hs, sym_len_he = calculate_weights_indices(in_h, out_h, scale, kernel, kernel_width, |
| 124 | antialiasing) |
| 125 | weights_w, indices_w, sym_len_ws, sym_len_we = calculate_weights_indices(in_w, out_w, scale, kernel, kernel_width, |
| 126 | antialiasing) |
| 127 | # process H dimension |
| 128 | # symmetric copying |
| 129 | img_aug = torch.FloatTensor(in_c, in_h + sym_len_hs + sym_len_he, in_w) |
| 130 | img_aug.narrow(1, sym_len_hs, in_h).copy_(img) |
| 131 | |
| 132 | sym_patch = img[:, :sym_len_hs, :] |
| 133 | inv_idx = torch.arange(sym_patch.size(1) - 1, -1, -1).long() |
| 134 | sym_patch_inv = sym_patch.index_select(1, inv_idx) |
| 135 | img_aug.narrow(1, 0, sym_len_hs).copy_(sym_patch_inv) |
| 136 | |
| 137 | sym_patch = img[:, -sym_len_he:, :] |
| 138 | inv_idx = torch.arange(sym_patch.size(1) - 1, -1, -1).long() |
| 139 | sym_patch_inv = sym_patch.index_select(1, inv_idx) |
| 140 | img_aug.narrow(1, sym_len_hs + in_h, sym_len_he).copy_(sym_patch_inv) |
| 141 | |
| 142 | out_1 = torch.FloatTensor(in_c, out_h, in_w) |
| 143 | kernel_width = weights_h.size(1) |
no test coverage detected