You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.1 KiB

6 years ago
  1. import cv2
  2. import time
  3. import numpy as np
  4. cap = cv2.VideoCapture(1)
  5. def threshold_slow(image):
  6. h = image.shape[0]
  7. w = image.shape[1]
  8. for y in range(0, h):
  9. for x in range(0, w):
  10. if np.any(image[y, x] != 0):
  11. return True
  12. return False
  13. while True:
  14. ret, frame = cap.read()
  15. frame_org = frame
  16. frame = frame[100:300, 100:300]
  17. hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
  18. sensitivity = 40
  19. lower_white = np.array([0,0,255-sensitivity])
  20. upper_white = np.array([255,sensitivity,255])
  21. mask = cv2.inRange(hsv, lower_white, upper_white)
  22. res = cv2.bitwise_and(frame,frame, mask=mask)
  23. res = cv2.erode(res, None, iterations=2)
  24. res = cv2.dilate(res, None, iterations=4)
  25. mask = cv2.erode(mask, None, iterations=2)
  26. mask = cv2.dilate(mask, None, iterations=4)
  27. if threshold_slow(res):
  28. cv2.rectangle(frame_org, (100, 100), (300, 300), (255, 0, 0), 2)
  29. cv2.imshow('frame', frame)
  30. cv2.imshow('org', frame_org)
  31. cv2.imshow('mask', mask)
  32. cv2.imshow('res', res)
  33. k = cv2.waitKey(5) & 0xFF
  34. if k == ord('q'):
  35. break
  36. cap.release()
  37. cv2.destroyAllWindows()