I wanted to stack vertically 5 videos with cv2.VideoWriter
, however something is off with the dimensions, as in the attached image. Each data is of size (400, 100) for (x, y).
frame from the wrongly stacked video
Here is the code:
# Set up video writer
fps = 30
size = (400, 100*5) # Dimensions for lbs2d
video2 = cv2.VideoWriter('flow_data_video_new.mp4', cv2.VideoWriter_fourcc(*'mp4v'), fps, size)
# Load flow data
flow_gt = load_data('flow_gt.pkl')
flow_pred_1 = load_data('flow_pred_1.pkl')
flow_diff_1 = load_data('flow_diff_1.pkl')
flow_pred_2 = load_data('flow_pred_2.pkl')
flow_diff_2 = load_data('flow_diff_2.pkl')
# just to make it faster
flow_gt = flow_gt[:10]
flow_pred_1 = flow_pred_1[:10]
flow_diff_1 = flow_diff_1[:10]
flow_pred_2 = flow_pred_2[:10]
flow_diff_2 = flow_diff_2[:10]
# Determine the length of the shortest data array
min_length = min(len(flow_gt), len(flow_pred_1), len(flow_diff_1), len(flow_pred_2), len(flow_diff_2))
# Flow Data Visualization
quiver_steps = 6
max_flow_gt = np.max([np.max(np.sqrt(u**2 + v**2)) for field in [flow_gt, flow_pred_1, flow_diff_1, flow_pred_2, flow_diff_2] for u, v in zip(field[:, 0, ...], field[:, 1, ...])])
# Create video frames
for i in range(min_length):
# Create a blank image for the video frame
frame = np.zeros((500, 400, 3), dtype=np.uint8) # 400x500 for 5 stacked images
# List of flow fields to process
fields = [flow_gt, flow_pred_1, flow_diff_1, flow_pred_2, flow_diff_2]
for j, field in enumerate(fields):
u = field[i, 0, ...]
v = field[i, 1, ...]
norm = np.sqrt(u ** 2 + v ** 2)
img = np.zeros(u.shape)
# Create the quiver plot and save it as an image
fig, ax = plt.subplots(figsize=(5, 1), dpi=100)
pyimof.display.quiver(u, v, c=norm, bg=img, ax=ax, cmap='turbo', clim=(0, max_flow_gt), scale=None, bg_cmap='gray', step=quiver_steps)
ax.axis('off')
plt.subplots_adjust(left=0, right=1, top=1, bottom=0)
# Convert the Matplotlib figure to an image
fig.canvas.draw()
image = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
image = image.reshape(500, 400, 3)
# Insert the image into the frame
frame[j*100:(j+1)*100, :, :] = cv2.resize(image, (400, 100))
plt.close(fig)
print("x")
# Write the frame to the video
video2.write(cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
video2.release()
I tried to debug it but something is still off, any help will be appreciated!
1