forked from sleichu2/Drone_Patrolling_Net_Evaluation
-
Notifications
You must be signed in to change notification settings - Fork 1
/
img_utils.py
59 lines (42 loc) · 1.52 KB
/
img_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
import cv2
import numpy as np
def load_img(path, grayscale=False, target_size=None, crop_size=None):
"""
Load an image.
# Arguments
path: Path to image file.
grayscale: Boolean, whether to load the image as grayscale.
target_size: Either `None` (default to original size)
or tuple of ints `(img_width, img_height)`.
crop_size: Either `None` (default to original size)
or tuple of ints `(img_width, img_height)`.
# Returns
Image as numpy array.
"""
img = cv2.imread(path)
if grayscale:
if len(img.shape) != 2:
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
if target_size:
if (img.shape[0], img.shape[1]) != target_size:
img = cv2.resize(img, (target_size[1],target_size[0]))
if crop_size:
img = central_image_crop(img, crop_size[0], crop_size[1])
if grayscale:
img = img.reshape((img.shape[0], img.shape[1], 1))
return np.asarray(img, dtype=np.float32)
def central_image_crop(img, crop_heigth=150, crop_width=150):
"""
Crop the input image centered in width and starting from the bottom
in height.
# Arguments:
crop_width: Width of the crop.
crop_heigth: Height of the crop.
# Returns:
Cropped image.
"""
half_the_width = int(img.shape[1] / 2)
img = img[img.shape[0] - crop_heigth: img.shape[0],
half_the_width - int(crop_width / 2):
half_the_width + int(crop_width / 2)]
return img