![]() |
University of Murcia ![]() |
Image and video input/outputThe QVision provides functionality to perform the following image an video input/output tasks:
The detailed documentation of these functions can be found in the group Video and image input/output group. Reading and writing images from and to files.The first application example seen in section Hello world includes the header file qvio.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. First the video file must be opened for reading:
// Headers for the QFile and the YUV4MPEG functions. #include <QFile> #include <qvio.h> [...] // Code for video file reading. QFile videoFile("test.mpeg"); int cols, rows, fps; if (!videoFile.open(QIODevice::ReadOnly)) std::err << "Can't open file 'test.mpeg'." << 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.mpeg' doesn't seem to contain a valid YUV4MPEG2 header." << std::endl; If that is successful, using the function readYUV4MPEG2Frame, we can start reading image frames to QVImage objects:
else // Proceed to frame reading { 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)) show(imageY, imageU, imageV); // Use image objects 'imageY', 'imageU', 'imageV' } Writting 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 initializated with a header:
#include <QFile> [...] QFile videoFile("test.mpeg"); videoFile.open(QIODevice::WriteOnly|QIODevice::Truncate); writeYUV4MPEG2Header(videoFile, cols, rows, 24); [...] Then, each image frame must be written to the video file, until a stop condition:
while(newImages) { // Read a new image in RGB format QVImage<uChar,3> rgbImage = getNewRGBImage(); // 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 = getNewYUVImageYChannel(); QVImage<uChar,1> UImage = getNewYUVImageUChannel(); QVImage<uChar,1> VImage = getNewYUVImageVChannel(); // Write the YUV image to the file writeYUV4MPEG2Frame(videoFile, yImage, uImage, vImage); [...] } when finished storing frames in the video file, it can be closed:
videoFile.close(); Reading from general video sourcesThe QVMPlayerReader class contains functionallity to open and read frames from a wide set of video sources:
To do so, it 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 through a series of Linux named pipes, to get the video properties and image frames and use them in the QVision application. The following code shows an usage example for the class. It is an application that opens an avi video file, named penguin.avi, and reads frames from it until the end of the video file is reached.
#include <QVMPlayerReader> int main(int argc, char *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; } The firts string parameter of the QVMPlayerReader::open() method specifies the video source, using a special url format. For more info about how to specify a different video source (digital camera, remote file, etc...) see section Video source identifier URL formats. The first string parameter value can be changed to the name of a video file or video source, other than the file penguin.avi. For more info about the format of the |