-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_processing.py
More file actions
95 lines (61 loc) · 1.85 KB
/
image_processing.py
File metadata and controls
95 lines (61 loc) · 1.85 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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import cv2 as cv
import numpy as np
from time import time
import json
max_value = 255
low_H = 0
low_S = 0
low_V = 0
high_H = max_value
high_S = max_value
high_V = max_value
def init_values(color: str):
global low_H, high_H, low_S, low_V
with open("colors.json") as f:
colors = json.load(f)
if not color in colors:
print("Color does not exist")
exit()
values = colors[color]
low_H = values["low_H"]
high_H = values["high_H"]
low_S = values["low_S"]
low_V = values["low_V"]
def threshold(image):
image = cv.cvtColor(image, cv.COLOR_BGR2HSV)
image = cv.inRange(image, (low_H,low_S,low_V),(high_H,high_S,high_V))
structElt = cv.getStructuringElement(cv.MORPH_ELLIPSE, (5, 5))
image = cv.erode(image, structElt)
image = cv.dilate(image, structElt)
#cv.imwrite('1_threshold.jpg', image)
return image
def get_centroid(image):
M = cv.moments(image)
# calcul du centroïde (si tout vaut 0, alors pas de ligne détectée)
try:
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
except:
print("No line detected.")
return None,None
height, width = image.shape
cx -= width/2
return cx, cy
def is_yellow(image):
image = cv.cvtColor(image, cv.COLOR_BGR2HSV)
image = cv.inRange(image, (20,80,80),(100,high_S,high_V))
structElt = cv.getStructuringElement(cv.MORPH_ELLIPSE, (5, 5))
image = cv.erode(image, structElt)
image = cv.dilate(image, structElt)
x,y = get_centroid(image)
return x != None
def processing(image):
img = threshold(image)
x, y = get_centroid(img)
# change = False
# if is_yellow(image):
# change = True
return x, y, False # change
if __name__ == "__main__":
image = cv.imread('img2.jpg')
processing(image)