Tuesday, April 26, 2016

多個追蹤

import cv2
import numpy as np
#create a VideoCapture object
cap = cv2.VideoCapture(0)

while(1):

 # Capture Video from Camera
 _, frame = cap.read()

 # Convert BGR to HSV
 hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

 # define range of blue color in HSV
 lower = np.array([0,0,0])
 upper = np.array([255,70,70])

 # Threshold the HSV image to get only blue colors
 mask_black = cv2.inRange(hsv, lower, upper)
 _, contours, _ = cv2.findContours(mask_black, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
 for cnt in contours: 
   x,y,w,h = cv2.boundingRect(cnt)
   cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)
   # cv2.rectangle(frame,(x,y),((x+w),(y-h)),(255,0,0),2)
   # cv2.rectangle(frame,(x+(w/2),y+(h/2)-10),(x+(w/2),y+(h/2)+10),(255,0,0),2)
 # contours, hierarchy = cv2.findContours(Anti_White,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
 cv2.imshow('frame',frame)
 cv2.imshow('mask_black)',mask_black)
 k = cv2.waitKey(5) & 0xFF
 if k == 27:
  break

cv2.destroyAllWindows()

參考資料:
http://stackoverflow.com/questions/25504964/opencv-python-valueerror-too-many-values-to-unpack