From 543ce81c434395b6d368678a8d1f773d68067587 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Efe=20Ayd=C4=B1n?= <3feaydin@gmail.com> Date: Sat, 4 May 2019 00:34:34 +0300 Subject: [PATCH] ambulance --- traffic_analyzer/white_mask.py | 47 ++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 traffic_analyzer/white_mask.py diff --git a/traffic_analyzer/white_mask.py b/traffic_analyzer/white_mask.py new file mode 100644 index 0000000..dd4dbe6 --- /dev/null +++ b/traffic_analyzer/white_mask.py @@ -0,0 +1,47 @@ +import cv2 +import time +import numpy as np + +cap = cv2.VideoCapture(1) + +def threshold_slow(image): + h = image.shape[0] + w = image.shape[1] + for y in range(0, h): + for x in range(0, w): + if np.any(image[y, x] != 0): + return True + + return False + +while True: + ret, frame = cap.read() + frame_org = frame + frame = frame[100:300, 100:300] + hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) + sensitivity = 40 + lower_white = np.array([0,0,255-sensitivity]) + upper_white = np.array([255,sensitivity,255]) + mask = cv2.inRange(hsv, lower_white, upper_white) + res = cv2.bitwise_and(frame,frame, mask=mask) + + res = cv2.erode(res, None, iterations=2) + res = cv2.dilate(res, None, iterations=4) + + mask = cv2.erode(mask, None, iterations=2) + mask = cv2.dilate(mask, None, iterations=4) + + if threshold_slow(res): + cv2.rectangle(frame_org, (100, 100), (300, 300), (255, 0, 0), 2) + + cv2.imshow('frame', frame) + cv2.imshow('org', frame_org) + cv2.imshow('mask', mask) + cv2.imshow('res', res) + + k = cv2.waitKey(5) & 0xFF + if k == ord('q'): + break + +cap.release() +cv2.destroyAllWindows() \ No newline at end of file