Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2 as cv
- cap = cv.VideoCapture(0)
- while True:
- ret, frame = cap.read()
- cv.imshow('Frame', frame)
- hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
- hsv = cv.blur(hsv, (5, 5))
- mask = cv.inRange(hsv,(89, 124, 73), (255, 255, 255)) # bgr: 45 123 73 | 89 124 73
- cv.imshow('Mask', mask)
- mask = cv.erode(mask, None, iterations=2)
- mask = cv.dilate(mask, None, iterations=4)
- cv.imshow('Mask2', mask)
- contours = cv.findContours(mask, cv.RETR_TREE, cv.CHAIN_APPROX_NONE)
- contours = contours[1]
- if contours:
- contours = sorted(contours, key=cv.contourArea, reverse=True)
- cv.drawContours(frame, contours, 0, (255, 0, 255), 3)
- cv.imshow('Contours', frame)
- (x, y, w, h) = cv.boundingRect(contours[0])
- cv.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
- if cv.waitKey(1) == ord('q'):
- break
- cap.release()
- cv.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement