Resize sample to given size (width, height).
| 3 | |
| 4 | |
| 5 | class Resize(object): |
| 6 | """Resize sample to given size (width, height). |
| 7 | """ |
| 8 | |
| 9 | def __init__( |
| 10 | self, |
| 11 | width, |
| 12 | height, |
| 13 | resize_target=True, |
| 14 | keep_aspect_ratio=False, |
| 15 | ensure_multiple_of=1, |
| 16 | resize_method="lower_bound", |
| 17 | image_interpolation_method=cv2.INTER_AREA, |
| 18 | ): |
| 19 | """Init. |
| 20 | |
| 21 | Args: |
| 22 | width (int): desired output width |
| 23 | height (int): desired output height |
| 24 | resize_target (bool, optional): |
| 25 | True: Resize the full sample (image, mask, target). |
| 26 | False: Resize image only. |
| 27 | Defaults to True. |
| 28 | keep_aspect_ratio (bool, optional): |
| 29 | True: Keep the aspect ratio of the input sample. |
| 30 | Output sample might not have the given width and height, and |
| 31 | resize behaviour depends on the parameter 'resize_method'. |
| 32 | Defaults to False. |
| 33 | ensure_multiple_of (int, optional): |
| 34 | Output width and height is constrained to be multiple of this parameter. |
| 35 | Defaults to 1. |
| 36 | resize_method (str, optional): |
| 37 | "lower_bound": Output will be at least as large as the given size. |
| 38 | "upper_bound": Output will be at max as large as the given size. (Output size might be smaller than given size.) |
| 39 | "minimal": Scale as least as possible. (Output size might be smaller than given size.) |
| 40 | Defaults to "lower_bound". |
| 41 | """ |
| 42 | self.__width = width |
| 43 | self.__height = height |
| 44 | |
| 45 | self.__resize_target = resize_target |
| 46 | self.__keep_aspect_ratio = keep_aspect_ratio |
| 47 | self.__multiple_of = ensure_multiple_of |
| 48 | self.__resize_method = resize_method |
| 49 | self.__image_interpolation_method = image_interpolation_method |
| 50 | |
| 51 | def constrain_to_multiple_of(self, x, min_val=0, max_val=None): |
| 52 | y = (np.round(x / self.__multiple_of) * self.__multiple_of).astype(int) |
| 53 | |
| 54 | if max_val is not None and y > max_val: |
| 55 | y = (np.floor(x / self.__multiple_of) * self.__multiple_of).astype(int) |
| 56 | |
| 57 | if y < min_val: |
| 58 | y = (np.ceil(x / self.__multiple_of) * self.__multiple_of).astype(int) |
| 59 | |
| 60 | return y |
| 61 | |
| 62 | def get_size(self, width, height): |
nothing calls this directly
no outgoing calls
no test coverage detected