1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| import cv2 import numpy as np cat = cv2.VideoCapture(0) low_h = np.array([93, 100, 90]) high_h = np.array([103, 160, 130]) kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5)) def corner(img): hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV) ghsv = cv2.GaussianBlur(hsv, (3, 3), 0) mask = cv2.inRange(ghsv, low_h, high_h) mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel) mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel) contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) return contours def draw(contours, img): for c in contours: x, y, w, h = cv2.boundingRect(c) if cv2.contourArea(c) > 400: print('x:',x,'y:',y) cv2.rectangle(img, (x, y), (x+w, y+h), (0, 0, 255), 2) else: continue return img while True: ret, frame = cat.read() if not ret: break contours = corner(frame) img = draw(contours, frame) cv2.imshow('img', img) if cv2.waitKey(10) == ord('q'): break cv2.destroyAllWindows()
|