-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pattern_Matching.py
161 lines (131 loc) · 6.19 KB
/
Pattern_Matching.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
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_COMPLEX ##Font style for writing text on video frame
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280) ##Set camera resolution
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
x1 = 750 ##450
x2 = 930 ##630
y1 = 100
y2 = 300
Result_Count1 = 0
Result_Count2 = 0
Result_Count3 = 0
Result_Count4 = 0
Pattern_Matrix = np.zeros((9, 9), np.uint8)
Pattern_1 = np.array([ [0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 255, 255, 255, 255, 255, 255, 255, 0],
[0, 255, 255, 255, 0, 0, 0, 255, 0],
[0, 255, 255, 255, 255, 255, 255, 255, 0],
[0, 255, 255, 255, 0, 255, 255, 255, 0],
[0, 255, 255, 255, 255, 255, 255, 255, 0],
[0, 255, 0, 0, 0, 255, 255, 255, 0],
[0, 255, 255, 255, 255, 255, 255, 255, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
], dtype=np.uint8)
Pattern_2 = np.array([ [0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 255, 255, 255, 255, 255, 255, 255, 0],
[0, 255, 0, 255, 255, 255, 0, 255, 0],
[0, 255, 255, 0, 255, 0, 255, 255, 0],
[0, 255, 255, 255, 0, 255, 255, 255, 0],
[0, 255, 255, 0, 255, 0, 255, 255, 0],
[0, 255, 0, 255, 255, 255, 0, 255, 0],
[0, 255, 255, 255, 255, 255, 255, 255, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
], dtype=np.uint8)
Pattern_3 = np.array([ [0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 255, 255, 255, 255, 255, 255, 255, 0],
[0, 255, 0, 0, 0, 0, 0, 255, 0],
[0, 255, 0, 255, 255, 255, 0, 255, 0],
[0, 255, 0, 255, 255, 255, 0, 255, 0],
[0, 255, 0, 255, 255, 255, 0, 255, 0],
[0, 255, 0, 0, 0, 0, 0, 255, 0],
[0, 255, 255, 255, 255, 255, 255, 255, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
], dtype=np.uint8)
Pattern_4 = np.array([ [0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 255, 255, 255, 255, 255, 0, 255, 0],
[0, 255, 0, 255, 255, 255, 0, 255, 0],
[0, 255, 255, 255, 255, 255, 0, 255, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 255, 0, 255, 255, 255, 255, 255, 0],
[0, 255, 0, 255, 255, 255, 0, 255, 0],
[0, 255, 0, 255, 255, 255, 255, 255, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
], dtype=np.uint8)
while 1:
ret, frame = cap.read() ##Read image frame
frame = cv2.flip(frame, +1) ##Mirror image frame
if not ret: ##If frame is not read then exit
break
if cv2.waitKey(1) == ord('s'): ##While loop exit condition
break
frame2 = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('Gray Image', frame2)
ret, thresh1 = cv2.threshold(frame2, 150, 255, cv2.THRESH_BINARY)
cv2.imshow('Binary Image', thresh1)
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 3)
width = int((x2-x1)/9)
height = int((y2-y1)/9)
W1 = x1
H1 = y1
W2 = x1 + width
H2 = y1 + height
for i in range(0, 9):
for j in range(0, 9):
Sum = np.sum(thresh1[H1: H2, W1:W2]) ##56100
if Sum > 56100:
Pattern_Matrix[i, j] = 255
else:
Pattern_Matrix[i, j] = 0
W1 = W2
W2 = W2 + width
#print('W1 = ' +str(W1))
#print('W2 = ' +str(W2))
#print('H1 = ' +str(H1))
#print('H2 = ' +str(H2))
#print("Sum = "+str(Sum))
Sum = 0
W1 = x1
W2 = x1 + width
H1 = H2
H2 = H2 + height
#print(Pattern_Matrix)
#print("width="+str(width))
#print("height="+str(height))
##*******************Pattern Comparison*********************##
for a in range(0, 9):
for b in range (0, 9):
if Pattern_Matrix[a, b] == Pattern_1[a, b]:
Result_Count1 = Result_Count1 + 1
if Pattern_Matrix[a, b] == Pattern_2[a, b]:
Result_Count2 = Result_Count2 + 1
if Pattern_Matrix[a, b] == Pattern_3[a, b]:
Result_Count3 = Result_Count3 + 1
if Pattern_Matrix[a, b] == Pattern_4[a, b]:
Result_Count4 = Result_Count4 + 1
##print(Result_Count)
Match_Percentage1 = (Result_Count1 / 81) * 100
Match_Percentage2 = (Result_Count2 / 81) * 100
Match_Percentage3 = (Result_Count3 / 81) * 100
Match_Percentage4 = (Result_Count4 / 81) * 100
Result_Count1 = 0
Result_Count2 = 0
Result_Count3 = 0
Result_Count4 = 0
if Match_Percentage1 > 90:
S = "Pattern 1 " + str(round(Match_Percentage1, 1)) + '%'
cv2.putText(frame, S, (5, 50), font, 2, (0, 0, 255), 2, cv2.LINE_AA)
elif Match_Percentage2 > 90:
S = "Pattern 2 " + str(round(Match_Percentage2, 1)) + '%'
cv2.putText(frame, S, (5, 50), font, 2, (0, 0, 255), 2, cv2.LINE_AA)
elif Match_Percentage3 > 90:
S = "Pattern 3 " + str(round(Match_Percentage3, 1)) + '%'
cv2.putText(frame, S, (5, 50), font, 2, (0, 0, 255), 2, cv2.LINE_AA)
elif Match_Percentage4 > 90:
S = "Pattern 4 " + str(round(Match_Percentage4, 1)) + '%'
cv2.putText(frame, S, (5, 50), font, 2, (0, 0, 255), 2, cv2.LINE_AA)
else:
cv2.putText(frame, "No Match Found", (5, 50), font, 2, (0, 0, 255), 2, cv2.LINE_AA)
cv2.imshow('Original Image', frame)
cv2.imshow('Pattern Template', thresh1[y1:y2, x1:x2])