Ask Your Question
0

I can't draw the contour approximation, I use a green square to take the video but no contour on it.

asked 2019-01-30 21:32:48 -0600

updated 2020-04-09 14:27:52 -0600

supra56 gravatar image

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()
edit retag flag offensive close merge delete

Comments

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:

LBerger gravatar imageLBerger ( 2019-01-31 01:30:31 -0600 )edit
1

or, draw into the dst image instead:

dst = cv2.drawContours(dst, [c], -1, (0,255,0), 3)
berak gravatar imageberak ( 2019-01-31 05:43:23 -0600 )edit
1

I am using an example, that draws the contour in a grayscale image, that is why I’m asking but thanks it works...

SebastianPanozzo gravatar imageSebastianPanozzo ( 2019-01-31 05:56:34 -0600 )edit

When using 64-bit, you don't need this & 0xFF

supra56 gravatar imagesupra56 ( 2019-01-31 08:49:56 -0600 )edit

@SebastianPanozzo. I attempted this, but it is not working under OpenCV4.0.1. Too many errors.

supra56 gravatar imagesupra56 ( 2019-02-04 08:21:17 -0600 )edit

2 answers

Sort by » oldest newest most voted
-1

answered 2019-02-04 06:18:57 -0600

The answer is to transform from gray to bgr

edit flag offensive delete link more
0

answered 2020-04-09 14:29:17 -0600

supra56 gravatar image

Typo error aprox:

approx = cv2.approxPolyDP(cnt, epsilon, True)
     for c in aprox:

To:

approx = cv2.approxPolyDP(cnt, epsilon, True)
     for c in approx:
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-01-30 21:32:48 -0600

Seen: 790 times

Last updated: Apr 09 '20