Advertisement
MagicWinnie

Colour

Jun 3rd, 2019
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.14 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. cap = cv2.VideoCapture(0)
  4. #BGR
  5. luc = [
  6.     ([17, 15, 100], [50, 56, 200],'RED'),
  7.     ([86, 31, 4], [220, 88, 50], "B"),
  8.     ([25, 146, 190], [62, 174, 250],"Y"),
  9.     ([103, 86, 65], [145, 133, 128],"GRAY"),
  10.     ([40, 210, 100],[40, 200, 30], "GREEN")
  11. ]
  12. while(cap.isOpened):
  13.     _, frame = cap.read()
  14.     for (lower, upper, colour) in luc:
  15.         lower = np.array(lower, dtype = "uint8")
  16.         upper = np.array(upper, dtype = "uint8")
  17.         mask = cv2.inRange(frame, lower, upper)
  18.         kernel = np.ones((5,5),'int')
  19.         dilated = cv2.dilate(mask,kernel)
  20.         res = cv2.bitwise_and(frame,frame, mask=mask)
  21.         ret,thrshed = cv2.threshold(cv2.cvtColor(res,cv2.COLOR_BGR2GRAY),3,255,cv2.THRESH_BINARY)
  22.         contours,hier = cv2.findContours(thrshed,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
  23.         for cnt in contours:
  24.             area = cv2.contourArea(cnt)
  25.             if area > 100:
  26.                 print(colour)
  27.         # cv2.imshow("frames", np.hstack([frame, res]))
  28.         cv2.imshow("frame", frame)
  29.     k = cv2.waitKey(5) & 0xFF
  30.     if k == 27:
  31.         break
  32. cap.release()
  33. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement