-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImage_functions.py
More file actions
32 lines (24 loc) · 971 Bytes
/
Image_functions.py
File metadata and controls
32 lines (24 loc) · 971 Bytes
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
import cv2 as cv
import numpy as np
#np.set_printoptions(precision=3)
def get_image(img_source):
image = cv.imread(img_source)
return image
def save_image(image, path='/Users/hadi/Downloads/RERE/'):
cv.imwrite(path, image)
def display_image(image):
cv.imshow('imagesss', image)
cv.waitKey(0)
cv.destroyAllWindows()
def resize_image(img, desired_size):
img_w, img_h = img.shape[1], img.shape[0]
w, h = desired_size
new_w = int(img_w * min(w / img_w, h / img_h))
new_h = int(img_h * min(w / img_w, h / img_h))
resized_image = cv.resize(img, (new_w, new_h), interpolation=cv.INTER_CUBIC)
canvas = np.full((desired_size[1], desired_size[0], 3), 128)
canvas[(h - new_h) // 2:(h - new_h) // 2 + new_h, (w - new_w) // 2:(w - new_w) // 2 + new_w, :] = resized_image
canvas = canvas[:, :, ::-1].transpose((2, 0, 1)).copy()
canvas = canvas / 255.0
canvas = np.expand_dims(canvas, axis=0)
return canvas