-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect_sign_from_bgr_img.py
111 lines (91 loc) · 3.92 KB
/
detect_sign_from_bgr_img.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
import cv2
import numpy as np
import sys, os
def filterMasking(img, hsv, lower, upper):
"""
入力画像に色フィルターを適用し、マスク画像を生成する関数
:param img: 入力画像(BGR)。これにマスク画像を適用する。
:param hsv: 入力画像(HSV)。これに色フィルターを適用する。
:param lower: 色フィルターの下限値。
:param upper: 色フィルターの上限値。
:return result: 色フィルターによって、指定の色だけが残された画像(BGR)。
:return mask: 生成したマスク画像。
"""
mask = cv2.inRange(hsv, lower, upper) # HSV画像に、HSVで指定した色フィルターを適用してマスク画像を生成する。
result = cv2.bitwise_and(img, img, mask=mask) # 生成したマスク画像をBGR画像に適用し、特定の色のピクセルだけを残す。
return result, mask
"""
色の条件指定(フィルター設定)
Upper: 上限値
Lower: 下限値
B,G,R の順で指定する
# 今回使用する標識の色の理論値は以下の通り
# ( B, G, R) | ( H, S, V)
# (255, 0, 255) | (150, 255, 255) # right
# (255, 0, 127) | (135, 255, 255) # left
# ( 0, 127, 255) | ( 15, 255, 255) # stop
# (255, 0, 0) | (120, 255, 255) # 速度100
# ( 0, 255, 0) | (110, 255, 255) # 速度50
# ( 0, 0, 255) | ( 0, 255, 255) # 速度10
"""
# 「右に曲がれ」標識
rightFilterUpper = np.array([255, 10, 255])
rightFilterLower = np.array([200, 0, 200])
# 「左に曲がれ」標識
leftFilterUpper = np.array([255, 10, 135])
leftFilterLower = np.array([200, 0, 100])
# 「止まれ」標識
stopFilterUpper = np.array([10, 135, 255])
stopFilterLower = np.array([0, 100, 200])
# 「速度を100%にしろ」標識
speed100FilterUpper = np.array([255, 10, 10])
speed100FilterLower = np.array([200, 0, 0])
# 「速度を50%にしろ」標識
speed50FilterUpper = np.array([10, 255, 10])
speed50FilterLower = np.array([10, 200, 0])
# 「速度を10%にしろ」標識
speed10FilterUpper = np.array([10, 10, 255])
speed10FilterLower = np.array([0, 0, 200])
# 動作確認用の画像を読み込む
img = cv2.imread("data/sample/1_left.jpg", 1)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # BGRからHSVに変換
"""
各標識のフィルターを適用して、マスク画像と処理結果を得る
"""
speed100Result, speed100Mask = filterMasking(img, hsv, speed100FilterLower, speed100FilterUpper)
speed50Result, speed50Mask = filterMasking(img, hsv, speed50FilterLower, speed50FilterUpper)
speed10Result, speed10Mask = filterMasking(img, hsv, speed10FilterLower, speed10FilterUpper)
rightResult, rightMask = filterMasking(img, hsv, rightFilterLower, rightFilterUpper)
leftResult, leftMask = filterMasking(img, hsv, leftFilterLower, leftFilterUpper)
stopResult, stopMask = filterMasking(img, hsv, stopFilterLower, stopFilterUpper)
"""
各マスク画像に写った白色の領域の面積を求め、しきい値を超えていれば、その標識が写っている判定を出す
(面積と言うが、正確にはマスク画像に写った白色のピクセルの個数で判定している)
"""
if speed100Mask.sum() > 205000:
print("speed100Mask", speed100Mask.sum())
cv2.imshow("", speed100Result)
cv2.waitKey(0)
if speed50Mask.sum() > 205000:
print("speed50Mask", speed50Mask.sum())
cv2.imshow("", speed50Result)
cv2.waitKey(0)
if speed10Mask.sum() > 205000:
print("speed10Mask", speed10Mask.sum())
cv2.imshow("", speed10Result)
cv2.waitKey(0)
if rightMask.sum() > 205000:
print("rightMask", rightMask.sum())
cv2.imshow("", rightResult)
cv2.waitKey(0)
if leftMask.sum() > 205000:
print("leftMask", leftMask.sum())
cv2.imshow("", leftResult)
cv2.waitKey(0)
if stopMask.sum() > 205000:
print("stopMask", stopMask.sum())
cv2.imshow("", stopResult)
cv2.waitKey(0)
# cv2.imshow("", leftResult)
# cv2.waitKey(0)
cv2.destroyAllWindows()