简体   繁体   English

从网络摄像头流openCV隔离并显示红色通道

[英]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. 我看到的是均匀的鲜红色流,但是frame [:,:,2]为我提供了正确隔离的通道,但是是灰度的。

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. 当您执行red = frame[:, :, 2] ,这将提取红色通道,它只是一个二维数组,其值的范围为0到255。如果打印形状,您将看到它只有一个尺寸。 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM