import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while(1):
# get a frame
ret, frame = cap.read()
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (21, 21), 0)
edge = cv2.Canny(gray.copy(), 1000, 2000, apertureSize=5)
_,contours, hierarchy = cv2.findContours(edge.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for i in contours:
x, y, w, h = cv2.boundingRect(i)
print x,y
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
opencv包
import cv2
摄像头对象,0是第一个摄像头
cap = cv2.VideoCapture(0)
读取
ret, frame = cap.read()