Created
May 24, 2014 17:13
-
-
Save kevinmartinjos/9c8755549f125e73e129 to your computer and use it in GitHub Desktop.
Tracks the color red. Crude and ineffective. LOT of room for improvement
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 time | |
| import numpy as np | |
| from pymouse import PyMouse | |
| class ColourTracker: | |
| def __init__(self): | |
| cv2.namedWindow("colourTrackerWindow",cv2.CV_WINDOW_AUTOSIZE) | |
| cv2.namedWindow("alternateWindow",cv2.CV_WINDOW_AUTOSIZE) | |
| self.capture=cv2.VideoCapture(1) | |
| self.scale_down=1 | |
| self.m=PyMouse() | |
| time.sleep(5) | |
| def run(self): | |
| while True: | |
| f,orig_img=self.capture.read() | |
| orig_img2=cv2.flip(orig_img,1) | |
| img=cv2.GaussianBlur(orig_img,(5,5),0) | |
| #img=cv2.resize(img,(img.shape[0]/self.scale_down,img.shape[1]/self.scale_down)) | |
| img=cv2.cvtColor(img,cv2.COLOR_BGR2HSV) | |
| red_lower=np.array([0,150,0],np.uint8) | |
| red_upper=np.array([5,255,255],np.uint8) | |
| red_binary=cv2.inRange(img,red_lower,red_upper) | |
| dilation=np.ones((15,15),"uint8") | |
| red_binary=cv2.dilate(red_binary,dilation) | |
| #red_binary2=red_binary Because contours modify the original image | |
| contours,hierarchy=cv2.findContours(red_binary,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) | |
| max_area=0 | |
| largest_contour=None | |
| #term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 ) | |
| #track_window=(0,0,100,100) | |
| #track_box,track_window=cv2.CamShift(red_binary,track_window,term_crit) | |
| #print track_box | |
| for idx,contour in enumerate(contours): | |
| area=cv2.contourArea(contour.astype('int')) | |
| #area=0 | |
| if area>max_area: | |
| largest_contour=contour | |
| if not largest_contour==None: | |
| moment=cv2.moments(largest_contour) | |
| if moment["m00"]>1000/self.scale_down: | |
| (x,y),radius=cv2.minEnclosingCircle(largest_contour) | |
| center=(int(x),int(y)) | |
| center_x=center[0] | |
| center_y=center[1] | |
| radius=int(radius) | |
| cv2.circle(orig_img,(center[0]*self.scale_down,center[1]*self.scale_down),radius*self.scale_down,(0,255,0),2) | |
| self.mouse_scale_x=1376/orig_img.shape[0] | |
| self.mouse_scale_y=1376/orig_img.shape[1] | |
| print orig_img.shape[0],orig_img.shape[1] | |
| if center_x<200: | |
| self.m.release(1376-center_x*self.mouse_scale_x,center_y*self.mouse_scale_y) | |
| center_x=200/self.mouse_scale_x | |
| elif center_x>1200: | |
| self.m.release(1376-center_x*self.mouse_scale_x,center_y*self.mouse_scale_y) | |
| center_x=1200/self.mouse_scale_x | |
| if center_y<200: | |
| self.m.release(1376-center_x*self.mouse_scale_x,center_y*self.mouse_scale_y) | |
| center_y=200/self.mouse_scale_y | |
| self.m.press(1376-center_x*self.mouse_scale_x,center_y*self.mouse_scale_y) | |
| cv2.imshow('colourTrackerWindow',orig_img) | |
| cv2.imshow('alternateWindow',red_binary) | |
| if cv2.waitKey(2)==27: | |
| break | |
| colour_tracker=ColourTracker() | |
| colour_tracker.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment