-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtransform_image.py
106 lines (82 loc) · 3.62 KB
/
transform_image.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import numpy as np
from skimage.transform import resize
def crop_shape_from_box(image, shape, box):
"""Crops a specified shape from an image which includes the maximal cropped
image of the box. Performs rescaling if the the box is larger than the
shape.
Args:
image: A numpy array.
shape: A [height, width] shape.
box: A [top, left, bottom, right] defined box.
Returns:
A cropped image.
"""
bbox_top = box[0]
bbox_height = box[2] - bbox_top
bbox_left = box[1]
bbox_width = box[3] - bbox_left
bbox_center_y = bbox_top + bbox_height // 2
bbox_center_x = bbox_left + bbox_width // 2
if shape[0] < bbox_height or shape[1] < bbox_width:
# We have a bounding box that is on at least one side greater than the
# requested image shape. We need to crop the image, so that the
# complete bounding box of the image is visible after cropping.
# Find the side with the greater ratio between bounding size and shape
# size.
ratio_y = bbox_height / shape[0]
ratio_x = bbox_width / shape[1]
if ratio_y < ratio_x:
width = bbox_width
height = min(image.shape[0], int(ratio_x * shape[0]))
else:
height = bbox_height
width = min(image.shape[1], int(ratio_y * shape[1]))
else:
# The bounding box is smaller than the requested shape. We try to crop
# the bounding box from its center to the requested shape.
height = shape[0]
width = shape[1]
# Crop the image based on the center of the bounding box.
crop_top = max(bbox_center_y - height // 2, 0)
crop_left = max(bbox_center_x - width // 2, 0)
# We need to adjust the variables if the object is too far at the right
# or the bottom of the image, so that we can get the maximal cropping
# defined by the height and width.
crop_top = min(crop_top, max(image.shape[0] - height, 0))
crop_left = min(crop_left, max(image.shape[1] - width, 0))
# Calculate the opposite sides of the cropping in case the image is
# smaller than the defined cropping.
crop_bottom = min(crop_top + height, image.shape[0])
crop_right = min(crop_left + width, image.shape[1])
# Finally crop the image.
image = image[crop_top:crop_bottom, crop_left:crop_right]
# Rescale the image if needed.
return rescale_and_crop(image, shape)
def rescale_and_crop(image, shape):
"""Rescales an image to the specified shape. Performs cropping if the image
dimensions do not match the specified shape.
Args:
image: A numpy array.
shape: A [height, width] shape.
Returns:
A rescaled and cropped image.
"""
# The passed image can be greater or smaller than the requested shape.
# We need to either scale the image up or down and crop it if this is the
# case.
if image.shape[0] == shape[0] and image.shape[1] == shape[1]:
return image
scale = max(1.0 * shape[0] / image.shape[0],
1.0 * shape[1] / image.shape[1])
# Calculate the shape after resizing and resize the image based on this
# shape.
post_shape = [max(int(scale * image.shape[0]), shape[0]),
max(int(scale * image.shape[1]), shape[1])]
image = resize(image, post_shape, preserve_range=True)
image = image.astype(np.uint8)
# Finally crop the image again based on its center.
crop_top = (image.shape[0] - shape[0]) // 2
crop_bottom = crop_top + shape[0]
crop_left = (image.shape[1] - shape[1]) // 2
crop_right = crop_left + shape[1]
return image[crop_top:crop_bottom, crop_left:crop_right]