Ask Your Question
0

Real-time webcam stream very slow with pixel manipulation

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

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 = cap.read()
    height, width, channels = frame.shape

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

    cv2.imshow('frame', frame)

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

cap.release()
cv2.destroyAllWindows()
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

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

berak gravatar image

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

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

Stats

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

Seen: 164 times

Last updated: Dec 03 '20