且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

OpenCV(Python)视频子图

更新时间:2023-11-21 23:07:16

您可以简单地使用cv2.hconcat()方法水平合并2张图像,然后使用imshow进行显示,但是请记住,这些图像必须相同 size type 以便在其上应用hconcat.

You may simply use the cv2.hconcat() method to horizontally join 2 images and then display using imshow, But keep in mind that the images must be of same size and type for applying hconcat on them.

您也可以使用vconcat垂直合并图像.

You may also use vconcat to join the images vertically.

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