-
Notifications
You must be signed in to change notification settings - Fork 4
/
frame.py
426 lines (357 loc) · 16.9 KB
/
frame.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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
__author__="Gabriel Garrido Calvo " +"-"+ " Miguel Angel Valero Rivero"
__version__="0.9"
import os
import platform
try:
arquitecture = platform.architecture()[0]
OS = os.uname()[0]
if OS == 'Linux':
if arquitecture == '32bit':
import libLINUX.cv as cv
else:
import libLINUX.x64.cv as cv
elif OS == 'Darwin':
if arquitecture == '32bit':
import libMac.cv as cv
else:
import libMac.x64.cv as cv
else:
import cv
except:
try:
import cv
except:
print "ERROR: This program works only on LINUX or MAC. Version 2.6.0 OPENCV"
import math
# Types of filters
class FrameFilter:
""" Static class where is selected the type of filter to apply
@cvar DISABLE: None filter to apply
@cvar GAUSSIAN: Gaussian filter to apply- Linear convolution with a 9x9 Gaussian kernel
@cvar MEDIAN: Median filter with a 9x9 Square aperture
@cvar CANNY: Algorithm for edge detection.
"""
DISABLE = 0
GAUSSIAN = 1
MEDIAN = 2
CANNY = 3
class Frame:
""" Keeps the information about each frame before to display. This class applys the
different filters and techniques of detection of background and tracking on the image
@ivar image: Stores the frame information.
"""
__image = None
def __init__(self,iplI):
""" Initiator of the class Frame. It is stored the information about a IplImage
@param iplI: Information of the image to store
@type iplI: IpLImage or cvMat
"""
self.__image = cv.CloneMat(iplI)
def getImage(self):
""" Gets the stored image
@return: IpLImage or cvMat
"""
return self.__image
def applyFilter(self,typeF):
""" Applies filter to the stored image
@param typeF: The type the filter to apply
@type typeF: FrameFilter {GAUSSIAN,MEDIAN,CANNY}
"""
if typeF == FrameFilter.GAUSSIAN:
cv.Smooth(self.__image,self.__image,cv.CV_GAUSSIAN,9,9)
elif typeF == FrameFilter.MEDIAN:
cv.Smooth(self.__image,self.__image,cv.CV_MEDIAN,9)
elif typeF == FrameFilter.CANNY:
frame2 = cv.CreateImage(cv.GetSize(self.__image),8,1)
cv.CvtColor(self.__image,frame2,cv.CV_BGR2GRAY)
cv.Canny(frame2,frame2,70.0,140.0,3)
cv.CvtColor(frame2,self.__image,cv.CV_GRAY2BGR)
def replaceBG(self,colorToReplace, threshold,imageBG, typeBG):
""" Replaces the background color with the specified image. So that it is
used a amount of threshold to detected the background color and different method
to detect it.
@param colorToReplace: Color used to detect the Background
@type colorToReplace: cvScalar
@param threshold: Threshould used as the edge to detect Background.
@type threshold: int or IplImage
@param imageBG: The image used as new Background
@type imageBG: IplImage
@param typeBG: Select type of technique to detect the Background
@param typeBG: int{0,1,2 or 3}
@note: If the used technique is 2, the colorToReplace must be a another IpLImage
"""
BG = cv.CreateImage(cv.GetSize(self.__image), cv.IPL_DEPTH_8U, 3)
if typeBG == 0:
BG,grayI = self.__getBackGround(threshold,colorToReplace)
elif typeBG == 1:
BG,grayI = self.__getBackGround1(threshold,colorToReplace)
elif typeBG == 2:
BG,grayI = self.__getBackGround2(threshold,colorToReplace[0])
colorToReplace[0] = cv.CloneMat(cv.GetMat(self.__image))
elif typeBG == 3:
BG,grayI = self.__getBackGround3(threshold,colorToReplace)
cv.Not(grayI,grayI)
cv.Add(BG,imageBG,self.__image,grayI)
#----- GRAY SCALE DISPLAY (pixel white to replace)
#cv.CvtColor(grayI,self.__image,cv.CV_GRAY2BGR)
#----- REPLACE BG DISPLAY (pixel black to replace)
#self.__image = cv.CloneMat(cv.GetMat(BG))
def __getBackGround(self,threshold,color):
""" Method number 1 to detect the background pixels. The background is detected of
the local Image using given color. For this it is calculed
the difference between local Image and another image set up the given color only.
@param threshold: Edge to detect the color background
@type threshold: int
@param color: Color used to detect background
@type color: cvScalar
@return: As first parameter the original image changing the different pixel to black pixel.
As Second parameter a IplImage in Gray Scale where the differences are white pixels. After
this can be used as mask.
"""
differenceI = cv.CreateImage(cv.GetSize(self.__image), cv.IPL_DEPTH_8U, 3)
grayI = cv.CreateImage(cv.GetSize(self.__image), cv.IPL_DEPTH_8U, 1)
backgroundI = cv.CreateImage(cv.GetSize(self.__image), cv.IPL_DEPTH_8U, 3)
cv.Set(backgroundI,color)
cv.AbsDiff(self.__image,backgroundI,differenceI)
cv.CvtColor(differenceI,grayI,cv.CV_BGR2GRAY)
cv.Smooth(grayI,grayI,cv.CV_BLUR,5)
cv.Smooth(grayI,grayI,cv.CV_MEDIAN,5)
cv.Threshold(grayI,grayI,threshold, 255, cv.CV_THRESH_BINARY )
cv.SetZero(differenceI)
cv.And(self.__image,self.__image,differenceI,grayI)
return differenceI,grayI
#return grayI
def __getBackGround1(self,threshold,color):
""" Method number 2 to detect the background pixels. The background is detected of
the local Image using given color. For this it is calculed
the difference between local Image and another image set up the given color only in
each one channel of color.
@param threshold: Edge to detect the color background
@type threshold: int
@param color: Color used to detect background
@type color: cvScalar
@return: As first parameter the original image changing the different pixel to black pixel.
As Second parameter a IplImage in Gray Scale where the differences are white pixels. After
this can be used as mask.
"""
backgroundI = cv.CreateImage(cv.GetSize(self.__image), cv.IPL_DEPTH_8U, 3)
cv.Set(backgroundI,color)
i0,i1,i2,i3,b0,b1,b2,b3,d0,d1,d2,d3 = tuple([cv.CreateImage(cv.GetSize(self.__image), cv.IPL_DEPTH_8U, 1) for i in range(12)])
differenceI = cv.CreateImage(cv.GetSize(self.__image), cv.IPL_DEPTH_8U, 3)
grayI = cv.CreateImage(cv.GetSize(differenceI), cv.IPL_DEPTH_8U, 1)
cv.Split(self.__image,i0,i1,i2,None)
cv.Split(backgroundI,b0,b1,b2,None)
cv.AbsDiff(i0,b0,d0)
cv.AbsDiff(i1,b1,d1)
cv.AbsDiff(i2,b2,d2)
cv.Threshold(d0,d0,threshold, 255, cv.CV_THRESH_BINARY )
cv.Threshold(d1,d1,threshold, 255, cv.CV_THRESH_BINARY )
cv.Threshold(d2,d2,threshold, 255, cv.CV_THRESH_BINARY )
cv.Merge(d0,d1,d2,None,differenceI)
cv.CvtColor(differenceI,grayI,cv.CV_BGR2GRAY)
cv.Smooth(grayI,grayI,cv.CV_BLUR,5)
cv.Smooth(grayI,grayI,cv.CV_MEDIAN,5)
cv.Threshold(grayI,grayI,threshold, 255, cv.CV_THRESH_BINARY)
cv.SetZero(differenceI)
cv.Add(self.__image,differenceI,differenceI,grayI)
return differenceI,grayI
def __getBackGround2(self,threshold,preImage):
""" Method number 3 to detect the background pixels. The background is detected of
the local Image using the given preImage. For this it is calculed the difference
between them.
@type threshold: int
@param preImage: Image used to compare the differences
@type preImage: IplImage
@return: As first parameter the original image changing the different pixel to black pixel.
As Second parameter a IplImage in Gray Scale where the differences are white pixels. After
this can be used as mask.
"""
differenceI = cv.CreateImage(cv.GetSize(self.__image), cv.IPL_DEPTH_8U, 3)
grayI = cv.CreateImage(cv.GetSize(differenceI), cv.IPL_DEPTH_8U, 1)
cv.AbsDiff(self.__image,preImage,differenceI)
cv.CvtColor(differenceI,grayI,cv.CV_BGR2GRAY)
cv.Threshold(grayI,grayI,threshold, 255, cv.CV_THRESH_BINARY )
cv.Smooth(grayI,grayI,cv.CV_MEDIAN,5)
cv.Smooth(grayI,grayI,cv.CV_GAUSSIAN,5)
cv.SetZero(differenceI)
cv.Add(self.__image,differenceI,differenceI,grayI)
return differenceI,grayI
def __getBackGround3(self,threshold,color):
""" Method number 4 to detect the background pixels. Detect the background
comparing each pixel of local image with the given color using the threshold.
(TOO SLOW)
@param threshold: Edge to detect the color background
@type threshold: int
@param color: Color used to detect background
@type color: cvScalar
@return: As first parameter the original image changing the different pixel to black pixel.
As Second parameter a IplImage in Gray Scale where the differences are white pixels. After
this can be used as mask.
"""
def intervale(id1,id2):
if math.fabs(id1-id2) < threshold:
return True
return False
differenceI = cv.CloneMat(self.__image)
size = cv.GetSize(self.__image)
grayI = cv.CreateImage(cv.GetSize(differenceI), cv.IPL_DEPTH_8U, 1)
cv.SetZero(grayI)
for i in range(0, size[0]):
for j in range(0, size[1]):
pixel = cv.Get2D(self.__image,j,i)
if intervale(color[0],pixel[0]) and intervale(color[1],pixel[1]) and intervale(color[2],pixel[2]):
cv.Set2D(differenceI,j,i,cv.Scalar(0,0,0))
cv.Set2D(grayI,j,i,cv.Scalar(255,255,255))
cv.Not(grayI,grayI)
return differenceI,grayI
def trackColor(self, objectColor, threshold, penColor, thickness, lastPoint2,imgScribble,typeS,typeDraw,disable):
""" Applies the technique of tracking to local image. For this it is used the color
of the followed object to draw the track in the image. It is possible apply two differents
techniques to follow the object and also thickness is chosen. (ONLY WEBCAM)
@param objectColor: Color of the used object to detect.
@type objectColor: cvScalar
@param threshold: Threshould detected color of the object.
@type threshold: int [0,255]
@param penColor: Drawing used color
@type penColor: cvScalar
@param thickness: Thickness used to draw.
@type thickness: int [0,255]
@param lastPoint2: Auxiliar Object
@param imgScribble: Image where is stored the tracking way.
@type imgScribble: IplImage
@param typeS: Type of method used to detect the object
@type typeS: int[0,1]
@param typeDraw: If 1 the transparence is disabled.
@param disable: Parameter of pause option
"""
lastPoint = lastPoint2[0]
#penColor = objectColor
imgThresh = None
if typeS == 1:
lowerColor = cv.Scalar(objectColor[0]-threshold,objectColor[1]-threshold,objectColor[2]-threshold)
upperColor = cv.Scalar(objectColor[0]+threshold,objectColor[1]+threshold,objectColor[2]+threshold)
imgThresh = self.__getThresholdImage(lowerColor,upperColor)
elif typeS == 0:
imgThresh = self.__getThresholdImage1(threshold,objectColor)
moments = cv.Moments(imgThresh,1)
moment10 = cv.GetSpatialMoment(moments, 1, 0)
moment01 = cv.GetSpatialMoment(moments, 0, 1);
area = cv.GetCentralMoment(moments, 0, 0);
#print area
if area == 0 or disable == True:
currentPoint = None
else:
currentPoint = [moment10/area,moment01/area]
if lastPoint != None and currentPoint != None:
cv.Line(imgScribble, (int(currentPoint[0]), int(currentPoint[1])), (int(lastPoint[0]), int(lastPoint[1])), penColor, thickness,cv.CV_AA)
mask = cv.CreateImage(cv.GetSize(self.__image), 8, 1)
cv.CvtColor(imgScribble, mask, cv.CV_BGR2GRAY)
cv.Threshold(mask,mask,20, 255, cv.CV_THRESH_BINARY )
cv.Not(mask,mask)
blackI = cv.CreateImage(cv.GetSize(self.__image), 8, 3)
cv.SetZero(blackI)
cv.And(self.__image,self.__image,blackI,mask)
if typeDraw == 0:# Totally Transparent
cv.Add(self.__image, imgScribble, self.__image)
else: # Totally Opaque
cv.Add(blackI, imgScribble, self.__image)
#self.__image = blackI
lastPoint2[0] = currentPoint
## LIMPIAR
def trackClean(self, objectColor, threshold, thickness, lastPoint2,imgScribble):
""" Applies the technique of tracking to local image with the method of to clear.
For this it is used the color of the followed object to show the lowed image.(ONLY WEBCAM)
@param objectColor: Color of the used object to detect.
@type objectColor: cvScalar
@param threshold: Threshould detected color of the object.
@type threshold: int [0,255]
@param thickness: Thickness used to draw.
@type thickness: int [0,255]
@param lastPoint2: Auxiliar Object
@param imgScribble: Image where is stored the tracking way.
@type imgScribble: IplImage
"""
lastPoint = lastPoint2[0]
imgThresh = self.__getThresholdImage1(threshold,objectColor)
moments = cv.Moments(imgThresh,1)
moment10 = cv.GetSpatialMoment(moments, 1, 0)
moment01 = cv.GetSpatialMoment(moments, 0, 1)
area = cv.GetCentralMoment(moments, 0, 0)
#print area
if area == 0:
currentPoint = None
else:
currentPoint = [moment10/area,moment01/area]
if lastPoint != None and currentPoint != None:
cv.Line(imgScribble, (int(currentPoint[0]), int(currentPoint[1])), (int(lastPoint[0]), int(lastPoint[1])), cv.Scalar(250,250,250), thickness,cv.CV_AA)
mask = cv.CreateImage(cv.GetSize(self.__image), 8, 1)
cv.SetZero(mask)
cv.CvtColor(imgScribble, mask, cv.CV_BGR2GRAY)
blackI = cv.CreateImage(cv.GetSize(self.__image), 8, 3)
cv.SetZero(blackI)
cv.And(self.__image,self.__image,blackI,mask)
self.__image = blackI
lastPoint2[0] = currentPoint
# MODO LINTERNA
def trackLamp(self, objectColor, threshold, thickness, lastPoint2):
""" Applies the technique of tracking to local image with the method of to clear.
For this it is used the color of the followed object to show the lowed image only
in the current position. (ONLY WEBCAM)
@param objectColor: Color of the used object to detect.
@type objectColor: cvScalar
@param threshold: Threshould detected color of the object.
@type threshold: int [0,255]
@param thickness: Thickness used to draw.
@type thickness: int [0,255]
@param lastPoint2: Auxiliar Object
"""
lastPoint = lastPoint2[0]
imgThresh = self.__getThresholdImage1(threshold,objectColor)
moments = cv.Moments(imgThresh,1)
moment10 = cv.GetSpatialMoment(moments, 1, 0)
moment01 = cv.GetSpatialMoment(moments, 0, 1);
area = cv.GetCentralMoment(moments, 0, 0);
if area == 0:
currentPoint = None
else:
currentPoint = [moment10/area,moment01/area]
mask = cv.CreateImage(cv.GetSize(self.__image), 8, 1)
cv.SetZero(mask)
if lastPoint != None and currentPoint != None:
cv.Line(mask, (int(currentPoint[0]), int(currentPoint[1])), (int(lastPoint[0]), int(lastPoint[1])), cv.Scalar(250,250,250), thickness,cv.CV_AA)
#cv.CvtColor(imgScribble, mask, cv.CV_BGR2GRAY)
blackI = cv.CreateImage(cv.GetSize(self.__image), 8, 3)
cv.SetZero(blackI)
cv.And(self.__image,self.__image,blackI,mask)
self.__image = blackI
lastPoint2[0] = currentPoint
def __getThresholdImage(self,colorLower,colorUpper):
""" Tecnique number 1 to detect the current possition of a specific color
in the local image.
@param colorLower: This is Low value of the intervale of colour to search.
@param colorUpper: This is Hight value of the intervale of colour to search.
@return The mask where is found the intervale of chosen colours.
"""
imgHSV = cv.CreateImage(cv.GetSize(self.__image), 8, 3)
cv.CvtColor(self.__image, imgHSV, cv.CV_BGR2HSV)
imgResult = cv.CreateImage(cv.GetSize(self.__image), 8, 1)
cv.InRangeS(self.__image, colorLower, colorUpper, imgResult)
return imgResult
def __getThresholdImage1(self,threshold, color):
""" Tecnique number 2 to detect the current possition of a specific color
in the local image.
@param threshold: Allowed threshold to search.
@param color: Value of the color to search.
@return The mask where is found the intervale of chosen colours.
"""
differenceI = cv.CreateImage(cv.GetSize(self.__image), cv.IPL_DEPTH_8U, 3)
grayI = cv.CreateImage(cv.GetSize(differenceI), cv.IPL_DEPTH_8U, 1)
backgroundI = cv.CreateImage(cv.GetSize(self.__image), cv.IPL_DEPTH_8U, 3)
cv.Set(backgroundI,color)
cv.AbsDiff(self.__image,backgroundI,differenceI)
cv.CvtColor(differenceI,grayI,cv.CV_BGR2GRAY)
cv.Smooth(grayI,grayI,cv.CV_BLUR,5)
cv.Smooth(grayI,grayI,cv.CV_MEDIAN,5)
cv.Threshold(grayI,grayI,threshold, 255, cv.CV_THRESH_BINARY )
cv.Not(grayI,grayI)
return grayI