nemain.py 1012 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import cv2
  2. import numpy as np
  3. camera = cv2.VideoCapture(0)
  4. # camera.set(cv2.CAP_PROP_AUTO_EXPOSURE, 1)
  5. # camera.set(cv2.CAP_PROP_EXPOSURE, -5)
  6. cv2.namedWindow('Image', cv2.WINDOW_GUI_NORMAL)
  7. cv2.namedWindow('Debug', cv2.WINDOW_GUI_NORMAL)
  8. lower = 100
  9. upper = 200
  10. def lower_update(value):
  11. global lower
  12. lower = value
  13. def upper_update(value):
  14. global upper
  15. upper = value
  16. cv2.createTrackbar("Lower", "Debug", lower, 255, lower_update)
  17. cv2.createTrackbar("Upper", "Debug", upper, 255, upper_update)
  18. while camera.isOpened():
  19. ret, image = camera.read()
  20. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  21. gray = cv2.GaussianBlur(gray, (7,7), 0)
  22. mask = cv2.Canny(gray, lower, upper)
  23. cnts, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  24. cv2.drawContours(image, cnts, -1, (0,255,0), 10)
  25. print(mask.shape)
  26. print(image.shape)
  27. cv2.imshow('Image', image)
  28. cv2.imshow('Debug', mask)
  29. key = cv2.waitKey(10)
  30. if key == ord('q'):
  31. break