-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathconversion_utils.py
64 lines (50 loc) · 1.96 KB
/
conversion_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
64
import cv2
import torch
import torchvision.transforms.functional as TF
from torch import Tensor
from cv2.typing import MatLike
def image_to_tensor(image: Tensor) -> Tensor:
typical = image.permute(0, 3, 1, 2).squeeze(0).mul(255).byte()
return typical
def tensor_to_image(tensor: Tensor) -> Tensor:
image = tensor.float().div(255).unsqueeze(0).permute(0, 2, 3, 1)
return image
def tensor_to_cv2BGR(tensor_image: Tensor) -> MatLike:
'''
Converts a typical Tensor image to cv2 BGR image.
'''
# Step 1: Convert PyTorch tensor to NumPy array
numpy_image = tensor_image.permute(1, 2, 0).numpy()
# Step 2: Convert NumPy array to cv2 image
cv2_image = cv2.cvtColor(numpy_image, cv2.COLOR_RGB2BGR)
return cv2_image
def tensor_to_cv2YCrCb(tensor_image: Tensor) -> MatLike:
'''
Converts a typical Tensor image to cv2 YCrCb image.
'''
# Step 1: Convert PyTorch tensor to NumPy array
numpy_image = tensor_image.permute(1, 2, 0).numpy()
# Step 2: Convert NumPy array to cv2 image
cv2_image = cv2.cvtColor(numpy_image, cv2.COLOR_RGB2YCrCb)
return cv2_image
def tensor_to_cv2HSV(tensor_image: Tensor) -> MatLike:
'''
Converts a typical Tensor image to cv2 YCrCb image.
'''
# Step 1: Convert PyTorch tensor to NumPy array
numpy_image = tensor_image.permute(1, 2, 0).numpy()
# Step 2: Convert NumPy array to cv2 image
cv2_image = cv2.cvtColor(numpy_image, cv2.COLOR_RGB2HSV)
return cv2_image
def cv2BGR_to_tensor(cv2_image) -> Tensor:
'''
Converts a cv2 image to typical Tensor image.
'''
# Step 1: Convert cv2 image to NumPy array
# If the image is in BGR order, convert it to RGB
numpy_image = cv2.cvtColor(cv2_image, cv2.COLOR_BGR2RGB)
# Step 2: Convert NumPy array to PyTorch tensor
tensor_image = torch.tensor(numpy_image)
# Permute dimensions to match PyTorch tensor format (3, H, W)
tensor_image = tensor_image.permute(2, 0, 1)
return tensor_image