2016-06-07 101 views
3

我想顯示兩個OpenCV視頻源與子圖相同的圖中,但無法找到如何去做。當我嘗試使用plt.imshow(...), plt.show()時,窗口甚至不會出現。當我嘗試使用cv2.imshow(...)時,它顯示兩個獨立的數字。我真正想要的是次要情節:(任何幫助OpenCV(Python)視頻子圖

下面是代碼,我到目前爲止有:?

import numpy as np 
import cv2 
import matplotlib.pyplot as plt 

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

while(True): 
    ret, frame = cap.read() 
    channels = cv2.split(frame) 
    frame_merge = cv2.merge(channels) 

    #~ subplot(211), plt.imshow(frame) 
    #~ subplot(212), plt.imshow(frame_merged) 
    cv2.imshow('frame',frame) 
    cv2.imshow('frame merged', frame_merge) 
    k = cv2.waitKey(30) & 0xff 
    if k == 27: 
     break 

cap.release() 
cv2.destroyAllWindows() 

UPDATE:理想情況下,輸出應該是這個樣子的是:

Subplots for OpenCV videos

回答

4

您可以簡單地使用cv2.hconcat()方法水平連接2個圖像,然後使用imshow顯示,但請記住,圖像必須相同尺寸類型對其應用hconcat

您也可以使用vconcat垂直連接圖像。

import numpy as np 
import cv2 
import matplotlib.pyplot as plt 

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

bg = [[[0] * len(frame[0]) for _ in xrange(len(frame))] for _ in xrange(3)] 

while(True): 
    ret, frame = cap.read() 
    # Resizing down the image to fit in the screen. 
    frame = cv2.resize(frame, None, fx = 0.5, fy = 0.5, interpolation = cv2.INTER_CUBIC) 

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

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

    # Show the concatenated frame using imshow. 
    cv2.imshow('frame',final_frame) 

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

cap.release() 
cv2.destroyAllWindows() 
+0

我剛剛嘗試過它,它運行得非常漂亮。問題是我不能放置'matplotlib'風格的標題:(任何建議在前面? – RafazZ

+0

如果你能顯示預期的輸出,那麼我可以幫你 – ZdaR

+0

我已經更新了包含它的答案 - 謝謝! – RafazZ