Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I've managed to write a code in Python that can extract the frames from the .xvi file (as Tetragramm suggested in C#). It's strange that the first frame seems to be discarded or there's something else in the first 640x480x2 bytes after the videofile header (which is 1024 bytes). It also seems that each frame has a 32 byte header.

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

#read the data from the file
with open('filename.xvi', 'rb') as infile:
    #choose the frame number to extract
    n=51
    infile.seek(1024+(32+640*480*2)*n)
    #read frame bytes into buffer
    buf = infile.read(640*480*2)

#use numpy to construct an array from the bytes
x = np.frombuffer(buf, dtype='uint16').reshape(480, 640)
#visualize extracted frame
plt.figimage(x, cmap='gray_r', vmin=np.min(x), vmax=np.max(x))
plt.show()

As you can see from the code I've visualized the frame (16bit depth array) with matplotlib. I cannot understand why when trying to visualize with opencv like in the code below it gives me an error (img_data = None). Maybe someone could help? Does opencv have a problem opening 16 bit greyscale images? If someone could help, here is a link to a small .xvi file with 81 frames (32 Mb) https://drive.google.com/file/d/0BzQd3MR0r7Y6RTFZci1RWG1sU00/view?usp=sharing

x = np.frombuffer(buf, dtype='uint16').reshape(480, 640)
img_data = cv2.imdecode(x,2)

Thank you for your help.