Ask Your Question

Revision history [back]

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

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!

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

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

cap = cv2.VideoCapture(0)

while(True): ret, img = cap.read()

# Do image processing here, using img
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
contours,h = cv2.findContours(img,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, 0.1),-1)

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

When everything done, release the capture

cap.release() cv2.destroyAllWindows() ```

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

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

cv2 cap = cv2.VideoCapture(0)

cv2.VideoCapture(0) while(True): ret, img = cap.read()

cap.read()
# Do image processing here, using img
 img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 contours,h = cv2.findContours(img,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, 0.1),-1)
 # Display the resulting frame
 cv2.imshow('img',img)
 if cv2.waitKey(1) & 0xFF == ord('q'):
 break

# When everything done, release the capture

capture cap.release() cv2.destroyAllWindows() ```

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

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

cap = cv2.VideoCapture(0)

while(True):
    ret, img = cap.read()

    # Do image processing here, using img
    img ret = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    contours,h = cv2.findContours(img,1,2)
cv2.findContours(ret,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, 0.1),-1)
1),-1)

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

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

Edited code. Now I see the screen will with a red rectangle.

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

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()

    ret = cv2.cvtColor(img,     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(ret,1,2)
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()

Edited code. Now now I see the screen will with am seeing red flickering -- but when I hold up a red rectangle.solid colored object (a pink yoga block in front of a white mat) it does not show.