reading_and_writing_images_and_video.rst 22.3 KB
Newer Older
1 2 3 4 5
Reading and Writing Images and Video
====================================

.. highlight:: cpp

6
imdecode
7
--------
8 9
Reads an image from a buffer in memory.

10
.. ocv:function:: Mat imdecode( InputArray buf,  int flags )
11

12 13
.. ocv:function:: Mat imdecode( InputArray buf,  int flags, Mat* dst )

14 15 16 17
.. ocv:cfunction:: IplImage* cvDecodeImage( const CvMat* buf, int iscolor=CV_LOAD_IMAGE_COLOR)

.. ocv:cfunction:: CvMat* cvDecodeImageM( const CvMat* buf, int iscolor=CV_LOAD_IMAGE_COLOR)

18
.. ocv:pyfunction:: cv2.imdecode(buf, flags) -> retval
19

20
    :param buf: Input array or vector of bytes.
21

22 23 24
    :param flags: The same flags as in :ocv:func:`imread` .
    
    :param dst: The optional output placeholder for the decoded matrix. It can save the image reallocations when the function is called repeatedly for images of the same size.
25

26
The function reads an image from the specified buffer in the memory.
27
If the buffer is too short or contains invalid data, the empty matrix/image is returned.
28

29
See
30
:ocv:func:`imread` for the list of supported formats and flags description.
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
31

32 33
.. note:: In the case of color images, the decoded images will have the channels stored in ``B G R`` order.

34
imencode
35
--------
36 37
Encodes an image into a memory buffer.

38
.. ocv:function:: bool imencode( const string& ext, InputArray img, vector<uchar>& buf, const vector<int>& params=vector<int>())
39

40
.. ocv:cfunction:: CvMat* cvEncodeImage( const char* ext, const CvArr* image, const int* params=0 )
41

42
.. ocv:pyfunction:: cv2.imencode(ext, img[, params]) -> retval, buf
43

44
    :param ext: File extension that defines the output format.
45

46
    :param img: Image to be written.
47

48
    :param buf: Output buffer resized to fit the compressed image.
49

50
    :param params: Format-specific parameters. See  :ocv:func:`imwrite` .
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
51

52
The function compresses the image and stores it in the memory buffer that is resized to fit the result.
53
See
54
:ocv:func:`imwrite` for the list of supported formats and flags description.
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
55

56 57
.. note:: ``cvEncodeImage`` returns single-row matrix of type ``CV_8UC1`` that contains encoded image as array of bytes.

58
imread
59
------
60 61
Loads an image from a file.

62
.. ocv:function:: Mat imread( const string& filename, int flags=1 )
63

64 65
.. ocv:pyfunction:: cv2.imread(filename[, flags]) -> retval

66
.. ocv:cfunction:: IplImage* cvLoadImage( const char* filename, int iscolor=CV_LOAD_IMAGE_COLOR )
67

68
.. ocv:cfunction:: CvMat* cvLoadImageM( const char* filename, int iscolor=CV_LOAD_IMAGE_COLOR )
69

70
.. ocv:pyoldfunction:: cv.LoadImage(filename, iscolor=CV_LOAD_IMAGE_COLOR) -> None
71

72
.. ocv:pyoldfunction:: cv.LoadImageM(filename, iscolor=CV_LOAD_IMAGE_COLOR) -> None
73

74
    :param filename: Name of file to be loaded.
75

76
    :param flags: Flags specifying the color type of a loaded image:
77
    
78
        * CV_LOAD_IMAGE_ANYDEPTH - If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
79
        
80 81 82
        * CV_LOAD_IMAGE_COLOR - If set, always convert image to the color one

        * CV_LOAD_IMAGE_GRAYSCALE - If set, always convert image to the grayscale one
83

84 85
        * **>0**  Return a 3-channel color image.
            .. note:: In the current implementation the alpha channel, if any, is stripped from the output image. Use negative value if you need the alpha channel.
86

87
        * **=0**  Return a grayscale image.
88

89
        * **<0**  Return the loaded image as is (with alpha channel).
90

91
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:
92

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
93
 * Windows bitmaps - ``*.bmp, *.dib`` (always supported)
94

95
 * JPEG files - ``*.jpeg, *.jpg, *.jpe`` (see the *Notes* section)
96

97
 * JPEG 2000 files - ``*.jp2`` (see the *Notes* section)
98

99
 * Portable Network Graphics - ``*.png`` (see the *Notes* section)
100

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
101
 * Portable image format - ``*.pbm, *.pgm, *.ppm``     (always supported)
102

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
103
 * Sun rasters - ``*.sr, *.ras``     (always supported)
104

105
 * TIFF files - ``*.tiff, *.tif`` (see the *Notes* section)
106

107
.. note::
108

109
    * The function determines the type of an image by the content, not by the file extension.
110

111
    * 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.
112

113
    * 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.
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
114

115 116
.. note:: In the case of color images, the decoded images will have the channels stored in ``B G R`` order.

117
imwrite
118
-----------
119 120
Saves an image to a specified file.

121
.. ocv:function:: bool imwrite( const string& filename, InputArray img, const vector<int>& params=vector<int>() )
122

123
.. ocv:pyfunction:: cv2.imwrite(filename, img[, params]) -> retval
124

125
.. ocv:cfunction:: int cvSaveImage( const char* filename, const CvArr* image, const int* params=0 )
126 127

.. ocv:pyoldfunction:: cv.SaveImage(filename, image)-> None
128

129
    :param filename: Name of the file.
130

131
    :param image: Image to be saved.
132

133
    :param params: Format-specific save parameters encoded as pairs  ``paramId_1, paramValue_1, paramId_2, paramValue_2, ...`` . The following parameters are currently supported:
134

135
        *  For JPEG, it can be a quality ( ``CV_IMWRITE_JPEG_QUALITY`` ) from 0 to 100 (the higher is the better). Default value is 95.
136

137
        *  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.
138

139
        *  For PPM, PGM, or PBM, it can be a binary format flag ( ``CV_IMWRITE_PXM_BINARY`` ), 0 or 1. Default value is 1.
140

141
The function ``imwrite`` saves the image to the specified file. The image format is chosen based on the ``filename`` extension (see
142
:ocv:func:`imread` for the list of extensions). Only 8-bit (or 16-bit unsigned (``CV_16U``) 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
143 144
:ocv:func:`Mat::convertTo` , and
:ocv:func:`cvtColor` to convert it before saving. Or, use the universal XML I/O functions to save the image to XML or YAML format.
145

146
It is possible to store PNG images with an alpha channel using this function. To do this, create 8-bit (or 16-bit) 4-channel image BGRA, where the alpha channel goes last. Fully transparent pixels should have alpha set to 0, fully opaque pixels should have alpha set to 255/65535. The sample below shows how to create such a BGRA image and store to PNG file. It also demonstrates how to set custom compression parameters ::
147

148 149 150
    #include <vector>
    #include <stdio.h>
    #include <opencv2/opencv.hpp>
151

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
    using namespace cv;
    using namespace std;

    void createAlphaMat(Mat &mat)
    {
        for (int i = 0; i < mat.rows; ++i) {
            for (int j = 0; j < mat.cols; ++j) {
                Vec4b& rgba = mat.at<Vec4b>(i, j);
                rgba[0] = UCHAR_MAX;
                rgba[1] = saturate_cast<uchar>((float (mat.cols - j)) / ((float)mat.cols) * UCHAR_MAX);
                rgba[2] = saturate_cast<uchar>((float (mat.rows - i)) / ((float)mat.rows) * UCHAR_MAX);
                rgba[3] = saturate_cast<uchar>(0.5 * (rgba[1] + rgba[2]));
            }
        }
    }

    int main(int argv, char **argc)
    {
        // Create mat with alpha channel
        Mat mat(480, 640, CV_8UC4);
        createAlphaMat(mat);

        vector<int> compression_params;
        compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
        compression_params.push_back(9);

        try {
            imwrite("alpha.png", mat, compression_params);
        }
        catch (runtime_error& ex) {
            fprintf(stderr, "Exception converting image to PNG format: %s\n", ex.what());
            return 1;
        }

        fprintf(stdout, "Saved PNG file with alpha data.\n");
        return 0;
    }


191 192
VideoCapture
------------
193
.. ocv:class:: VideoCapture
194

195 196
Class for video capturing from video files or cameras.
The class provides C++ API for capturing video from cameras or for reading video files. Here is how the class can be used: ::
197

198
    #include "opencv2/opencv.hpp"
199

200
    using namespace cv;
201

202 203 204 205 206
    int main(int, char**)
    {
        VideoCapture cap(0); // open the default camera
        if(!cap.isOpened())  // check if we succeeded
            return -1;
207

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
        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;
    }
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
223

224

225
.. note:: In C API the black-box structure ``CvCapture`` is used instead of ``VideoCapture``.
226

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
227

228
VideoCapture::VideoCapture
229
------------------------------
230 231
VideoCapture constructors.

232
.. ocv:function:: VideoCapture::VideoCapture()
233

234
.. ocv:function:: VideoCapture::VideoCapture(const string& filename)
235

236
.. ocv:function:: VideoCapture::VideoCapture(int device)
237

238 239 240 241 242
.. ocv:pyfunction:: cv2.VideoCapture() -> <VideoCapture object>
.. ocv:pyfunction:: cv2.VideoCapture(filename) -> <VideoCapture object>
.. ocv:pyfunction:: cv2.VideoCapture(device) -> <VideoCapture object>

.. ocv:cfunction:: CvCapture* cvCaptureFromCAM( int device )
243
.. ocv:pyoldfunction:: cv.CaptureFromCAM(index) -> CvCapture
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
.. ocv:cfunction:: CvCapture* cvCaptureFromFile( const char* filename )
.. ocv:pyoldfunction:: cv.CaptureFromFile(filename) -> CvCapture

    :param filename: name of the opened video file

    :param device: id of the opened video capturing device (i.e. a camera index). If there is a single camera connected, just pass 0.

.. note:: In C API, when you finished working with video, release ``CvCapture`` structure with ``cvReleaseCapture()``, or use ``Ptr<CvCapture>`` that calls ``cvReleaseCapture()`` automatically in the destructor.


VideoCapture::open
---------------------
Open video file or a capturing device for video capturing

.. ocv:function:: bool VideoCapture::open(const string& filename)
.. ocv:function:: bool VideoCapture::open(int device)

261 262
.. ocv:pyfunction:: cv2.VideoCapture.open(filename) -> retval
.. ocv:pyfunction:: cv2.VideoCapture.open(device) -> retval
263

264
    :param filename: name of the opened video file
265

266
    :param device: id of the opened video capturing device (i.e. a camera index).
267

268
The methods first call :ocv:func:`VideoCapture::release` to close the already opened file or camera.
269 270 271 272 273 274 275 276


VideoCapture::isOpened
----------------------
Returns true if video capturing has been initialized already.

.. ocv:function:: bool VideoCapture::isOpened()

277
.. ocv:pyfunction:: cv2.VideoCapture.isOpened() -> retval
278 279 280 281 282 283 284 285 286

If the previous call to ``VideoCapture`` constructor or ``VideoCapture::open`` succeeded, the method returns true.

VideoCapture::release
---------------------
Closes video file or capturing device.

.. ocv:function:: void VideoCapture::release()

287
.. ocv:pyfunction:: cv2.VideoCapture.release() -> None
288

289
.. ocv:cfunction:: void cvReleaseCapture(CvCapture** capture)
290 291 292 293 294 295 296 297 298 299 300 301

The methods are automatically called by subsequent :ocv:func:`VideoCapture::open` and by ``VideoCapture`` destructor.

The C function also deallocates memory and clears ``*capture`` pointer.


VideoCapture::grab
---------------------
Grabs the next frame from video file or capturing device.

.. ocv:function:: bool VideoCapture::grab()

302
.. ocv:pyfunction:: cv2.VideoCapture.grab() -> retval
303

304
.. ocv:cfunction:: int cvGrabFrame(CvCapture* capture)
305 306 307 308 309 310 311

.. ocv:pyoldfunction:: cv.GrabFrame(capture) -> int

The methods/functions grab the next frame from video file or camera and return true (non-zero) in the case of success.

The primary use of the function is in multi-camera environments, especially when the cameras do not have hardware synchronization. That is, you call ``VideoCapture::grab()`` for each camera and after that call the slower method ``VideoCapture::retrieve()`` to decode and get frame from each camera. This way the overhead on demosaicing or motion jpeg decompression etc. is eliminated and the retrieved frames from different cameras will be closer in time.

312
Also, when a connected camera is multi-head (for example, a stereo camera or a Kinect device), the correct way of retrieving data from it is to call `VideoCapture::grab` first and then call :ocv:func:`VideoCapture::retrieve` one or more times with different values of the ``channel`` parameter. See http://code.opencv.org/projects/opencv/repository/revisions/master/entry/samples/cpp/kinect_maps.cpp
313 314 315 316 317 318 319 320


VideoCapture::retrieve
----------------------
Decodes and returns the grabbed video frame.

.. ocv:function:: bool VideoCapture::retrieve(Mat& image, int channel=0)

321
.. ocv:pyfunction:: cv2.VideoCapture.retrieve([image[, channel]]) -> retval, image
322

323
.. ocv:cfunction:: IplImage* cvRetrieveFrame( CvCapture* capture, int streamIdx=0 )
324

325
.. ocv:pyoldfunction:: cv.RetrieveFrame(capture) -> image
326

327
The methods/functions decode and return the just grabbed frame. If no frames has been grabbed (camera has been disconnected, or there are no more frames in video file), the methods return false and the functions return NULL pointer.
328 329 330 331 332 333 334 335 336

.. note:: OpenCV 1.x functions ``cvRetrieveFrame`` and ``cv.RetrieveFrame`` return image stored inside the video capturing structure. It is not allowed to modify or release the image! You can copy the frame using :ocv:cfunc:`cvCloneImage` and then do whatever you want with the copy.


VideoCapture::read
----------------------
Grabs, decodes and returns the next video frame.

.. ocv:function:: VideoCapture& VideoCapture::operator >> (Mat& image)
337

338 339
.. ocv:function:: bool VideoCapture::read(Mat& image)

340
.. ocv:pyfunction:: cv2.VideoCapture.read([image]) -> retval, image
341

342
.. ocv:cfunction:: IplImage* cvQueryFrame(CvCapture* capture)
343

344
.. ocv:pyoldfunction:: cv.QueryFrame(capture) -> image
345

346
The methods/functions combine :ocv:func:`VideoCapture::grab` and :ocv:func:`VideoCapture::retrieve` in one call. This is the most convenient method for reading video files or capturing data from decode and return the just grabbed frame. If no frames has been grabbed (camera has been disconnected, or there are no more frames in video file), the methods return false and the functions return NULL pointer.
347 348

.. note:: OpenCV 1.x functions ``cvRetrieveFrame`` and ``cv.RetrieveFrame`` return image stored inside the video capturing structure. It is not allowed to modify or release the image! You can copy the frame using :ocv:cfunc:`cvCloneImage` and then do whatever you want with the copy.
349

Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
350

351
VideoCapture::get
352
---------------------
353
Returns the specified ``VideoCapture`` property
354 355 356 357 358

.. ocv:function:: double VideoCapture::get(int propId)

.. ocv:pyfunction:: cv2.VideoCapture.get(propId) -> retval

359
.. ocv:cfunction:: double cvGetCaptureProperty( CvCapture* capture, int property_id )
360

361
.. ocv:pyoldfunction:: cv.GetCaptureProperty(capture, property_id) -> float
362 363


364 365 366
    :param propId: Property identifier. It can be one of the following:

        * **CV_CAP_PROP_POS_MSEC** Current position of the video file in milliseconds or video capture timestamp.
367

368
        * **CV_CAP_PROP_POS_FRAMES** 0-based index of the frame to be decoded/captured next.
369

370
        * **CV_CAP_PROP_POS_AVI_RATIO** Relative position of the video file: 0 - start of the film, 1 - end of the film.
371

372
        * **CV_CAP_PROP_FRAME_WIDTH** Width of the frames in the video stream.
373

374
        * **CV_CAP_PROP_FRAME_HEIGHT** Height of the frames in the video stream.
375

376
        * **CV_CAP_PROP_FPS** Frame rate.
377

378
        * **CV_CAP_PROP_FOURCC** 4-character code of codec.
379

380
        * **CV_CAP_PROP_FRAME_COUNT** Number of frames in the video file.
381

382
        * **CV_CAP_PROP_FORMAT** Format of the Mat objects returned by ``retrieve()`` .
383

384
        * **CV_CAP_PROP_MODE** Backend-specific value indicating the current capture mode.
385

386
        * **CV_CAP_PROP_BRIGHTNESS** Brightness of the image (only for cameras).
387

388
        * **CV_CAP_PROP_CONTRAST** Contrast of the image (only for cameras).
389

390
        * **CV_CAP_PROP_SATURATION** Saturation of the image (only for cameras).
391

392
        * **CV_CAP_PROP_HUE** Hue of the image (only for cameras).
393

394
        * **CV_CAP_PROP_GAIN** Gain of the image (only for cameras).
395

396
        * **CV_CAP_PROP_EXPOSURE** Exposure (only for cameras).
397

398
        * **CV_CAP_PROP_CONVERT_RGB** Boolean flags indicating whether images should be converted to RGB.
399

400
        * **CV_CAP_PROP_WHITE_BALANCE** Currently not supported
401

402
        * **CV_CAP_PROP_RECTIFICATION** Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently)
403

404 405

**Note**: When querying a property that is not supported by the backend used by the ``VideoCapture`` class, value 0 is returned.
406

407
VideoCapture::set
408
---------------------
409
Sets a property in the ``VideoCapture``.
410

411
.. ocv:function:: bool VideoCapture::set( int propId, double value )
412

413
.. ocv:pyfunction:: cv2.VideoCapture.set(propId, value) -> retval
414

415
.. ocv:cfunction:: int cvSetCaptureProperty( CvCapture* capture, int property_id, double value )
416

417
.. ocv:pyoldfunction:: cv.SetCaptureProperty(capture, property_id, value) -> retval
418 419 420 421

    :param propId: Property identifier. It can be one of the following:

        * **CV_CAP_PROP_POS_MSEC** Current position of the video file in milliseconds.
422

423
        * **CV_CAP_PROP_POS_FRAMES** 0-based index of the frame to be decoded/captured next.
424

425
        * **CV_CAP_PROP_POS_AVI_RATIO** Relative position of the video file: 0 - start of the film, 1 - end of the film.
426

427
        * **CV_CAP_PROP_FRAME_WIDTH** Width of the frames in the video stream.
428

429
        * **CV_CAP_PROP_FRAME_HEIGHT** Height of the frames in the video stream.
430

