-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathl0_image.py
60 lines (49 loc) · 1.77 KB
/
l0_image.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
import os, colorsys
import cv2
import numpy as np
import config
'''
Load an image, run SIFT, and draw some test lines on it.
'''
detector = cv2.SIFT()
class BadImageException(Exception):
pass
class Image:
def __init__(self, path, resize=None):
if not os.path.exists(path):
raise Exception('image not found: {}'.format(path))
self.path = path
self.image = cv2.imread(path)
if self.image is None:
raise BadImageException()
h, w, _ = self.image.shape
if resize and resize != 1:
self.image = cv2.resize(
self.image, (int(round(w * resize)), int(round(h * resize))))
self.features = Features(self.image)
class Features:
def __init__(self, image):
self.kp, self.desc = detector.detectAndCompute(image, None)
def draw_lines(canvas, points1, points2, line_strengths):
min_strength = min(line_strengths) if line_strengths else 0
max_strength = max(line_strengths) if line_strengths else 0
for p1, p2, line_strength in zip(points1, points2, line_strengths):
x1, y1 = p1
x2, y2 = p2
x1, y1, x2, y2 = [int(round(x)) for x in x1, y1, x2, y2]
hue = 0
if (max_strength - min_strength) != 0:
hue = .5 * (line_strength - min_strength) / float(max_strength - min_strength)
color = tuple(x * 255 for x in colorsys.hsv_to_rgb(hue, 1, 1))
cv2.circle(canvas, (x1, y1), 2, color, -1)
cv2.circle(canvas, (x2, y2), 2, color, -1)
cv2.line(canvas, (x1, y1), (x2, y2), color)
return canvas.copy()
def display(image):
cv2.imwrite("stuff/out.png", image)
os.system("open -a Preview stuff/out.png")
if __name__ == '__main__':
image = Image(config.get_frame('out0001.png'), resize=.5)
canvas = draw_lines(
image.image, [(10,10), (20,20), (30,30)], [(100,10), (100,100), (100,200)], [1,2,3])
display(canvas)