File size: 1,943 Bytes
709cfd2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import cv2
import numpy as np
import torch
import torch.nn.functional as F



def image2tensor(image):
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image = np.asarray(image / 255.).astype(np.float32)
    image = np.transpose(image, (2, 0, 1))
    image = np.ascontiguousarray(image).astype(np.float32)
    image = torch.from_numpy(image).unsqueeze(0)

    return image

def resize_1024(image):
    image = cv2.resize(image, (1024, 768), interpolation=cv2.INTER_LINEAR)
    return image

def resize_1024_crop(image):
    ori_h, ori_w = image.shape[:2]
    tar_w, tar_h = 1024, 768
    if ori_h > ori_w:
        resize_h = int(tar_w / ori_w * ori_h)
        image = cv2.resize(image, (tar_w, resize_h), interpolation=cv2.INTER_LINEAR)
        if resize_h > tar_h:
            top = (resize_h - tar_h) // 2
            image = image[top:top+tar_h, :]
        else:
            image = cv2.resize(image, (tar_w, tar_h), interpolation=cv2.INTER_LINEAR)

    else:
        resize_w = int(tar_h / ori_h * ori_w)
        image = cv2.resize(image, (resize_w, tar_h), interpolation=cv2.INTER_LINEAR)

        if resize_w > tar_w:
            left = (resize_w - tar_w) // 2
            image = image[:, left:left+tar_w]
        else:
            image = cv2.resize(image, (tar_w, tar_h), interpolation=cv2.INTER_LINEAR)

    return image

def resize_keep_aspect(image):
    ori_h, ori_w = image.shape[:2]
    tar_w, tar_h = 1024, 768
    ori_area = ori_h * ori_w
    tar_area = tar_h * tar_w
    scale = scale = (tar_area / ori_area) ** 0.5
    resize_h = ori_h * scale
    resize_w = ori_w * scale
    resize_h = max(16, int(round(resize_h / 16)) * 16)
    resize_w = max(16, int(round(resize_w / 16)) * 16)
    if scale < 1:
        image = cv2.resize(image, (resize_w, resize_h), interpolation=cv2.INTER_AREA)
    else:
        image = cv2.resize(image, (resize_w, resize_h), interpolation=cv2.INTER_CUBIC)
    return image