-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrackbar_new.py
105 lines (82 loc) · 2.84 KB
/
trackbar_new.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/python
import cv2
import numpy as np
def filter_mask(im_filter_mask):
kernel = np.ones((5, 5), np.uint8)
erode = cv2.erode(im_filter_mask, kernel, iterations=1)
dilate = cv2.dilate(erode, kernel, iterations=1)
gaussian = cv2.GaussianBlur(dilate, (5, 5), 1)
return gaussian
def update(*arg):
h0 = cv2.getTrackbarPos('h min', 'control')
h1 = cv2.getTrackbarPos('h max', 'control')
s0 = cv2.getTrackbarPos('s min', 'control')
s1 = cv2.getTrackbarPos('s max', 'control')
v0 = cv2.getTrackbarPos('v min', 'control')
v1 = cv2.getTrackbarPos('v max', 'control')
lower = np.array((h0, s0, v0))
upper = np.array((h1, s1, v1))
mask = cv2.inRange(hsv, lower, upper)
gaussian = filter_mask(mask)
cv2.imshow('GAUSSIAN:', gaussian)
cv2.imshow('mask', mask)
contours, hierarchy = cv2.findContours(
gaussian,
cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE
)
total_contours = len(contours)
result = src.copy()
#cv2.drawContours(result, contours, 1, (0,255,0), 3)
contours_error = []
contours_ok = []
for cont_contours in contours:
area = cv2.contourArea(cont_contours)
print("area: ", area)
if (area > 50 and area < 400):
contours_ok.append(cont_contours)
else:
print("ERROR_AREA: ", area)
contours_error.append(cont_contours)
cv2.drawContours(result, contours_ok, -1, (0, 255, 0), 3)
cv2.drawContours(result, contours_error, -1, (0, 0, 255), 3)
print("TOTAL: ", total_contours)
print("OK_IF: ", len(contours_ok))
print("ERROR_IF: ", len(contours_error))
cv2.imshow('result:', result)
def main():
cv2.namedWindow('control', 0)
cv2.createTrackbar('h min', 'control', 32, 255, update)
cv2.createTrackbar('h max', 'control', 38, 255, update)
cv2.createTrackbar('s min', 'control', 0, 255, update)
cv2.createTrackbar('s max', 'control', 176, 255, update)
cv2.createTrackbar('v min', 'control', 106, 255, update)
cv2.createTrackbar('v max', 'control', 255, 255, update)
# trackbar(nome controle, janela, default, max, funcao)
im = cv2.resize(src, None, fx=0.25, fy=0.25, interpolation=cv2.INTER_CUBIC)
cv2.imshow('control', im)
update()
while 1:
ch = cv2.waitKey(30)
if (ch == 27):
cv2.destroyAllWindows()
break
if __name__ == '__main__':
import sys
try:
fn = sys.argv[1]
print("parametro:", fn)
except:
fn = 'images/hortomosaico_eucalipto.png'
src = cv2.imread(fn)
# resize(imagem_entrada, None, scala x, escala y, flag interpolacao )
src = cv2.resize(
src,
None,
fx=0.25,
fy=0.25,
interpolation=cv2.INTER_CUBIC
)
print("resized shape:", src.shape)
hsv = cv2.cvtColor(src, cv2.COLOR_BGR2HSV)
main()