I can't draw the contour approximation, I use a green square to take the video but no contour on it.
Code:
import cv2
import time
import numpy as np
import picamera.array import PiRGBArray
from picamera import 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)
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()
thresh is not color image it's a grayscale image : (0,255,0) in cv2.drawContours(thresh, [c], -1, (0,255,0), 3) is not possible. Try thresh =cv2.cvtColor(thresh,cv2.COLOR_GRAY2BGR) before for c in aprox:
or, draw into the
dst
image instead:I am using an example, that draws the contour in a grayscale image, that is why I’m asking but thanks it works...
When using 64-bit, you don't need this
& 0xFF
@SebastianPanozzo. I attempted this, but it is not working under OpenCV4.0.1. Too many errors.