I can't draw the contour approximation, I use a green square to take the video but no contour on it.
import cv2
import time
import numpy as np
import picamera.array import PiRGBArray
from picamera import
PiCamera
PiCamera
#Initialize PiCamera
camera = PiCamera()
camera.resolution = (352,288)
camera.framerate = 32
rawCapture = PiRGBArray(camera)
time.sleep(0.1)
#Take video
for frame in camera.capture_continuous(rawCapture, format=('bgr'), use_video_port = True):
frame = frame.array
rows, cols , _ = frame.shape
#Rotate the video
M = cv2.getRotationMatrix2D((cols/2,rows/2), 180, 1)
dst = cv2.warpAffine(frame, M, (cols,rows))
#Recognize the green color
hsv = cv2.cvtColor(dst, cv2.COLOR_BGR2HSV)
lower_green = np.array([50, 50, 50])
upper_green = np.array([70, 255, 255])
mask = cv2.inRange(hsv, lower_green, upper_green)
#Contour
ret, thresh = cv2.threshold(mask, 127, 255, 0)
thr = cv2.cvtColor(thresh, cv2.COLOR_GRAY2BGR)
contours, hierarchy = cv2.findContours(thresh, 1, 2)
cnt = contours[0]
epsilon = 0.1*cv2.arcLength(cnt, True)
approx = cv2.approxPolyDP(cnt, epsilon, True)
for c in aprox:
thresh = cv2.drawContours(thresh, [c], -1, (0,255,0), 3)
#Show images
cv2.imshow('Tracking', thresh)
cv2.imshow('Video', dst)
rawCapture.truncate(0)
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()