color2.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import cv2
  2. import numpy as np
  3. cap = cv2.VideoCapture(0)
  4. while(1):
  5. _, frame = cap.read()
  6. #hsv or rgb
  7. hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
  8. rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
  9. lower_red = np.array([0,120,82])
  10. upper_red = np.array([15,255,249])
  11. mask = cv2.inRange(hsv, lower_red, upper_red)
  12. color_image = cv2.circle(mask, (1, 1), 1, (0, 255, 255), -1)
  13. res = cv2.bitwise_and(frame,frame, mask= mask)
  14. point_size = 4
  15. point_color = (0, 0, 255) # BGR
  16. thickness = 4
  17. b = 239
  18. w = 0
  19. for i in range(32):
  20. w = w + 19
  21. coordinate = hsv[b, w] # 輸入要取得顏色的指定座標
  22. point = (w, b)
  23. cv2.circle(color_image, point, point_size, point_color, thickness)
  24. if coordinate[0] == 0:
  25. print('Is red')
  26. else:
  27. print('Is green')
  28. print(w, b)
  29. #cv2.imshow('frame',frame)
  30. #cv2.imwrite('C:/Users/User/Desktop/company_files/coffee_case/opencv/1.png', color_image)
  31. cv2.imshow('mask',color_image)
  32. #cv2.imshow('res',res)
  33. k = cv2.waitKey(5) & 0xFF
  34. if k == 27:
  35. break
  36. cv2.destroyAllWindows()