如何使用OpenCV Beta 3.0.0和Python 2.7.5缩小视频以适应窗口?

时间:2015-02-23 00:45:16

标签: python python-2.7 opencv webcam opencv3.0

我正在开展一个项目,我需要在一个屏幕上并排显示3个网络摄像头。我并排放置了源,但视频没有完全显示在窗口上。如何使视频自动适应窗口?谢谢!

以下是代码:

import cv2

window_x = 340
window_y = 340

capture1 = cv2.VideoCapture(0)
capture2 = cv2.VideoCapture(1)
capture3 = cv2.VideoCapture(3)

while True:
    cv2.namedWindow("frame1")
    cv2.namedWindow("frame2")
    cv2.namedWindow("frame3")

    cv2.moveWindow("frame1",0,0)
    cv2.moveWindow("frame2",window_x,0)
    cv2.moveWindow("frame3",window_x * 2,0)

    cv2.resizeWindow("frame1",window_x,window_y)
    cv2.resizeWindow("frame2",window_x,window_y)
    cv2.resizeWindow("frame3",window_x,window_y)

    ret, frame1 = capture1.read()
    ret, frame2 = capture2.read()
    ret, frame3 = capture3.read()

    cv2.imshow("frame1",frame1)
    cv2.imshow("frame2",frame2)
    cv2.imshow("frame3",frame3)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

capture1.release()
capture2.release()
capture3.release()

cv2.destroyAllWindows()

1 个答案:

答案 0 :(得分:0)

在Opencv中,将三个图像放入一个大窗口的方法是使用ROI将每个图像复制到大窗口中的专用位置。以下是一个使用示例:

import cv2
import numpy as np

window_x = 340
window_y = 340

cv2.namedWindow("Big picture")

capture1 = cv2.VideoCapture(0)
capture2 = cv2.VideoCapture(1)
capture3 = cv2.VideoCapture(3)

while True:

    ret, frame1 = capture1.read()
    ret, frame2 = capture2.read()
    ret, frame3 = capture3.read()

    #Create the big Mat to put all three frames into it
    big_frame = m = np.zeros((window_y, window_x*3, 3), dtype=np.uint8)

    # Resize the frames to fit in the big picture
    frame1 = cv2.resize(frame1, (window_y, window_x)) 
    frame2 = cv2.resize(frame2, (window_y, window_x))
    frame3 = cv2.resize(frame3, (window_y, window_x))
    rows,cols,channels = frame1.shape

    #Add the first frame to the big picture
    roi = big_frame[0:cols, 0:rows]
    dst = cv2.add(roi,frame1)
    big_frame[0:cols, 0:rows] = dst

    #Add second frame to the big picture
    roi = big_frame[0:cols, rows:rows*2]
    dst = cv2.add(roi,frame2)
    big_frame[0:cols, rows:rows*2] = dst

    #Add third frame to the big picture
    roi = big_frame[0:cols, rows*2:rows*3]
    dst = cv2.add(roi,frame3)
    big_frame[0:cols, rows*2:rows*3] = dst

    cv2.imshow("Big picture", big_frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

capture1.release()
capture2.release()
capture3.release()

cv2.destroyAllWindows()