2011-02-22 21:43:26 +01:00
Reading and Writing Images and Video
====================================
.. highlight :: cpp
.. index :: imdecode
2011-03-08 23:22:24 +01:00
.. _imdecode:
2011-02-28 22:26:43 +01:00
imdecode
2011-02-22 21:43:26 +01:00
------------
2011-02-28 22:26:43 +01:00
.. c:function :: Mat imdecode( const Mat\& buf, int flags )
2011-02-22 21:43:26 +01:00
Reads an image from a buffer in memory.
2011-05-02 21:34:16 +02:00
:param buf: Input array of vector of bytes.
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
:param flags: The same flags as in :ref:`imread` .
2011-03-03 08:29:55 +01:00
2011-05-02 21:34:16 +02:00
The function reads an image from the specified buffer in memory.
If the buffer is too short or contains invalid data, the empty matrix is returned.
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
See
2011-05-02 21:34:16 +02:00
:ref: `imread` for the list of supported formats and flags description.
2011-02-22 21:43:26 +01:00
.. index :: imencode
2011-03-08 23:22:24 +01:00
.. _imencode:
2011-02-28 22:26:43 +01:00
imencode
2011-02-22 21:43:26 +01:00
------------
2011-02-28 22:26:43 +01:00
.. c:function :: bool imencode( const string\& ext, const Mat\& img, vector<uchar>\& buf, const vector<int>\& params=vector<int>())
2011-02-22 21:43:26 +01:00
Encode an image into a memory buffer.
2011-05-02 21:34:16 +02:00
:param ext: File extension that defines the output format.
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
:param img: Image to be written.
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
:param buf: Output buffer resized to fit the compressed image.
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
:param params: Format-specific parameters. See :ref:`imwrite` .
2011-03-03 08:29:55 +01:00
2011-05-02 21:34:16 +02:00
The function compresses the image and stores it in the memory buffer that is resized to fit the result.
2011-02-26 12:05:10 +01:00
See
2011-05-02 21:34:16 +02:00
:ref: `imwrite` for the list of supported formats and flags description.
2011-02-22 21:43:26 +01:00
.. index :: imread
2011-03-08 23:22:24 +01:00
.. _imread:
2011-02-28 22:26:43 +01:00
imread
2011-02-22 21:43:26 +01:00
----------
2011-02-28 22:26:43 +01:00
.. c:function :: Mat imread( const string\& filename, int flags=1 )
2011-02-22 21:43:26 +01:00
Loads an image from a file.
2011-02-26 12:05:10 +01:00
:param filename: Name of file to be loaded.
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
:param flags: Flags specifying the color type of a loaded image:
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* **>0** a 3-channel color image
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* **=0** a grayscale image
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* **<0** The image is loaded as is. Note that in the current implementation the alpha channel, if any, is stripped from the output image. For example, a 4-channel RGBA image is loaded as RGB if :math: `flags\ge0` .
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
The function `` imread `` loads an image from the specified file and returns it. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format), the function returns an empty matrix ( `` Mat::data==NULL `` ). Currently, the following file formats are supported:
2011-02-22 21:43:26 +01:00
2011-03-08 23:22:24 +01:00
* Windows bitmaps - `` *.bmp, *.dib `` (always supported)
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* JPEG files - `` *.jpeg, *.jpg, *.jpe `` (see the *Notes* section)
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* JPEG 2000 files - `` *.jp2 `` (see the *Notes* section)
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* Portable Network Graphics - `` *.png `` (see the *Notes* section)
2011-02-22 21:43:26 +01:00
2011-03-08 23:22:24 +01:00
* Portable image format - `` *.pbm, *.pgm, *.ppm `` (always supported)
2011-02-22 21:43:26 +01:00
2011-03-08 23:22:24 +01:00
* Sun rasters - `` *.sr, *.ras `` (always supported)
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* TIFF files - `` *.tiff, *.tif `` (see the *Notes* section)
2011-02-26 12:05:10 +01:00
2011-05-02 21:34:16 +02:00
**Notes** :
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* The function determines the type of an image by the content, not by the file extension.
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* On Microsoft Windows* OS and MacOSX* , the codecs shipped with an OpenCV image (libjpeg, libpng, libtiff, and libjasper) are used by default. So, OpenCV can always read JPEGs, PNGs, and TIFFs. On MacOSX, there is also an option to use native MacOSX image readers. But beware that currently these native image loaders give images with different pixel values because of the color management embedded into MacOSX.
* On Linux*, BSD flavors and other Unix-like open-source operating systems, OpenCV looks for codecs supplied with an OS image. Install the relevant packages (do not forget the development files, for example, "libjpeg-dev", in Debian* and Ubuntu*) to get the codec support or turn on the `` OPENCV_BUILD_3RDPARTY_LIBS `` flag in CMake.
2011-02-22 21:43:26 +01:00
.. index :: imwrite
2011-03-08 23:22:24 +01:00
.. _imwrite:
2011-02-28 22:26:43 +01:00
imwrite
2011-02-22 21:43:26 +01:00
-----------
2011-02-28 22:26:43 +01:00
.. c:function :: bool imwrite( const string\& filename, const Mat\& img, const vector<int>\& params=vector<int>())
2011-02-22 21:43:26 +01:00
Saves an image to a specified file.
2011-02-26 12:05:10 +01:00
:param filename: Name of the file.
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
:param img: Image to be saved.
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
:param params: Format-specific save parameters encoded as pairs ``paramId_1, paramValue_1, paramId_2, paramValue_2, ...`` . The following parameters are currently supported:
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* For JPEG, it can be a quality ( `` CV_IMWRITE_JPEG_QUALITY `` ) from 0 to 100 (the higher is the better). Default value is 95.
2011-02-26 12:05:10 +01:00
2011-05-02 21:34:16 +02:00
* For PNG, it can be the compression level ( `` CV_IMWRITE_PNG_COMPRESSION `` ) from 0 to 9. A higher value means a smaller size and longer compression time. Default value is 3.
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* For PPM, PGM, or PBM, it can be a binary format flag ( `` CV_IMWRITE_PXM_BINARY `` ), 0 or 1. Default value is 1.
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
The function `` imwrite `` saves the image to the specified file. The image format is chosen based on the `` filename `` extension (see
:ref: `imread` for the list of extensions). Only 8-bit (or 16-bit in case of PNG, JPEG 2000, and TIFF) single-channel or 3-channel (with 'BGR' channel order) images can be saved using this function. If the format, depth or channel order is different, use
2011-02-26 12:05:10 +01:00
:ref: `Mat::convertTo` , and
2011-05-02 21:34:16 +02:00
:ref: `cvtColor` to convert it before saving. Or, use the universal XML I/O functions to save the image to XML or YAML format.
2011-02-22 21:43:26 +01:00
.. index :: VideoCapture
.. _VideoCapture:
VideoCapture
------------
2011-02-28 22:26:43 +01:00
.. c:type :: VideoCapture
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
Class for video capturing from video files or cameras ::
2011-02-22 21:43:26 +01:00
class VideoCapture
{
public:
// the default constructor
VideoCapture();
// the constructor that opens video file
VideoCapture(const string& filename);
// the constructor that starts streaming from the camera
VideoCapture(int device);
2011-02-26 12:05:10 +01:00
2011-02-22 21:43:26 +01:00
// the destructor
virtual ~VideoCapture();
2011-02-26 12:05:10 +01:00
2011-02-22 21:43:26 +01:00
// opens the specified video file
virtual bool open(const string& filename);
2011-02-26 12:05:10 +01:00
2011-02-22 21:43:26 +01:00
// starts streaming from the specified camera by its id
virtual bool open(int device);
2011-02-26 12:05:10 +01:00
2011-02-22 21:43:26 +01:00
// returns true if the file was open successfully or if the camera
// has been initialized succesfully
virtual bool isOpened() const;
2011-02-26 12:05:10 +01:00
2011-02-22 21:43:26 +01:00
// closes the camera stream or the video file
// (automatically called by the destructor)
virtual void release();
2011-02-26 12:05:10 +01:00
2011-02-22 21:43:26 +01:00
// grab the next frame or a set of frames from a multi-head camera;
// returns false if there are no more frames
virtual bool grab();
// reads the frame from the specified video stream
// (non-zero channel is only valid for multi-head camera live streams)
virtual bool retrieve(Mat& image, int channel=0);
// equivalent to grab() + retrieve(image, 0);
virtual VideoCapture& operator >> (Mat& image);
2011-02-26 12:05:10 +01:00
// sets the specified property propId to the specified value
2011-02-22 21:43:26 +01:00
virtual bool set(int propId, double value);
// retrieves value of the specified property
virtual double get(int propId);
2011-02-26 12:05:10 +01:00
2011-02-22 21:43:26 +01:00
protected:
...
};
2011-03-03 08:29:55 +01:00
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
The class provides C++ video capturing API. Here is how the class can be used: ::
2011-02-22 21:43:26 +01:00
#include "cv.h"
#include "highgui.h"
2011-02-26 12:05:10 +01:00
2011-02-22 21:43:26 +01:00
using namespace cv;
2011-02-26 12:05:10 +01:00
2011-02-22 21:43:26 +01:00
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
2011-02-26 12:05:10 +01:00
2011-02-22 21:43:26 +01:00
Mat edges;
namedWindow("edges",1);
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
cvtColor(frame, edges, CV_BGR2GRAY);
GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
Canny(edges, edges, 0, 30, 3);
imshow("edges", edges);
if(waitKey(30) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
2011-03-03 08:29:55 +01:00
2011-02-22 21:43:26 +01:00
.. index :: VideoCapture::VideoCapture
2011-03-08 23:22:24 +01:00
.. _VideoCapture :: VideoCapture:
2011-02-28 22:26:43 +01:00
VideoCapture::VideoCapture
2011-02-22 21:43:26 +01:00
------------------------------
2011-02-28 22:26:43 +01:00
.. c:function :: VideoCapture::VideoCapture()
2011-02-22 21:43:26 +01:00
2011-02-28 22:26:43 +01:00
.. c:function :: VideoCapture::VideoCapture(const string\& filename)
2011-02-22 21:43:26 +01:00
2011-02-28 22:26:43 +01:00
.. c:function :: VideoCapture::VideoCapture(int device)
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
VideoCapture constructors.?? No desc here and further??
2011-02-26 12:05:10 +01:00
:param filename: TOWRITE
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
:param device: TOWRITE
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
.. index :: VideoCapture::get
2011-02-22 21:43:26 +01:00
2011-03-08 23:22:24 +01:00
.. _VideoCapture :: get:
2011-02-28 22:26:43 +01:00
VideoCapture::get
2011-02-26 12:05:10 +01:00
---------------------
2011-02-28 22:26:43 +01:00
.. c:function :: double VideoCapture::get(int property_id)
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
:param property_id: Property identifier. It can be one of the following:
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* **CV_CAP_PROP_POS_MSEC** Film current position in milliseconds or video capture timestamp.
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* **CV_CAP_PROP_POS_FRAMES** 0-based index of the frame to be decoded/captured next.
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* **CV_CAP_PROP_POS_AVI_RATIO** Relative position of the video file: 0 - start of the film, 1 - end of the film.
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* **CV_CAP_PROP_FRAME_WIDTH** Width of the frames in the video stream.
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* **CV_CAP_PROP_FRAME_HEIGHT** Height of the frames in the video stream.
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* **CV_CAP_PROP_FPS** Frame rate.
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* **CV_CAP_PROP_FOURCC** 4-character code of codec.
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* **CV_CAP_PROP_FRAME_COUNT** Number of frames in the video file.
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* **CV_CAP_PROP_FORMAT** Format of the Mat objects returned by `` retrieve() `` .
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* **CV_CAP_PROP_MODE** Backend-specific value indicating the current capture mode.
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* **CV_CAP_PROP_BRIGHTNESS** Brightness of the image (only for cameras).
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* **CV_CAP_PROP_CONTRAST** Contrast of the image (only for cameras).
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* **CV_CAP_PROP_SATURATION** Saturation of the image (only for cameras).
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* **CV_CAP_PROP_HUE** Hue of the image (only for cameras).
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* **CV_CAP_PROP_GAIN** Gain of the image (only for cameras).
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* **CV_CAP_PROP_EXPOSURE** Exposure (only for cameras).
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* **CV_CAP_PROP_CONVERT_RGB** Boolean flags indicating whether images should be converted to RGB.
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
* **CV_CAP_PROP_WHITE_BALANCE** Currently unsupported
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
* **CV_CAP_PROP_RECTIFICATION** TOWRITE (note: only supported by DC1394 v 2.x backend currently)
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
**Note** : When querying a property that is not supported by the backend used by the `` VideoCapture `` class, value 0 is returned.
2011-02-22 21:43:26 +01:00
.. index :: VideoCapture::set
2011-03-08 23:22:24 +01:00
.. _VideoCapture :: set:
2011-02-28 22:26:43 +01:00
VideoCapture::set
2011-02-22 21:43:26 +01:00
---------------------
2011-02-28 22:26:43 +01:00
.. c:function :: bool VideoCapture::set(int property_id, double value)
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
Sets a property in the VideoCapture backend.
:param property_id: Property identifier. It can be one of the following:
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* **CV_CAP_PROP_POS_MSEC** Film current position in milliseconds or video capture timestamp.
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* **CV_CAP_PROP_POS_FRAMES** 0-based index of the frame to be decoded/captured next.
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* **CV_CAP_PROP_POS_AVI_RATIO** Relative position of the video file: 0 - start of the film, 1 - end of the film.
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* **CV_CAP_PROP_FRAME_WIDTH** Width of the frames in the video stream.
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* **CV_CAP_PROP_FRAME_HEIGHT** Height of the frames in the video stream.
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* **CV_CAP_PROP_FPS** Frame rate.
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* **CV_CAP_PROP_FOURCC** 4-character code of codec.
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* **CV_CAP_PROP_FRAME_COUNT** Number of frames in the video file.
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* **CV_CAP_PROP_FORMAT** Format of the Mat objects returned by `` retrieve() `` .
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* **CV_CAP_PROP_MODE** Backend-specific value indicating the current capture mode.
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* **CV_CAP_PROP_BRIGHTNESS** Brightness of the image (only for cameras).
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* **CV_CAP_PROP_CONTRAST** Contrast of the image (only for cameras).
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* **CV_CAP_PROP_SATURATION** Saturation of the image (only for cameras).
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* **CV_CAP_PROP_HUE** Hue of the image (only for cameras).
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* **CV_CAP_PROP_GAIN** Gain of the image (only for cameras).
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
* **CV_CAP_PROP_EXPOSURE** Exposure (only for cameras).
2011-02-26 12:05:10 +01:00
2011-05-02 21:34:16 +02:00
* **CV_CAP_PROP_CONVERT_RGB** Boolean flags indicating whether images should be converted to RGB.
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
* **CV_CAP_PROP_WHITE_BALANCE** Currently unsupported
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
* **CV_CAP_PROP_RECTIFICATION** TOWRITE (note: only supported by DC1394 v 2.x backend currently)
2011-02-22 21:43:26 +01:00
2011-05-02 21:34:16 +02:00
:param value: Value of the property.
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
.. index :: VideoWriter
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
.. _VideoWriter:
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
VideoWriter
-----------
2011-02-28 22:26:43 +01:00
.. c:type :: VideoWriter
2011-02-22 21:43:26 +01:00
2011-02-26 12:05:10 +01:00
Video writer class ::
2011-02-22 21:43:26 +01:00
class VideoWriter
{
2011-02-26 12:05:10 +01:00
public:
2011-02-22 21:43:26 +01:00
// default constructor
VideoWriter();
// constructor that calls open
VideoWriter(const string& filename, int fourcc,
double fps, Size frameSize, bool isColor=true);
2011-02-26 12:05:10 +01:00
2011-02-22 21:43:26 +01:00
// the destructor
virtual ~VideoWriter();
2011-02-26 12:05:10 +01:00
2011-02-22 21:43:26 +01:00
// opens the file and initializes the video writer.
2011-02-26 12:05:10 +01:00
// filename - the output file name.
2011-02-22 21:43:26 +01:00
// fourcc - the codec
// fps - the number of frames per second
// frameSize - the video frame size
// isColor - specifies whether the video stream is color or grayscale
virtual bool open(const string& filename, int fourcc,
double fps, Size frameSize, bool isColor=true);
2011-02-26 12:05:10 +01:00
2011-02-22 21:43:26 +01:00
// returns true if the writer has been initialized successfully
virtual bool isOpened() const;
2011-02-26 12:05:10 +01:00
2011-02-22 21:43:26 +01:00
// writes the next video frame to the stream
virtual VideoWriter& operator << (const Mat& image);
2011-02-26 12:05:10 +01:00
2011-02-22 21:43:26 +01:00
protected:
...
};
2011-03-03 08:29:55 +01:00
2011-02-22 21:43:26 +01:00
..