PARP Research Group | Universidad de Murcia |
Image and video input/outputThe QVision provides functionality to perform the following image an video input/output tasks:
Reading and writing images from and to files.The first application example seen in section Hello world includes the header file qvip.h. This file contains much of the image input/output functionality provided by the QVision. For example, it contains the headers for the functions writeQVImageToFile and readQVImageFromFile, which respectively write and read the content of an image file from and to a QVImage object. Reading from YUV4MPEG video files.Functions readYUV4MPEG2Header and readYUV4MPEG2Frame can be used to retrieve the contents of the video file. An example code usage follows: #include <iostream> // Headers for the QFile and the YUV4MPEG functions. #include <QFile> #include <qvip.h> int main() { QFile videoFile("test.yuv"); int cols, rows, fps; // First the video file must be opened for reading: if (not videoFile.open(QIODevice::ReadOnly)) std::err << "Can't open file 'test.yuv'." << std::endl; // If the video file was correctly opened, we can use function 'readYUV4MPEG2Header' to read the video file header: else if (!readYUV4MPEG2Header(videoFile, cols, rows, fps)) std::err << "File 'test.yuv' doesn't seem to contain a valid YUV4MPEG2 header." << std::endl; // If that is successful, we can use the function 'readYUV4MPEG2Frame' to read image frames to 'QVImage' objects: else { // Each image will contain a different channel for the input images, read in YUV format. QVImage<uChar, 1> imageY(cols, rows, cols), imageU(cols/2, rows/2, cols/2), imageV(cols/2, rows/2, cols/2); while (!videoFile.atEnd() && readYUV4MPEG2Frame(videoFile, imageY, imageU, imageV)) { // Perform some image processing with the 'imageY', 'imageU' and 'imageV' objects, // which contain the image frame readed from the input file, in YUV 4:2:2 format. } // When finished, close the video file. videoFile.close(); } return 0; } Writing to YUV4MPEG video files.This can be done using the functions writeYUV4MPEG2Header, writeYUV4MPEG2Frame and writeYUV4MPEG2Frame. The following code illustrates their usage. First the video file must be created and initialized with a header: #include <QFile> #include <qvip.h> [...] QFile videoFile("test.yuv"); videoFile.open(QIODevice::WriteOnly|QIODevice::Truncate); writeYUV4MPEG2Header(videoFile, cols, rows, 24); [...] Then, each image frame must be written to the video file, until a given stop condition is met: while(newImages) { // Read a new image in RGB format QVImage<uChar,3> rgbImage = getSomeRGBImage(); // Write the RGB image to the file writeYUV4MPEG2Frame(videoFile, rgbImage); [...] } Alternatively, an overloaded version of the writeYUV4MPEG2Frame function can be used, to store an image in YUV format: while(newImages) { // Read a new YUV image in 4:2:0 format QVImage<uChar,1> yImage = getSomeYUVImageYChannel(); QVImage<uChar,1> UImage = getSomeYUVImageUChannel(); QVImage<uChar,1> VImage = getSomeYUVImageVChannel(); // Write the YUV image to the file writeYUV4MPEG2Frame(videoFile, yImage, uImage, vImage); [...] } When finished storing frames in the video file, it must of course be closed: videoFile.close(); Reading video with the QVMPlayer applicationThe QVMPlayerReader class contains functionality to open and read frames from a wide set of video sources:
This class uses the widely known MPlayer as a back-end application. Each QVMPlayerReader object launches an MPlayer application, that opens the requested video source, and reads frames from it. The QVMPlayerReader communicates with the MPlayer process using Linux named pipes, in a completely user-transparent way, obtaining the image frames and converting them into QVImage objects. The following code shows an usage example of this class. It opens a .avi video file, named penguin.avi, and reads frames from it until the end of the video file is reached. #include <QVApplication> #include <QVMPlayerReader> int main(int argc, char *argv[]) { QVApplication app(argc,argv); QVMPlayerReader videoReader; if (!videoReader.open("penguin.avi", QVMPlayerReader::NoLoop)) { std::cout << "Error opening video source." << std::endl; exit(0); } std::cout << "Video source successfully opened." << std::endl; QVImage<uChar, 3> image; while(videoReader.grab(image)) std::cout << "Frame grabbed ok. Image size:\t" << image.getCols() << "x" << image.getRows() << std::endl; videoReader.close(); std::cout << "Video reading finished." << std::endl; } IMPORTANT: Please note that, in order to use a QVMPlayerReader object, a QVApplication object (which is just an specialization of the well-known QT QApplication, an thus inherits from it) must have been previously created. This is necessary because it internally uses QTimer, QProcess and QThread objects, which use themshelves the infrastructure created by the (singleton) QApplication object. The first parameter of the open method in the videoReader object specifies the video source. It is an URL format string which identifies different video sources: files, webcams, digital cameras, etc... For more info about how to specify a video source (digital camera, remote file, etc...) see section QVMPlayerURLFormat. |