Ask Your Question
-1

Can anyone know the code of python to put two frames in a single window output specifically to use it in opencv

asked 2018-11-03 09:22:01 -0600

Digbijay2 gravatar image

updated 2018-11-03 17:14:49 -0600

input_source = "sample.mp4" cap = cv2.VideoCapture(input_source) hasFrame, frame = cap.read()

vid_writer = cv2.VideoWriter('output.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 10, (frame.shape[1],frame.shape[0]))
vid_writer.write(frame)

Above is short code snippets to put one frame. Please tell me how to make it for 2 frames in a single window output.

edit retag flag offensive close merge delete

Comments

1

HAve a look at: multiple windows

supra56 gravatar imagesupra56 ( 2018-11-03 10:32:09 -0600 )edit
1

Or rather hconcat or vconcat

kbarni gravatar imagekbarni ( 2018-11-04 07:44:49 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
3

answered 2018-11-04 11:45:27 -0600

Images are numpy arrays. Therefore you can use numpy capabilities to create an image contaning two images. The images to stack/concatenate should have the same size.

# Import required packages:
import cv2
import numpy as np

# Load images
image_1 = cv2.imread('lenna.png')
image_2 = image_1.copy()

# Eliminate blue and read channels:
image_2[:, :, 0] = 0
image_2[:, :, 2] = 0

# Stack horizontally:
# Equivalent to np.concatenate(tup, axis=1)
# images_1_2_h = np.concatenate((image_1, image_2), axis=1)
images_1_2_h = np.hstack((image_1, image_2))

# Stack vertically:
# Equivalent to np.concatenate(tup, axis=0)
# images_1_2_v = np.concatenate((image_1, image_2), axis = 0)
images_1_2_v = np.vstack((image_1, image_2))

# Show images:
cv2.imshow("images_1_2_h", images_1_2_h)
cv2.imshow("images_1_2_v", images_1_2_v)

# Wait until a key is pressed:
cv2.waitKey(0)

# Destroy all created windows:
cv2.destroyAllWindows()

image description image description

edit flag offensive delete link more

Comments

2

hi @albertofernandez we missed your answers :)

sturkmen gravatar imagesturkmen ( 2018-11-04 12:07:33 -0600 )edit

Many thanks @sturkmen :). It's nice to ear things like this

albertofernandez gravatar imagealbertofernandez ( 2018-11-05 01:34:25 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-11-03 09:22:01 -0600

Seen: 4,680 times

Last updated: Nov 04 '18