Convert Camera Video to Black and White in Python OpenCV

In this tutorial, we will use an example to show you how to convert camera video to black and white. You can implement it step by step.

Best Practice to Convert Camera Video to Black and White in Python OpenCV

1.Import library

import cv2

2.Open camera video

capture = cv2.VideoCapture(0)

3.Grab frames and convert to Black and White

while (True):
    (ret, frame) = capture.read()
    grayFrame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    (thresh, blackAndWhiteFrame) = cv2.threshold(grayFrame, 127, 255, cv2.THRESH_BINARY)

    cv2.imshow('video bw', blackAndWhiteFrame)
    cv2.imshow('video original', frame)

    if cv2.waitKey(1) == 27:
        break

In this code, we will use capture.read() to grab video frame, then we will use cv2.cvtColor() to convert it to gray. Finally, we will display it.

4.Release camera

capture.release()
cv2.destroyAllWindows()

Convert Camera Video to Black and White in Python OpenCV