Last active
July 10, 2019 07:46
-
-
Save jw910731/174736ed7fb09516bbaed49c8efb174b to your computer and use it in GitHub Desktop.
FRC Stuff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import cv2 | |
| import numpy as np | |
| import os | |
| vidSrc = 0 | |
| # Capture Video | |
| cap = cv2.VideoCapture(vidSrc) | |
| os.system("./vidSetup.sh "+str(vidSrc)) | |
| # Color filter range | |
| lowG = np.array([40,100,100]) | |
| highG = np.array([75,255,255]) | |
| # Min area of a object to be recognized | |
| minArea = 500 | |
| def areaFilter(inputContours, minArea): | |
| ret = [] | |
| for contour in inputContours: | |
| area = cv2.contourArea(contour) | |
| if area >= minArea: | |
| ret.append(contour) | |
| return ret | |
| def genShape(contour): | |
| ret = [] | |
| for cont in contour: | |
| ret.append(cv2.minAreaRect(cont)) | |
| return ret | |
| def maskGen(input,low, high): | |
| hsv = cv2.cvtColor(input, cv2.COLOR_RGB2HSV) | |
| mask = cv2.inRange(hsv, low, high) | |
| return mask | |
| def getCountors(mask, minArea): | |
| _, contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) | |
| filtered = areaFilter(contours,minArea) | |
| return contours, filtered | |
| while True: | |
| #Capture Frame | |
| _, src = cap.read() | |
| cv2.imshow('origin',src) | |
| # Mask making | |
| mask= maskGen(src, lowG, highG) | |
| # Mask showing | |
| mask_view = cv2.bitwise_and(src, src, mask = mask) | |
| cv2.imshow('Mask', mask_view) | |
| # Mask Proccessing | |
| cont, filt = getCountors(mask, minArea) | |
| rects = genShape(filt) | |
| # Show final image | |
| opt = src.copy() | |
| cv2.drawContours(opt, cont, -1, (255,0, 0), 1) | |
| cv2.drawContours(opt, filt, -1, (255,255,0), 3) | |
| for rect in rects: | |
| cv2.circle(opt, (int(rect[0][0]),int(rect[0][1])), 7, (0, 0, 0), -1) | |
| cv2.drawContours(opt, [np.int0(cv2.boxPoints(rect))], -1, (0,0,0), 2) | |
| print(rect) | |
| cv2.imshow('Result', opt) | |
| if cv2.waitKey(1) & 0xff == ord('q'): | |
| break | |
| cap.release() | |
| cv2.destroyAllWindows() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import cv2 | |
| import numpy as np | |
| def areaFilter(inputContours, minArea): | |
| ret = [] | |
| for contour in inputContours: | |
| area = cv2.contourArea(contour) | |
| if area >= minArea: | |
| ret.append(contour) | |
| return ret | |
| def genShape(contour): | |
| ret = [] | |
| for cont in contour: | |
| ret.append(cv2.minAreaRect(cont)) | |
| return ret | |
| def maskGen(input,low, high): | |
| hsv = cv2.cvtColor(input, cv2.COLOR_BGR2HSV) | |
| mask = cv2.inRange(hsv, low, high) | |
| return mask | |
| def getCountors(mask, minArea): | |
| _, contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) | |
| filtered = areaFilter(contours,minArea) | |
| return contours, filtered | |
| # Capture Video | |
| cap = cv2.VideoCapture(0) | |
| # Color filter range | |
| lowG = np.array([50,100,100]) | |
| highG = np.array([70,255,255]) | |
| # Min area of a object to be recognized | |
| minPercent = 0.1 | |
| minArea = cap.get(cv2.CAP_PROP_FRAME_WIDTH) * cap.get(cv2.CAP_PROP_FRAME_HEIGHT) * minPercent | |
| while True: | |
| #Capture Frame | |
| _, src = cap.read() | |
| # Mask making | |
| mask= maskGen(src, lowG, highG) | |
| # Mask showing | |
| mask_view = cv2.bitwise_and(src, src, mask = mask) | |
| cv2.imshow('Mask', mask_view) | |
| # Mask Proccessing | |
| cont, filt = getCountors(mask, minArea) | |
| rects = genShape(filt) | |
| # Show final image | |
| opt = src.copy() | |
| cv2.drawContours(opt, cont, -1, (255,0, 0), 1) | |
| cv2.drawContours(opt, filt, -1, (255,255,0), 3) | |
| for rect in rects: | |
| cv2.circle(opt, (int(rect[0][0]),int(rect[0][1])), 7, (0, 0, 0), -1) | |
| cv2.drawContours(opt, [np.int0(cv2.boxPoints(rect))], -1, (0,0,0), 2) | |
| print(rect) | |
| cv2.imshow('Result', opt) | |
| if cv2.waitKey(1) & 0xff == ord('q'): | |
| break | |
| cap.release() | |
| cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment