.write() function, video problem


I am trying to write a video with the .write() function of cv2 and the frames of the video written don’t match with the frame I enter in the function.

It could be a problem of compression or bytes based.

I tried to apply the function np.uint8() on my matrix and change the format of the video but nothing works.

Thanks for your help.

Here’s my script :

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import math
import cv2
import ffmpeg
from PIL import Image

theta = math.radians(0)

size = 255

def rotation (x, y, theta): 
    x_rot = x*np.cos(theta) + y*np.sin(theta)
    y_rot = -x*np.sin(theta) + y*np.cos(theta)
    return(x_rot, y_rot)

def gabor(x, y, t, sf, sigma_x, sigma_y, w, phase) :
    carrier = np.cos(2*np.pi*sf*rotation(x, y, theta)[0] - w*t + phase)
    gaussian_window = np.exp(-0.5*((rotation(x, y, theta)[0]/sigma_x)**2 + (rotation(x, y, theta)[1]/sigma_y)**2 ))
    return carrier*gaussian_window

M = np.zeros((size, size))

def image (t) : 
    x=-(size/2) # initialization in x
    for i in range (size):
        y=-(size/2) # initialization in y
        x = x + 1
        for j in range (size):
            y = y + 1
            M[i,j] = gabor(x, y, t, 0.02, 30, 30, 2*np.pi, np.pi/1) 
    return M

N_frame = 20
N = size

fourcc = cv2.VideoWriter_fourcc('X','V','I','D')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (N,N), 0)

i = 0
for i in range (N_frame - 1) :
    t = i*0.1
    frame = image(t)

do you understand the meaning of “lossy compression”? XVID is a lossy codec.

please demonstrate what you consider “don’t match”