-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
77 lines (59 loc) · 2.31 KB
/
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
65
66
67
68
69
70
71
72
73
74
75
76
77
import os
from typing import Tuple, Optional, List
import cv2
import numpy as np
def is_same_extension(file_path: str, extension: str):
name, ext = os.path.splitext(file_path)
return ext == extension
def get_subwindow(image: np.array, window_name: str = "Select Rectangle") -> Optional[List[Tuple[float]]]:
"""
Prompts user to select 2 points on an image
:param image: Image to select the points on
:param window_name: Name of the prompt window
:returns: selected 2 points
"""
selected_pts = []
def on_mouse_clicked(event, x, y, flags, param):
nonlocal selected_pts
if event == cv2.EVENT_LBUTTONDOWN:
if len(selected_pts) < 2:
selected_pts.append((x, y))
if event == cv2.EVENT_RBUTTONDOWN:
selected_pts = []
cv2.namedWindow(window_name, cv2.WINDOW_AUTOSIZE)
cv2.setMouseCallback(window_name, on_mouse_clicked)
while True:
copied = image.copy()
cv2.putText(copied, "Select 2 points", (25, 25), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255, 255, 255), 1)
for point in selected_pts:
cv2.circle(copied, point, 5, (255, 255, 255), -1)
if len(selected_pts) == 2:
cv2.rectangle(copied, selected_pts[0], selected_pts[1], (255, 255, 255), 2)
cv2.imshow(window_name, copied)
key = cv2.waitKey(1)
if key & 255 == 32:
cv2.destroyWindow(window_name)
return selected_pts
elif key & 255 == 27:
break
cv2.destroyWindow(window_name)
return None
def scale_homography_matrix(homography_matrix, scale_factor):
homography_matrix[0, 2] *= scale_factor
homography_matrix[1, 2] *= scale_factor
homography_matrix[2, 0] /= scale_factor
homography_matrix[2, 1] /= scale_factor
return homography_matrix
def pyr_scale_image(image: np.array, scale_power: int) -> np.array:
"""
Uses cv2.pyrDown or cv2.pyrUp depending on provided scale
:param image: image you want to scale
:param scale_power: number of times to apply scaling operator. +ve scales up, -ve scales down.
:returns: scaled image
"""
if scale_power == 0:
return image
operator = cv2.pyrUp if scale_power > 0 else cv2.pyrDown
for i in range(abs(scale_power)):
image = operator(image)
return image