Awful artifacts when capturing from web cam

asked 2016-04-09 16:01:08 -0600

agoonforhire gravatar image

updated 2016-04-09 16:19:48 -0600

I am running OpenCV 3.1.0 on my Raspberry Pi 3 (with Raspbian Jessie). I'm using the Python 2.7 bindings.

As I request frames from a VideoCapture, occasionally (apparently at random) the frame will have an awful blocky artifact as seen below.

image description

This never occurs when I use the webcam on my PC, nor does it occur when I view the webcam with a program like Cheese on the Raspberry Pi, so I have to conclude that it's either an OpenCV problem, or I'm using openCV incorrectly.

Further, the bad frames happen MUCH more often when the Pi is doing other things as well (for example, if I open a web browser while it's capturing).

Below is a sample script where I encounter the problem.

import cv2
cap = cv2.VideoCapture(0)
while(True):
    res, im = cap.read()
    cv2.imshow("foo", im)
    cv2.waitKey(1)

My suspicion is that it has something to do with trying to retrieve the image buffer while the buffer is in the process of being updated, or something like that.

Am I doing something wrong here?

Again, I've only seen this issue occur with OpenCV on the Pi.

I've also tried it with OpenCV on my laptop and I don't see the artifacts. So, the problem might not be OpenCV specifically, but I'm hoping I'm not the only person here that's had this issue. (and again, I haven't seen it with any other image capture programs on the PI either... though they all run at much slower frame rates)

edit retag flag offensive close merge delete

Comments

Well, I'm a new account so I can't answer my own question yet, but I appear to have found a solution. After cv2.waitKey(1) I decided to see what would happen if I do a del(im). To my surprise, I don't see the problem anymore... I have no idea why that resolves it. Hopefully this will be of some use to someone else in the future.

agoonforhire gravatar imageagoonforhire ( 2016-04-09 16:34:56 -0600 )edit