Drawing contours detected in image quickly turns into an all-black screen [closed]

asked 2016-06-20 22:28:44 -0600

quantumpotato gravatar image

updated 2016-06-21 21:52:40 -0600

Hi, I've been reading tutorials and I'm trying to detect rectangles from my webcam using OpenCV.

When I run this code, I see white specs on parts of the screen for a few frames. These white areas flicker in a familiar pattern to what I've seen image recognition do. Within 2 seconds the screen is entirely black.

Why is this happening? I'm trying to detect rectangles on the screen. Thanks!

import numpy as np
import cv2
import copy

cap = cv2.VideoCapture(0)

while(True):
    ret, img = cap.read()
    clone = copy.copy(img)
    gray_image = cv2.cvtColor(clone, cv2.COLOR_BGR2GRAY)
    ret, thresh = cv2.threshold(gray_image,127,255,1)

    # contours,h = cv2.findContours(thresh,1,2)

    contours, h = cv2.findContours(thresh,1,2)

    for cnt in contours:
            approx = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True)
            print len(approx)
            if len(approx)==4:
                cv2.drawContours(img,[cnt],0,(0,0,255, 1),-1)

    # Display the resulting frame
    cv2.imshow('frame', img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

now I am seeing red flickering -- but when I hold up a solid colored object (a pink yoga block in front of a white mat) it does not show.

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sturkmen
close date 2020-10-08 02:50:25.778988

Comments

1

where is your code?

Balaji R gravatar imageBalaji R ( 2016-06-21 01:07:27 -0600 )edit

Just guessing: you're rendering the same Mat used for detecting contours. findContours()eats your input image, so if you want to use it later, make a copy (clone())

LorenaGdL gravatar imageLorenaGdL ( 2016-06-21 09:31:16 -0600 )edit

As I said (and docs say http://docs.opencv.org/master/d3/dc0/...) input image of findContours is modified by the function

LorenaGdL gravatar imageLorenaGdL ( 2016-06-21 09:56:09 -0600 )edit

@Lorena I am searching for the Python example of clone. You are saying the object returned from the .read is a Mat, is that the same as OutputArray ?

quantumpotato gravatar imagequantumpotato ( 2016-06-21 10:10:14 -0600 )edit

Sorry, comment made before pointing out you were using Python. Your code should work now, as you're finding contours of the grayscaled ret and showing original img. You'd better threshold before finding contours, as in examples. Is your problem solved?

LorenaGdL gravatar imageLorenaGdL ( 2016-06-21 10:24:50 -0600 )edit

Nope, I see "src is not a numerical tuple" when I run img = cap.read() clone = copy.copy(img) thresh = cv2.threshold(img,127,255,1)

quantumpotato gravatar imagequantumpotato ( 2016-06-21 21:34:29 -0600 )edit
  • os / opencv version ? how did you install cv2 ?
  • can you try to comment all contour finding/drawing code, and only show the img from your cam ?
  • i can't reproduce the problem on win10, neither with opencv2.4, nor with opencv3.1
  • findContours() "eating" the image is not a problem here, imho, as you're not trying to re-use your gray_ image. i'd rather guess, it's a problem with your setup (gui / webcam).
berak gravatar imageberak ( 2016-06-22 01:04:04 -0600 )edit

@berak he has updated the code quite a few times now, first version was re-using image

LorenaGdL gravatar imageLorenaGdL ( 2016-06-22 02:03:50 -0600 )edit

@LorenaGdL, ah, ok. explains a lot.

berak gravatar imageberak ( 2016-06-22 02:08:27 -0600 )edit