praneeth mendu
praneeth mendu

Reputation: 409

Isolate and display red channel from webcam stream openCV

Using below code to isolate the red channel and have it appear red in the stream displayed.

import numpy as np 
import cv2

cap = cv2.VideoCapture(0)   
while(True):
    ret, frame = cap.read()
    red = frame[:, :, 2]
    new = np.zeros(frame.shape)
    new[:, :, 2] = red
    #flip = cv2.flip(dummy, 1)
    cv2.imshow( 'frame', new )

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

cap.release() 
cv2.destroyAllWindows()

what i see is a uniform bright red stream but frame[:, :, 2] gives me the correctly isolated channel but in grayscale.

Upvotes: 2

Views: 1358

Answers (1)

nathancy
nathancy

Reputation: 46670

When you do red = frame[:, :, 2], this extracts the red channel and is just a 2D array with values ranging from 0 to 255. If you print the shape, you will see that it has only one dimension. If you display this image, the output looks like a grayscale image but these are actually red channel values. To visualize only the red channel, you need to set the blue and green channels to zero.

import numpy as np 
import cv2

cap = cv2.VideoCapture(0)   
while(cap.isOpened()):
    ret, frame = cap.read()

    # Set blue and green channels to 0
    frame[:, :, 0] = 0
    frame[:, :, 1] = 0
    cv2.imshow('frame', frame)

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

cap.release() 
cv2.destroyAllWindows()

Upvotes: 2

Related Questions