-
-
Save Psidium/46f7d119c22c9edbcb4a to your computer and use it in GitHub Desktop.
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, math | |
| import numpy as np | |
| class ColourTracker: | |
| def __init__(self): | |
| #this creates the window you'll use to show the data | |
| cv2.namedWindow("ColourTrackerWindow", cv2.CV_WINDOW_AUTOSIZE) | |
| self.capture = cv2.VideoCapture(0) | |
| self.capture.set(3,320) | |
| self.capture.set(4,280) | |
| #this sets a scale to down the data (i.e. set to 2 to process a image half the original size) | |
| self.scale_down = 1 | |
| def run(self): | |
| while True: | |
| #this gets a frame from the webcam | |
| f, orig_img = self.capture.read() | |
| orig_img = cv2.flip(orig_img, 1) | |
| #this blurs the image ( what is blur? http://www192.lunapic.com/editor/?action=blur) | |
| img = cv2.GaussianBlur(orig_img, (5,5), 0) | |
| #this converts from BGR to HSV (hue, color, saturation) | |
| img = cv2.cvtColor(orig_img, cv2.COLOR_BGR2HSV) | |
| #this scales the image down, but you are not using it! I'm puttin a 'if' if you ever change the scaledown factor | |
| if self.scale_down != 1: | |
| img = cv2.resize(img, (len(orig_img[0]) / self.scale_down, len(orig_img) / self.scale_down)) | |
| #this defines the lower threshold on witch it'll detect the color (o limiar, a faixa na qual ele vai pega a cor) | |
| red_lower = np.array([110, 100, 100],np.uint8) | |
| #same as above, but the upper threshold | |
| red_upper = np.array([130, 255, 255],np.uint8) | |
| #extrai a imagem so com as cores definidas no limiar | |
| red_binary = cv2.inRange(img, red_lower, red_upper) | |
| #dilates the image with a 15x15 matrix, imagine it'll leave only the strong lines (https://www.softintegration.com/products/thirdparty/opencv/demos/figure/morphology.gif) | |
| dilation = np.ones((15, 15), "uint8") | |
| red_binary = cv2.dilate(red_binary, dilation) | |
| #this extracts the contour of every object in the picture | |
| contours, hierarchy = cv2.findContours(red_binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) | |
| #prepare for the for | |
| max_area = 0 | |
| largest_contour = None | |
| #iterate over all the countous to find the biggest | |
| for idx, contour in enumerate(contours): | |
| area = cv2.contourArea(contour) | |
| if area > max_area: | |
| max_area = area | |
| largest_contour = contour | |
| #if you have found any contour | |
| if not largest_contour == None: | |
| #???? i don't know what a moment is | |
| moment = cv2.moments(largest_contour) | |
| if moment["m00"] > 1000 / self.scale_down: | |
| #extract the surrouding retangle arount the biggest contour | |
| rect = cv2.minAreaRect(largest_contour) | |
| #extract the center point of this retangle | |
| box_center_point = (rect[0][0] * self.scale_down, rect[0][1] * self.scale_down) | |
| #extract it width | |
| box_width = rect[1][0] * self.scale_down | |
| #extract its height | |
| box_height = rect[1][1] * self.scale_down | |
| #extract its angular rotation | |
| box_rotation_ang_in_rad = rect[2] | |
| #creates the box to print on the screen | |
| rect = (box_center_point, (box_width, box_height), box_rotation_ang_in_rad) | |
| box = cv2.cv.BoxPoints(rect) | |
| box = np.int0(box) | |
| #draw the box on your original frame | |
| cv2.drawContours(orig_img,[box], 0, (0, 0, 255), 2) | |
| #show to the user | |
| cv2.imshow("ColourTrackerWindow", orig_img) | |
| #wait for ESC to be pressed | |
| if cv2.waitKey(20) == 27: | |
| cv2.destroyWindow("ColourTrackerWindow") | |
| self.capture.release() | |
| break | |
| if __name__ == "__main__": | |
| colour_tracker = ColourTracker() | |
| colour_tracker.run() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment