python - 将RGB channel 和单 channel 的cv2帧堆叠在一起

标签 python opencv numpy

我想要做的是让我可以将 4 个视频源堆叠在一起,RGB、B、G、R channel 在四帧源中。这是我的代码,我收到错误“所有输入数组必须具有相同的维数。我想知道是否有办法解决或解决这个问题?如果你在 RGB 所在的位置插入 GRAY,你可以看到整体结果我我想要的不是 RGB 帧,而是灰色帧所在的位置。

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()

最佳答案

这是我的解决方案:

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()

关于python - 将RGB channel 和单 channel 的cv2帧堆叠在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38960411/

相关文章:

python - 设置多索引系列的多个层

python - 索引以填充预分配的 pandas 数据框

c++ - 使用 createTrackbar 设置图像路径

python-3.x - 在 Matplotlib 中使用百分比累积计数增强对比度

python - Rec 的用户-项目矩阵中共同评分项目的矢量化计算。系统

python - Numpy.copy 无法按预期进行随机采样

python - 可以在 python 中使用类作为字典键吗?

python - 如何根据 QcomboBox 索引切换小部件的启用状态

opencv - 如何在 YUV420p 帧上绘制矩形

python - Matplotlib 给出错误