OpenCV实验 - Pengesan Jalur Gemilang
利用OpenCV的色容差检测功能,做到了马来西亚国旗——辉煌条纹(Jalur Gemilang)的检测
当前功能仅有检测到Jalur Gemilang时,冻结那一帧,之后可能会再添加一些2333的功能。
由于该程序目标为马来西亚人,所以提示语也采用了马来文🤣
实验效果
# pjg.py V3
import cv2
import numpy as np
lower_red1 = np.array([0, 100, 100])
upper_red1 = np.array([10, 255, 255])
lower_red2 = np.array([160, 100, 100])
upper_red2 = np.array([180, 255, 255])
lower_white = np.array([0, 0, 200])
upper_white = np.array([180, 30, 255])
lower_yellow = np.array([20, 100, 100])
upper_yellow = np.array([30, 255, 255])
lower_blue = np.array([100, 100, 100])
upper_blue = np.array([130, 255, 255])
def dF(frame):
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
mask_red = cv2.inRange(hsv, lower_red1, upper_red1)
mask_red2 = cv2.inRange(hsv, lower_red2, upper_red2)
mask_white = cv2.inRange(hsv, lower_white, upper_white)
mask_blue = cv2.inRange(hsv, lower_blue, upper_blue)
mask_yellow = cv2.inRange(hsv, lower_yellow, upper_yellow)
mask = cv2.bitwise_or(mask_red, mask_red2, mask_white)
mask = cv2.bitwise_or(mask, mask_blue)
mask = cv2.bitwise_or(mask, mask_yellow)
if cv2.countNonZero(mask_red) > 100 and cv2.countNonZero(mask_white) > 100 and cv2.countNonZero(mask_blue) > 100 and cv2.countNonZero(mask_yellow) > 100:
return True
cap = cv2.VideoCapture(0)
print("Pengesan Jalur Gemilang\nVersi Aplikasi: V3\nMasukkan 'q' untuk keluar\n")
while True:
ret, frame = cap.read()
if dF(frame):
print('Unsur Jalur Gemilang dikesankan!!!')
cv2.waitKey(0)
cv2.imshow('Pengesan Jalur Gemilang', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if cv2.getWindowProperty('Pengesan Jalur Gemilang', cv2.WND_PROP_VISIBLE) < 1:
cap.release()
cv2.destroyAllWindows()
break