-
Notifications
You must be signed in to change notification settings - Fork 14
/
image_utils.py
63 lines (47 loc) · 1.82 KB
/
image_utils.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
import numpy as np
import cv2
import math
import matplotlib.pyplot as plt
from torchvision import transforms
HEIGHT = 256
WIDTH = 320
def scale_image(img, scale=None):
"""Resize/scale an image. If a scale is not provided, scale it closer to HEIGHT x WIDTH."""
# if scale is None, scale to the longer size
if scale is None:
scale = max(WIDTH / img.shape[1], HEIGHT / img.shape[0])
new_size = (math.ceil(img.shape[1] * scale), math.ceil(img.shape[0] * scale))
image = cv2.resize(img, new_size, interpolation=cv2.INTER_NEAREST)
return image
def center_crop(img):
"""Center crop an image to HEIGHT x WIDTH."""
corner = ((img.shape[0] - HEIGHT) // 2, (img.shape[1] - WIDTH) // 2)
img = img[corner[0]:corner[0] + HEIGHT, corner[1]:corner[1] + WIDTH]
return img
def img_transform(img):
"""Normalize an image."""
data_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
img = data_transform(img)
return img
def save_img_and_pred(img, depth, save_path):
"""Plot an image and a corresponding prediction next to each other, then save the plot."""
plt.figure()
plt.subplot(1, 2, 1)
plt.imshow(img)
plt.subplot(1, 2, 2)
pred = np.transpose(depth, (1, 2, 0))
plt.imshow(pred[:, :, 0])
plt.savefig(save_path)
def depth_to_grayscale(depth, max_dist=10.0):
"""Transform a prediction into a grayscale 8-bit image."""
depth = np.transpose(depth, (1, 2, 0))
depth[depth > max_dist] = max_dist
depth = depth / max_dist
depth = np.array(depth * 255.0, dtype=np.uint8)
depth = cv2.resize(depth, (WIDTH, HEIGHT))
bgr_depth_img = cv2.cvtColor(depth, cv2.COLOR_GRAY2BGR)
bgr_depth_img = np.clip(bgr_depth_img, 0, 255)
return bgr_depth_img