Skip to content

Instantly share code, notes, and snippets.

@kjunichi
Last active December 12, 2015 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjunichi/4742531 to your computer and use it in GitHub Desktop.
Save kjunichi/4742531 to your computer and use it in GitHub Desktop.
import cv2
import numpy as np
# create video capture
cap = cv2.VideoCapture(0)
imwidth=cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)
imheight=cap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)
print 'img width %d height %d ' % (imwidth,imheight)
while(1):
# read the frames
_,frame = cap.read()
# smooth it
frame = cv2.blur(frame,(30,3))
# convert to hsv and find range of colors
hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
thresh = cv2.inRange(hsv,np.array((0, 80, 80)), np.array((20, 255, 255)))
thresh2 = thresh.copy()
# find contours in the threshold image
contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
# finding contour with maximum area and store it as best_cnt
max_area = 0
for cnt in contours:
area = cv2.contourArea(cnt)
if area > max_area:
max_area = area
best_cnt = cnt
# finding centroids of best_cnt and draw a circle there
# //M = cv2.moments(best_cnt)
# //cx,cy = int(M['m10']/M['m00']), int(M['m01']/M['m00'])
# //cv2.circle(frame,(cx,cy),5,255,-1)
# Show it, if key pressed is 'Esc', exit the loop
imgsize = frame.shape
simg = cv2.resize(frame,(imgsize[1]/2,imgsize[0]/2))
cv2.imshow('frame',simg)
#cv2.imshow('thresh',thresh2)
if cv2.waitKey(33)== 27:
break
# Clean up everything before leaving
cv2.destroyAllWindows()
cap.release()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment