-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvision.py
57 lines (39 loc) · 1.53 KB
/
vision.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
import cv2
import numpy as np
def comp_mse(img1, img2):
h, w = img1.shape
diff = cv2.subtract(img1, img2)
err = np.sum(diff ** 2)
mse = err / (float(h * w))
return mse, diff
def compare_images(image1: str, image2: str) -> float:
# load the input images
img1 = cv2.imread(image1)
img2 = cv2.imread(image2)
# convert the images to grayscale
img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
error, _ = comp_mse(img1, img2)
# print("Image matching Error between the two images:", error)
return error
def get_coords(temp: str = "template.png") -> str:
screenshot = cv2.imread("screencap.png", 0)
template = cv2.imread(temp, 0)
h, w = template.shape
res = cv2.matchTemplate(screenshot, template, cv2.TM_SQDIFF)
# threshold = 0.1
# loc = np.where (res >= threshold)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
# return str(int(min_loc[0] + (w / 2))) + " " + str(int(min_loc[1] + (h / 2)))
return str(int(min_loc[0] + (w / 2))) + " " + str(int(min_loc[1]))
def is_template_in_image(img: str, templ: str):
image = cv2.imread(img)
template = cv2.imread(templ)
# Template matching using TM_SQDIFF: Perfect match => minimum value around 0.0
result = cv2.matchTemplate(image, template, cv2.TM_SQDIFF)
# Get value of best match, i.e. the minimum value
min_val = cv2.minMaxLoc(result)[0]
# Set up threshold for a "sufficient" match
# thr = 10e-6
thr = 300.0
return min_val <= thr