2016-08-15 53 views
1

我期望做的是讓我可以將四個視頻輸入,RGB,B,G,R通道堆疊在四幀輸入中。這裏是我的代碼我得到的錯誤「所有的輸入數組必須具有相同的維數,我想知道是否有辦法解決這個問題?如果你插入灰色的地方,你可以看到整體結果I比RGB幀我想對方應該是在灰框是RGB通道和單通道一起堆棧cv2框架

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

回答

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

如果你把我上面的代碼並添加灰色= CV2後更換灰色RGB。 cvtColor(frame,cv2.COLOR_BGR2GRAY)你可以看到我喜歡的結果,如果你在相機前面拿着一個紅色的物體,你可以看到紅色的通道突出顯示紅色,其他顏色等等。只是而不是第一個灰色框架,我希望它是常規的彩色框架。 –

+0

@MichaelHenderson不知道我是否理解正確,這意味着我的解決方案不是你在編輯之前想要實現的嗎? – BPL

+0

@MichaelHenderson我已經添加了一個新的解決方案,請讓我知道如果這就是你要找的,否則請解釋一點。 – BPL