-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_utils.py
More file actions
28 lines (24 loc) · 1.3 KB
/
image_utils.py
File metadata and controls
28 lines (24 loc) · 1.3 KB
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
import cv2
import numpy as np
from typing import Tuple
VisionFrame = np.ndarray
Translation = np.ndarray
Size = Tuple[int, int]
Matrix = np.ndarray
Angle = float
def conditional_optimize_contrast(crop_vision_frame: VisionFrame) -> VisionFrame:
crop_vision_frame = cv2.cvtColor(crop_vision_frame, cv2.COLOR_RGB2Lab)
if np.mean(crop_vision_frame[:, :, 0]) < 30:
crop_vision_frame[:, :, 0] = cv2.createCLAHE(clipLimit=2).apply(crop_vision_frame[:, :, 0])
crop_vision_frame = cv2.cvtColor(crop_vision_frame, cv2.COLOR_Lab2RGB)
return crop_vision_frame
def warp_face_by_translation(temp_vision_frame: VisionFrame, translation: Translation, scale: float, crop_size: Size) -> Tuple[VisionFrame, Matrix]:
affine_matrix = np.array([[scale, 0, translation[0]], [0, scale, translation[1]]])
crop_vision_frame = cv2.warpAffine(temp_vision_frame, affine_matrix, crop_size)
return crop_vision_frame, affine_matrix
def create_rotated_matrix_and_size(angle: Angle, size: Size) -> Tuple[Matrix, Size]:
rotated_matrix = cv2.getRotationMatrix2D((size[0] / 2, size[1] / 2), angle, 1)
rotated_size = np.dot(np.abs(rotated_matrix[:, :2]), size)
rotated_matrix[:, -1] += (rotated_size - size) * 0.5
rotated_size = int(rotated_size[0]), int(rotated_size[1])
return rotated_matrix, rotated_size