431
        * **CV_CAP_PROP_FPS** Frame rate.
432

433
        * **CV_CAP_PROP_FOURCC** 4-character code of codec.
434

435
        * **CV_CAP_PROP_FRAME_COUNT** Number of frames in the video file.
436

437
        * **CV_CAP_PROP_FORMAT** Format of the Mat objects returned by ``retrieve()`` .
438

439
        * **CV_CAP_PROP_MODE** Backend-specific value indicating the current capture mode.
440

441
        * **CV_CAP_PROP_BRIGHTNESS** Brightness of the image (only for cameras).
442

443
        * **CV_CAP_PROP_CONTRAST** Contrast of the image (only for cameras).
444

445
        * **CV_CAP_PROP_SATURATION** Saturation of the image (only for cameras).
446

447
        * **CV_CAP_PROP_HUE** Hue of the image (only for cameras).
448

449
        * **CV_CAP_PROP_GAIN** Gain of the image (only for cameras).
450

451
        * **CV_CAP_PROP_EXPOSURE** Exposure (only for cameras).
452

453
        * **CV_CAP_PROP_CONVERT_RGB** Boolean flags indicating whether images should be converted to RGB.
454

455
        * **CV_CAP_PROP_WHITE_BALANCE** Currently unsupported
456

457
        * **CV_CAP_PROP_RECTIFICATION** Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently)
