Code:
import cv2
import cv as cv
import numpy as np
import time
show = False
c = cv2.VideoCapture(0)
c.set(3, 320)
c.set(4, 240)
width,height = c.get(3),c.get(4)
print "width, height = ", width, ",", height
start_time = time.time()
i = 0
while(1):
_,f = c.read()
#f = cv2.flip(f,1)
#blur = cv2.medianBlur(f,5)
hsv = cv2.cvtColor(f,cv2.COLOR_BGR2HSV)
yellow = cv2.inRange(hsv,np.array((20,100,100)),np.array((30,255,255)))
erode = cv2.erode(yellow,None,iterations = 3)
dilate = cv2.dilate(erode,None,iterations = 10)
contours,hierarchy = cv2.findContours(dilate,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
x,y,w,h = cv2.boundingRect(cnt)
cx,cy = x+w/2, y+h/2
print "yellow :", x,y,w,h
if show:
cv2.rectangle(f,(x,y),(x+w,y+h),[0,255,255],2)
cv2.imshow('img',f)
if cv2.waitKey(25) == 27:
break
cv2.destroyAllWindows()
c.release()
Lesezeichen