Last active
December 12, 2015 08:18
-
-
Save kjunichi/4742531 to your computer and use it in GitHub Desktop.
This file contains 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 | |
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