458

459 460
    :param value: Value of the property.

461 462


463 464
VideoWriter
-----------
465
.. ocv:class:: VideoWriter
466

467
Video writer class.
468

469 470 471 472 473 474 475


VideoWriter::VideoWriter
------------------------
VideoWriter constructors

.. ocv:function:: VideoWriter::VideoWriter()
476

477 478 479 480
.. ocv:function:: VideoWriter::VideoWriter(const string& filename, int fourcc, double fps, Size frameSize, bool isColor=true)

.. ocv:pyfunction:: cv2.VideoWriter([filename, fourcc, fps, frameSize[, isColor]]) -> <VideoWriter object>

481 482
.. ocv:cfunction:: CvVideoWriter* cvCreateVideoWriter( const char* filename, int fourcc, double fps, CvSize frame_size, int is_color=1 )
.. ocv:pyoldfunction:: cv.CreateVideoWriter(filename, fourcc, fps, frame_size, is_color=true) -> CvVideoWriter
483 484 485 486 487

.. ocv:pyfunction:: cv2.VideoWriter.isOpened() -> retval
.. ocv:pyfunction:: cv2.VideoWriter.open(filename, fourcc, fps, frameSize[, isColor]) -> retval
.. ocv:pyfunction:: cv2.VideoWriter.write(image) -> None

488
    :param filename: Name of the output video file.
489

490
    :param fourcc: 4-character code of codec used to compress the frames. For example, ``CV_FOURCC('P','I','M,'1')``  is a MPEG-1 codec, ``CV_FOURCC('M','J','P','G')``  is a motion-jpeg codec etc. List of codes can be obtained at `Video Codecs by FOURCC <http://www.fourcc.org/codecs.php>`_ page.
491

492
    :param fps: Framerate of the created video stream.
493

494
    :param frameSize: Size of the  video frames.
495

496
    :param isColor: If it is not zero, the encoder will expect and encode color frames, otherwise it will work with grayscale frames (the flag is currently supported on Windows only).
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514

The constructors/functions initialize video writers. On Linux FFMPEG is used to write videos; on Windows FFMPEG or VFW is used; on MacOSX QTKit is used.



ReleaseVideoWriter
------------------
Releases the AVI writer.

.. ocv:cfunction:: void cvReleaseVideoWriter( CvVideoWriter** writer )

The function should be called after you finished using ``CvVideoWriter`` opened with :ocv:cfunc:`CreateVideoWriter`.


VideoWriter::open
-----------------
Initializes or reinitializes video writer.

515
.. ocv:function:: bool VideoWriter::open(const string& filename, int fourcc, double fps, Size frameSize, bool isColor=true)
516 517 518 519 520 521 522 523 524 525

.. ocv:pyfunction:: cv2.VideoWriter.open(filename, fourcc, fps, frameSize[, isColor]) -> retval

The method opens video writer. Parameters are the same as in the constructor :ocv:func:`VideoWriter::VideoWriter`.


VideoWriter::isOpened
---------------------
Returns true if video writer has been successfully initialized.

526
.. ocv:function:: bool VideoWriter::isOpened()
527 528 529 530 531 532 533 534 535

.. ocv:pyfunction:: cv2.VideoWriter.isOpened() -> retval


VideoWriter::write
------------------
Writes the next video frame

.. ocv:function:: VideoWriter& VideoWriter::operator << (const Mat& image)
536

537 538 539 540 541 542 543
.. ocv:function:: void VideoWriter::write(const Mat& image)

.. ocv:pyfunction:: cv2.VideoWriter.write(image) -> None

.. ocv:cfunction:: int cvWriteFrame( CvVideoWriter* writer, const IplImage* image )
.. ocv:pyoldfunction:: cv.WriteFrame(writer, image)->int

544 545 546 547
    :param writer: Video writer structure (OpenCV 1.x API)

    :param image: The written frame

548
The functions/methods write the specified image to video file. It must have the same size as has been specified when opening the video writer.
549