堆栈cv2 RGB通道和单通道的帧一起

时间:2016-08-15 18:01:47

标签: python opencv numpy

我要做的是使它能够将4个视频输入组合在一起,RGB,B,G,R通道在四帧输入中。这是我的代码我得到错误"所有输入数组必须具有相同数量的维度。我想知道是否有办法解决这个问题?如果您在RGB中插入GRAY,您可以看到我想要的整体结果,而RGB帧应该是GRAY帧所在的位置。

import numpy as np
import cv2

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

while(True):
    ret, frame = cap.read()
    # Resizing down the image to fit in the screen.
    b,g,r = cv2.split(frame)
    RGB = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
    GRAY = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # creating another frame.
    channels = cv2.split(frame)
    frame_merge = cv2.merge(channels)

    # horizintally concatenating the two frames.
    final_frame = cv2.hconcat((frame, frame_merge))
    final_frame2 = cv2.hconcat((frame, frame_merge))
    final = cv2.vconcat((final_frame, final_frame2))

    frame1 = np.hstack((RGB,b))
    frame2 = np.hstack((g,r))
    final = np.vstack((frame1,frame2))
    cv2.imshow('frame', final)

    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break

cap.release()
cv2.destroyAllWindows()

1 个答案:

答案 0 :(得分:0)

这是我的解决方案:

import numpy as np
import cv2

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

red = np.zeros(frame.shape, 'uint8')
green = np.zeros(frame.shape, 'uint8')
blue = np.zeros(frame.shape, 'uint8')

while(True):
    ret, frame = cap.read()
    b, g, r = cv2.split(frame)

    red[..., 0], red[..., 1], red[..., 2] = r, r, r
    green[..., 0], green[..., 1], green[..., 2] = g, g, g
    blue[..., 0], blue[..., 1], blue[..., 2] = b, b, b

    final = cv2.vconcat((
        cv2.hconcat((frame, blue)),
        cv2.hconcat((green, red))
    ))

    cv2.imshow('frame', final)

    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break

cap.release()
cv2.destroyAllWindows()