OpenCV (Python) video subplots

RafazZ

I am trying to show two OpenCV video feeds in the same figure as subplots, but couldn't find how to do it. When I try using plt.imshow(...), plt.show(), the window won't even appear. When I try using cv2.imshow(...), it shows two independent figures. What I actually want are subplots :(. Any help?

Here is the code that I have so far:

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: Ideally the output should look something like that:

Subplots for OpenCV videos

ZdaR

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.

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

openCV video saving in python

From Dev

No video output OpenCV Python

From Dev

Read a video in opencv (python)

From Dev

Python opencv video writer

From Dev

Saving a video capture in python with openCV : empty video

From Dev

Saving a Video in OpenCV (Linux, Python)

From Dev

Cannot open video in OpenCV (Python)

From Dev

How to record video using OpenCV and Python?

From Dev

Specify Compression Quality in Python for OpenCV Video Object

From Dev

How to Capture Video Stream with OpenCV (Python)

From Dev

Limiting video capture frame rate on python and opencv

From Dev

Not able to play video in opencv (Python 2.7)

From Dev

Python OpenCV video format play in browser

From Dev

Python creating video from images using opencv

From Dev

How to generate a video with python, matplotlib and opencv?

From Dev

video not refreshing when using OpenCV in python

From Dev

Changing fontsize in python subplots

From Java

Python xticks in subplots

From Dev

Python subplots fixed spacing

From Dev

Python subplots not working properly

From Dev

Common label on python subplots

From Dev

Python subplots not working properly

From Dev

Python Plot two subplots

From Dev

OpenCV subplots images with titles and space around borders

From Dev

OpenCV-Python : Issue with getting frame rate of video

From Dev

Auto-capture an image from a video in OpenCV using python

From Dev

Sending live video frame over network in python opencv

From Dev

How to mask a video frame using contours with opencv in python

From Dev

Python and OpenCV - getting the duration time of a video at certain points