From b252af251933f6d2a11d1661ef3577529159781a Mon Sep 17 00:00:00 2001 From: Jenil Shah Date: Thu, 20 Feb 2014 11:25:31 +0530 Subject: [PATCH 1/2] Exception added and some documentation --- software/firmware/cam.py | 75 ++++++++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 26 deletions(-) diff --git a/software/firmware/cam.py b/software/firmware/cam.py index cf824e7..88d2462 100644 --- a/software/firmware/cam.py +++ b/software/firmware/cam.py @@ -1,56 +1,79 @@ import cv2 import numpy as np + +# Starts Webcam cap = cv2.VideoCapture(0) + +# Performs Blurring,Thresholding and Edge detection while( cap.isOpened() ) : ret,img = cap.read() gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) blur = cv2.GaussianBlur(gray,(5,5),0) ret,thresh1 = cv2.threshold(blur,70,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) - contours, hierarchy = cv2.findContours(thresh1,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) drawing = np.zeros(img.shape,np.uint8) - + +# Finds the object with the maximum area in the frame max_area=0 - for i in range(len(contours)): - cnt=contours[i] - area = cv2.contourArea(cnt) - if(area>max_area): - max_area=area - ci=i + cnt=contours[i] + area = cv2.contourArea(cnt) + if(area>max_area): + max_area=area + ci=i cnt=contours[ci] + +# Calculates Convex hull hull = cv2.convexHull(cnt) + +# Calculates the center of the object using moments moments = cv2.moments(cnt) + if moments['m00']!=0: - cx = int(moments['m10']/moments['m00']) # cx = M10/M00 - cy = int(moments['m01']/moments['m00']) # cy = M01/M00 + cx = int(moments['m10']/moments['m00']) # cx = M10/M00 + cy = int(moments['m01']/moments['m00']) # cy = M01/M00 centr=(cx,cy) cv2.circle(img,centr,5,[0,0,255],2) cv2.drawContours(drawing,[cnt],0,(0,255,0),2) cv2.drawContours(drawing,[hull],0,(0,0,255),2) - + +# Finds Approximate contours and convex hull. +# This time convex hull returns indices of cnt rather than explicit co-ordinates. cnt = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True) hull = cv2.convexHull(cnt,returnPoints = False) - if(1): - defects = cv2.convexityDefects(cnt,hull) - mind=0 - maxd=0 - for i in range(defects.shape[0]): - s,e,f,d = defects[i,0] - start = tuple(cnt[s][0]) - end = tuple(cnt[e][0]) - far = tuple(cnt[f][0]) - dist = cv2.pointPolygonTest(cnt,centr,True) - cv2.line(img,start,end,[0,255,0],2) - - cv2.circle(img,far,5,[0,0,255],-1) - print(i) - i=0 +# Finds convexity defects in the current frame +# cv2.pointPolygonTest determines whether the convexity defect is inside or outside the object + try: + defects = cv2.convexityDefects(cnt,hull) + font = cv2.FONT_HERSHEY_SIMPLEX + + for i in range(defects.shape[0]): + s,e,f,d = defects[i,0] + start = tuple(cnt[s][0]) + end = tuple(cnt[e][0]) + far = tuple(cnt[f][0]) + dist = cv2.pointPolygonTest(cnt,centr,True) + cv2.line(img,start,end,[0,255,0],2) + cv2.circle(drawing,far,5,[0,0,255],-1) + +# Prints the command in the upper-left pane of the frame + + if (defects.shape[0]==4): + # Do Something + elif(defects.shape[0]==3): + # Do Something + elif(defects.shape[0]==2): + # Do Something + except AttributeError: + pass + + cv2.imshow('output',drawing) cv2.imshow('input',img) k = cv2.waitKey(10) + if k == 27: break From 414df976de99e2a294650dfafe7904c337afc834 Mon Sep 17 00:00:00 2001 From: Jenil Shah Date: Thu, 20 Feb 2014 11:34:56 +0530 Subject: [PATCH 2/2] Adding exception and minor documentation --- software/firmware/cam.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/software/firmware/cam.py b/software/firmware/cam.py index 88d2462..ec7d4a9 100644 --- a/software/firmware/cam.py +++ b/software/firmware/cam.py @@ -47,8 +47,7 @@ # cv2.pointPolygonTest determines whether the convexity defect is inside or outside the object try: defects = cv2.convexityDefects(cnt,hull) - font = cv2.FONT_HERSHEY_SIMPLEX - + for i in range(defects.shape[0]): s,e,f,d = defects[i,0] start = tuple(cnt[s][0])