#!/usr/bin/python import cv2 import numpy as np cap = cv2.VideoCapture(0) while True: _, frame = cap.read() hsv = cv2.cvtColor(frame , cv2.COLOR_BGR2HSV) l_red = np.array([150,150,50]) u_red = np.array([180,255,150]) mask = cv2.inRange(hsv , l_red , u_red) res = cv2.bitwise_and(frame , frame , mask = mask) cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] # print frame # print cnts center = None # only proceed if at least one contour was found # if len(cnts) > 0: # find the largest contour in the mask, then use # it to compute the minimum enclosing circle and # centroid try: areas = [cv2.contourArea(c) for c in cnts] max_index = np.argmax(areas) # print max_index cnt=cnts[max_index] x,y,w,h = cv2.boundingRect(cnt) print "X = ",x," Y=",y," w=",w," h= ",h cv2.rectangle