使用下面的代码隔离红色 channel 并使其在显示的流中显示为红色。
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()
我看到的是均匀的亮红色流,但帧[:, :, 2] 为我提供了正确隔离的 channel ,但为灰度。
最佳答案
当您执行 red = frame[:, :, 2]
时,这会提取红色 channel ,并且只是一个值范围从 0 到 255 的 2D 数组。如果您打印形状,则将会看到它只有一维。如果显示此图像,输出看起来像灰度图像,但这些实际上是红色 channel 值。要仅显示红色 channel ,您需要将蓝色和绿色 channel 设置为零。
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()
关于python - 从网络摄像头流 openCV 中隔离并显示红色 channel ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59080248/