Ask Your Question

Real-time webcam stream very slow with pixel manipulation

asked 2020-12-03 09:02:08 -0600

I am trying to write a script to manipulate video from a webcam. I am trying to do this through OpenCV with Python, but I am running into some issues.

If I run the video capture stream with no pixel manipulation applied, the stream works fine and has a smooth frame rate. However, I applied a threshold loop as a test, and my stream undergoes major lag and updates once every few seconds. Any ideas if it is possible to optimise this? Ideally, I am looking to get a 30 fps stream with the video manipulation applied. Here is the code:

import cv2
import numpy as np

cap = cv2.VideoCapture(0)
T = 100

while True:

    ret, frame =
    height, width, channels = frame.shape

    for x in range(width):
        for y in range(height):
            if frame[y,x,0] < T:

    cv2.imshow('frame', frame)

    if cv2.waitKey(1) == ord('q'):

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2020-12-03 09:33:01 -0600

berak gravatar image

updated 2020-12-03 09:34:06 -0600

NEVER do anything "per pixel", don't write loops like that !

please use the threshold() function instead, here's a tutorial

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2020-12-03 09:02:08 -0600

Seen: 1,105 times

Last updated: Dec 03 '20