Ask Your Question

Revision history [back]

Real-time webcam stream very slow with pixel manipulation

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