Advertisement
Guest User

Vision

a guest
Dec 13th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.92 KB | None | 0 0
  1. import cv2 as cv
  2. cap = cv.VideoCapture(0)
  3. while True:
  4.     ret, frame = cap.read()
  5.     cv.imshow('Frame', frame)
  6.     hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
  7.     hsv = cv.blur(hsv, (5, 5))
  8.     mask = cv.inRange(hsv,(89, 124, 73), (255, 255, 255))  # bgr: 45 123 73 | 89 124 73
  9.     cv.imshow('Mask', mask)
  10.     mask = cv.erode(mask, None, iterations=2)
  11.     mask = cv.dilate(mask, None, iterations=4)
  12.     cv.imshow('Mask2', mask)
  13.     contours = cv.findContours(mask, cv.RETR_TREE, cv.CHAIN_APPROX_NONE)
  14.     contours = contours[1]
  15.     if contours:
  16.         contours = sorted(contours, key=cv.contourArea, reverse=True)
  17.         cv.drawContours(frame, contours, 0, (255, 0, 255), 3)
  18.         cv.imshow('Contours', frame)
  19.         (x, y, w, h) = cv.boundingRect(contours[0])
  20.         cv.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
  21.        
  22.     if cv.waitKey(1) == ord('q'):
  23.         break
  24. cap.release()
  25. cv.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement