Aleph
Aleph

Reputation: 217

How to visualise overlay of quiver plot over video frames?

I have the following code segment where frames from a video are read sequentially. Next, the frames are cropped and intensity wise clipped from upper and lower side. The following segments demonstrate the relevant codes. However, I want to save and view these frames with an added quiver plot on top of the processed frame from the video. The quiver plot syntax is used as described [here][1].

import numpy as np
import cv2
import os
import matplotlib.pyplot as plt
import matplotlib.cm as cm

cap = cv2.VideoCapture('Video.mp4')

lower = np.array([22, 93, 0])
upper = np.array([45, 255, 255])  

while cap.isOpened():
    frame_id = cap.get(cv2.CAP_PROP_POS_FRAMES)
    (ret, frame) = cap.read()
    
    size_x_l=70
    size_x_u=350
    size_y_l=130
    size_y_u=430
    num_channel=3
    
    frame_cropped = frame[size_x_l:size_x_u, size_y_l:size_y_u]
    gray = cv2.cvtColor(frame_cropped, cv2.COLOR_BGR2GRAY)
    yellow = np.zeros_like(frame_cropped, np.uint8)

    for i in range(size_x_u-size_x_l):
        for j in range(size_y_u-size_y_l):
            flag=True
            for k in range(num_channel):
                if frame_cropped[i][j][k] < lower[k] or frame_cropped[i][j][k] > upper[k]:
                    flag = False
            if flag:
                yellow[i][j] = gray[i][j]

    frame_id = int(frame_id)
    f_name = str(frame_id) + '.jpg'
    f_name = os.path.join('QUIVER', f_name)
    fig, ax = plt.subplots()
    X = (70)
    Y = (70)
    U, V = (-5,5)
    plt.quiver(X, Y, U, V, color='r', width=.01, linewidth=1)
    RGB_yellow = cv2.cvtColor(yellow, cv2.COLOR_BGR2RGB)
    RGB_yellow = np.moveaxis(yellow, -1, 0)
    plt.plot(RGB_yellow)
    # plt.imsave(f_name, yellow_grayscale)

cap.release()

The image plotting tool is showing the following error.

~\anaconda3\lib\site-packages\matplotlib\axes\_base.py in
_plot_args(self, tup, kwargs, return_kwargs, ambiguous_fmt_datakey)
    505                              f"have shapes {x.shape} and {y.shape}")
    506         if x.ndim > 2 or y.ndim > 2:
--> 507             raise ValueError(f"x and y can be no greater than 2D, but have "
    508                              f"shapes {x.shape} and {y.shape}")
    509         if x.ndim == 1:

ValueError: x and y can be no greater than 2D, but have shapes (3,) and (3, 280, 300)

I want to see the output of the superimposing the quiver plot over the video frame. Any help in this regard will be highly effective. [1]: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.quiver.html

Upvotes: 0

Views: 89

Answers (0)

Related Questions