Resize an image array with interpolation. Parameters ---------- im : (H x W x K) ndarray new_dims : (height, width) tuple of new dimensions. interp_order : interpolation order, default is linear. Returns ------- im : resized ndarray with shape (new_dims[0], new
(im, new_dims, interp_order=1)
| 304 | |
| 305 | |
| 306 | def resize_image(im, new_dims, interp_order=1): |
| 307 | """ |
| 308 | Resize an image array with interpolation. |
| 309 | |
| 310 | Parameters |
| 311 | ---------- |
| 312 | im : (H x W x K) ndarray |
| 313 | new_dims : (height, width) tuple of new dimensions. |
| 314 | interp_order : interpolation order, default is linear. |
| 315 | |
| 316 | Returns |
| 317 | ------- |
| 318 | im : resized ndarray with shape (new_dims[0], new_dims[1], K) |
| 319 | """ |
| 320 | if im.shape[-1] == 1 or im.shape[-1] == 3: |
| 321 | im_min, im_max = im.min(), im.max() |
| 322 | if im_max > im_min: |
| 323 | # skimage is fast but only understands {1,3} channel images |
| 324 | # in [0, 1]. |
| 325 | im_std = (im - im_min) / (im_max - im_min) |
| 326 | resized_std = resize(im_std, new_dims, order=interp_order) |
| 327 | resized_im = resized_std * (im_max - im_min) + im_min |
| 328 | else: |
| 329 | # the image is a constant -- avoid divide by 0 |
| 330 | ret = np.empty((new_dims[0], new_dims[1], im.shape[-1]), |
| 331 | dtype=np.float32) |
| 332 | ret.fill(im_min) |
| 333 | return ret |
| 334 | else: |
| 335 | # ndimage interpolates anything but more slowly. |
| 336 | scale = tuple(np.array(new_dims, dtype=float) / np.array(im.shape[:2])) |
| 337 | resized_im = zoom(im, scale + (1,), order=interp_order) |
| 338 | return resized_im.astype(np.float32) |
| 339 | |
| 340 | |
| 341 | def oversample(images, crop_dims): |