-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScan.py
More file actions
234 lines (145 loc) · 4.15 KB
/
Scan.py
File metadata and controls
234 lines (145 loc) · 4.15 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import cv2
import sys
import numpy as np
from PIL import Image
from PIL import ImageTk
import VariableGlobal as varg
import time
def nothing(x):
pass
def scan(mat): #fonction pour scanner le threshold
a=mat.shape[0]
b=mat.shape[1]
#print(str(a)+" "+str(b))
retour=np.zeros(a)
y=0
while y < (a-1):
x=0
temparr=np.array([0])
while x < (b-1) :
if mat[y,x]>0 :
temparr=np.append(temparr,x)
x+=1
temparr=np.delete(temparr,0)
if temparr.size >0:
retour[y]=np.mean(temparr)
y+=1
return retour
def viscan(mask,arr): #fonction pour visualiser l'array du scan
maskblack=np.zeros_like(mask)
y=0
while y<arr.size:
maskblack.itemset( ( y , int( arr[y] ) ) , 255 )
y+=1
return maskblack
def threshold(img,hl,sl,vl,hu,su,vu): #fonction pour obtenir le threshold entre les valeur HSV- et HSV+ d'une image (en noir et blanc)
hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
lower_blue = np.array([hl,sl,vl])
upper_blue = np.array([hu,su,vu])
mask = cv2.inRange(hsv, lower_blue, upper_blue)
return mask
def brightness(img,value):
imgret=cv2.add(img , np.ones_like(img)+value)
return imgret
def convertTk(frame):
if(len(frame.shape) == 3):
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame = Image.fromarray(frame)
image = ImageTk.PhotoImage(frame)
return image
#un tour 2048 pas
def Scanning(cap,Heights,Precision,hl,sl,vl,hu,su,vu):
ret,frame = cap.read()
fond = cv2.imread("SAVES\imageFond.jpg")
Return = "!"
Step = int(2048/Precision)
for H in range(Heights):
for x in range(Step):
ret,frame = cap.read()
ThresholdT = threshold(frame,hl,sl,vl,hu,su,vu)
Line=scan(ThresholdT)
for v in range(len(Line)):
Return += str(Line[v])+":"
cv2.imshow("debug", viscan(ThresholdT,Line) )
k = cv2.waitKey(1) & 0xFF
varg.serial.write(str("001"+str(Precision)).encode('utf-8'))
Return += "\n"
time.sleep(0.4)
cv2.destroyAllWindows()
return Return
##np.set_printoptions(threshold=np.inf)
### imgread
##img = cv2.imread('laser.jpg')
##
##
##
##cv2.imshow('img', img)
##
##
##
##
##cv2.namedWindow('image')
##
### création des trackbars
##cv2.createTrackbar('H-','image',0,179,nothing)
##cv2.createTrackbar('S-','image',0,255,nothing)
##cv2.createTrackbar('V-','image',0,255,nothing)
##
##cv2.createTrackbar('H+','image',0,179,nothing)
##cv2.createTrackbar('S+','image',0,255,nothing)
##cv2.createTrackbar('V+','image',0,255,nothing)
##cv2.createTrackbar('brightness','image',0,100,nothing)
##
##
##
##
##while(1): #boucle principal
##
##
## # avoir les position des trackbars
## h = cv2.getTrackbarPos('H-','image')
## s = cv2.getTrackbarPos('S-','image')
## v = cv2.getTrackbarPos('V-','image')
##
## hu = cv2.getTrackbarPos('H+','image')
## su = cv2.getTrackbarPos('S+','image')
## vu = cv2.getTrackbarPos('V+','image')
## brg = cv2.getTrackbarPos('brightness','image')
##
## img2= brightness(img,brg)
##
## cv2.imshow('img2', img2)
##
## mask = threshold(img2,h,s,v,hu,su,vu) #threshold de l'image
##
## res = cv2.bitwise_and(img2,img2, mask= mask) # image soustraite du threshold
##
## cv2.imshow('img3', mask) #affichage du threshold
##
## cv2.imshow('image',res) #affichage de la soustraction
##
##
##
## k = cv2.waitKey(1) & 0xFF
## if k == 27:
##
## break
##
## if k == 13:
## location = scan(mask) #array scan
##
## maskb=viscan(mask,location) #image scan
##
## cv2.imshow('locat',maskb) # affichage image scan
##
## hsvinf=str(h)+'\n'+str(s)+'\n'+str(v)+'\n'+str(hu)+'\n'+str(su)+'\n'+str(vu)+'\n \n'
##
## f = open('filetest.txt','w')
## f.write(hsvinf+str(location))
## f.close()
##
##
##
##
##
##cv2.destroyAllWindows()