ColorObject.py
2.33 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
import cv2
import numpy as np
from Parameters import *
def IsolateObject(frame ,Lower, Upper):
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, Lower, Upper)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
res = cv2.bitwise_and(frame,frame, mask= mask)
return mask, res
def ColorDisplayResults(liste, nb):
for i in range (0, len(liste)):
if i == 0:
titre = 'mask'+str(nb)
else:
titre = 'res'+str(nb)
cv2.imshow(titre, liste[i])
return
def CombineColors(color1, color2):
mask = cv2.bitwise_or(color1[0], color2[0])
res = cv2.bitwise_or(color1[1], color2[1])
return mask, res
def ColorChoiceProcessing(image, choix):
if choix == '1':
blueMask, blueRes = IsolateObject(image, LowerBlue, UpperBlue)
return [blueMask, blueRes]
elif choix == '2':
greenMask, greenRes = IsolateObject(image, LowerGreen, UpperGreen)
return [greenMask, greenRes]
elif choix == '3':
redMask, redRes = IsolateObject(image, LowerRed, UpperRed)
return[redMask, redRes]
elif choix == '4':
blue = ColorChoiceProcessing(image, '1')
green = ColorChoiceProcessing(image, '2')
bgMask, bgRes = CombineColors(blue, green)
return [bgMask, bgRes]
elif choix == '5':
blue = ColorChoiceProcessing(image, '1')
red = ColorChoiceProcessing(image, '3')
brMask, brRes = CombineColors(blue, red)
return [brMask, brRes]
elif choix == '6':
red = ColorChoiceProcessing(image, '3')
green = ColorChoiceProcessing(image, '2')
rgMask, rgRes = CombineColors(red, green)
return [rgMask, rgRes]
elif choix == '7':
bg = ColorChoiceProcessing(image, '4')
red = ColorChoiceProcessing(image, '3')
bgrMask, bgrRes = CombineColors(bg, red)
return [bgrMask, bgrRes]
else:
return
def ColorChoice() :
image = None
print('\t\tFind Color Object Menu\n')
while (image is None):
image = str(raw_input('\tImage to use ? By default couleur.png \n'))
if not image:
image = 'couleur.png'
image = cv2.imread(str(image))
print ('\t1. Blue\n\t2. Green\n\t3. Red\n\t4. Blue Green\n\t5. Blue Red\n\t6. Red Green\n\t7. Blue Green Red\n')
choix = raw_input('\n\tMultiple choices possible\n')
for i in range (0, len(choix)):
liste = ColorChoiceProcessing(image, choix[i])
if liste :
ColorDisplayResults(liste,choix[i])
cv2.waitKey(0)
cv2.destroyAllWindows()
return