reading_and_writing_images_and_video.rst 21.9 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
    :param flags: The same flags as in :ocv:func:`imread` .
23

24
    :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

V
Vadim Pisarevsky 已提交
29
See
30
:ocv:func:`imread` for the list of supported formats and flags description.
V
Vadim Pisarevsky 已提交
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.

A
Andrey Kamaev 已提交
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` .
V
Vadim Pisarevsky 已提交
51

52
The function compresses the image and stores it in the memory buffer that is resized to fit the result.
V
Vadim Pisarevsky 已提交
53
See
54
:ocv:func:`imwrite` for the list of supported formats and flags description.
V
Vadim Pisarevsky 已提交
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.

A
Andrey Kamaev 已提交
62
.. ocv:function:: Mat imread( const String& filename, int flags=IMREAD_COLOR )
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

V
Vadim Pisarevsky 已提交
70
    :param filename: Name of file to be loaded.
71

72
    :param flags: Flags specifying the color type of a loaded image:
73

74
        * 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.
75

76 77 78
        * 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
79

80 81
        * **>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.
82

83
        * **=0**  Return a grayscale image.
84

85
        * **<0**  Return the loaded image as is (with alpha channel).
86

87
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:
88

V
Vadim Pisarevsky 已提交
89
 * Windows bitmaps - ``*.bmp, *.dib`` (always supported)
90

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

93
 * JPEG 2000 files - ``*.jp2`` (see the *Notes* section)
94

95
 * Portable Network Graphics - ``*.png`` (see the *Notes* section)
96

A
AoD314 已提交
97 98
 * WebP - ``*.webp`` (see the *Notes* section)

V
Vadim Pisarevsky 已提交
99
 * Portable image format - ``*.pbm, *.pgm, *.ppm``     (always supported)
100

V
Vadim Pisarevsky 已提交
101
 * Sun rasters - ``*.sr, *.ras``     (always supported)
102

103
 * TIFF files - ``*.tiff, *.tif`` (see the *Notes* section)
V
Vadim Pisarevsky 已提交
104

105
.. note::
106

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

109
    * 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.
110

111
    * 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.
V
Vadim Pisarevsky 已提交
112

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

115
imwrite
116
-----------
117 118
Saves an image to a specified file.

A
Andrey Kamaev 已提交
119
.. ocv:function:: bool imwrite( const String& filename, InputArray img, const vector<int>& params=vector<int>() )
120

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

123
.. ocv:cfunction:: int cvSaveImage( const char* filename, const CvArr* image, const int* params=0 )
124

V
Vadim Pisarevsky 已提交
125
    :param filename: Name of the file.
126

127
    :param image: Image to be saved.
128

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

131
        *  For JPEG, it can be a quality ( ``CV_IMWRITE_JPEG_QUALITY`` ) from 0 to 100 (the higher is the better). Default value is 95.
V
Vadim Pisarevsky 已提交
132

A
AoD314 已提交
133 134 135
        *  For WEBP, it can be a quality ( CV_IMWRITE_WEBP_QUALITY ) from 1 to 100 (the higher is the better).
           By default (without any parameter) and for quality above 100 the lossless compression is used.

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

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

140
The function ``imwrite`` saves the image to the specified file. The image format is chosen based on the ``filename`` extension (see
141
: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
142 143
: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.
144

145
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 ::
146

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

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
    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;
    }


190 191
VideoCapture
------------
192
.. ocv:class:: VideoCapture
V
Vadim Pisarevsky 已提交
193

194 195
Class for video capturing from video files, image sequences or cameras.
The class provides C++ API for capturing video from cameras or for reading video files and image sequences. Here is how the class can be used: ::
V
Vadim Pisarevsky 已提交
196

197
    #include "opencv2/opencv.hpp"
V
Vadim Pisarevsky 已提交
198

199
    using namespace cv;
V
Vadim Pisarevsky 已提交
200

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

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
        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;
    }
V
Vadim Pisarevsky 已提交
222

223

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

V
Vadim Pisarevsky 已提交
226

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

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

A
Andrey Kamaev 已提交
233
.. ocv:function:: VideoCapture::VideoCapture(const String& filename)
234

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

237 238 239 240 241 242 243
.. 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 )
.. ocv:cfunction:: CvCapture* cvCaptureFromFile( const char* filename )

244
    :param filename: name of the opened video file (eg. video.avi) or image sequence (eg. img%02d.jpg)
245 246 247 248 249 250 251 252 253 254

    :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

A
Andrey Kamaev 已提交
255
.. ocv:function:: bool VideoCapture::open(const String& filename)
256 257
.. ocv:function:: bool VideoCapture::open(int device)

258 259
.. ocv:pyfunction:: cv2.VideoCapture.open(filename) -> retval
.. ocv:pyfunction:: cv2.VideoCapture.open(device) -> retval
260

261
    :param filename: name of the opened video file (eg. video.avi) or image sequence (eg. img%02d.jpg)
262

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

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


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

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

274
.. ocv:pyfunction:: cv2.VideoCapture.isOpened() -> retval
275 276 277 278 279 280 281 282 283

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()

284
.. ocv:pyfunction:: cv2.VideoCapture.release() -> None
285

286
.. ocv:cfunction:: void cvReleaseCapture(CvCapture** capture)
287 288 289 290 291 292 293 294 295 296 297 298

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()

299
.. ocv:pyfunction:: cv2.VideoCapture.grab() -> retval
300

301
.. ocv:cfunction:: int cvGrabFrame(CvCapture* capture)
302 303 304 305 306

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.

307
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
308 309 310 311 312 313


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

A
Andrey Kamaev 已提交
314
.. ocv:function:: bool VideoCapture::retrieve( Mat& image, int flag=0 )
315

A
Andrey Kamaev 已提交
316
.. ocv:pyfunction:: cv2.VideoCapture.retrieve([image[, flag]]) -> retval, image
317

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

320
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.
321 322 323 324 325 326 327 328 329

.. 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)
330

331 332
.. ocv:function:: bool VideoCapture::read(Mat& image)

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

335
.. ocv:cfunction:: IplImage* cvQueryFrame(CvCapture* capture)
336

337
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.
338 339

.. 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.
340

V
Vadim Pisarevsky 已提交
341

342
VideoCapture::get
V
Vadim Pisarevsky 已提交
343
---------------------
344
Returns the specified ``VideoCapture`` property
345 346 347 348 349

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

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

350
.. ocv:cfunction:: double cvGetCaptureProperty( CvCapture* capture, int property_id )
351

352 353 354
    :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.
355

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

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

360
        * **CV_CAP_PROP_FRAME_WIDTH** Width of the frames in the video stream.
361

362
        * **CV_CAP_PROP_FRAME_HEIGHT** Height of the frames in the video stream.
363

364
        * **CV_CAP_PROP_FPS** Frame rate.
365

366
        * **CV_CAP_PROP_FOURCC** 4-character code of codec.
367

368
        * **CV_CAP_PROP_FRAME_COUNT** Number of frames in the video file.
369

370
        * **CV_CAP_PROP_FORMAT** Format of the Mat objects returned by ``retrieve()`` .
371

372
        * **CV_CAP_PROP_MODE** Backend-specific value indicating the current capture mode.
373

374
        * **CV_CAP_PROP_BRIGHTNESS** Brightness of the image (only for cameras).
375

376
        * **CV_CAP_PROP_CONTRAST** Contrast of the image (only for cameras).
377

378
        * **CV_CAP_PROP_SATURATION** Saturation of the image (only for cameras).
379

380
        * **CV_CAP_PROP_HUE** Hue of the image (only for cameras).
381

382
        * **CV_CAP_PROP_GAIN** Gain of the image (only for cameras).
383

384
        * **CV_CAP_PROP_EXPOSURE** Exposure (only for cameras).
385

386
        * **CV_CAP_PROP_CONVERT_RGB** Boolean flags indicating whether images should be converted to RGB.
387

388
        * **CV_CAP_PROP_WHITE_BALANCE** Currently not supported
389

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

392 393

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

395
VideoCapture::set
396
---------------------
397
Sets a property in the ``VideoCapture``.
398

399
.. ocv:function:: bool VideoCapture::set( int propId, double value )
400

401
.. ocv:pyfunction:: cv2.VideoCapture.set(propId, value) -> retval
402

403
.. ocv:cfunction:: int cvSetCaptureProperty( CvCapture* capture, int property_id, double value )
404

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

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

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

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

413
        * **CV_CAP_PROP_FRAME_WIDTH** Width of the frames in the video stream.
414

415
        * **CV_CAP_PROP_FRAME_HEIGHT** Height of the frames in the video stream.
416

417
        * **CV_CAP_PROP_FPS** Frame rate.
418

419
        * **CV_CAP_PROP_FOURCC** 4-character code of codec.
420

421
        * **CV_CAP_PROP_FRAME_COUNT** Number of frames in the video file.
422

423
        * **CV_CAP_PROP_FORMAT** Format of the Mat objects returned by ``retrieve()`` .
424

425
        * **CV_CAP_PROP_MODE** Backend-specific value indicating the current capture mode.
426

427
        * **CV_CAP_PROP_BRIGHTNESS** Brightness of the image (only for cameras).
428

429
        * **CV_CAP_PROP_CONTRAST** Contrast of the image (only for cameras).
430

431
        * **CV_CAP_PROP_SATURATION** Saturation of the image (only for cameras).
432

433
        * **CV_CAP_PROP_HUE** Hue of the image (only for cameras).
434

435
        * **CV_CAP_PROP_GAIN** Gain of the image (only for cameras).
436

437
        * **CV_CAP_PROP_EXPOSURE** Exposure (only for cameras).
V
Vadim Pisarevsky 已提交
438

439
        * **CV_CAP_PROP_CONVERT_RGB** Boolean flags indicating whether images should be converted to RGB.
440

V
Vadim Pisarevsky 已提交
441
        * **CV_CAP_PROP_WHITE_BALANCE** Currently unsupported
442

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

445 446
    :param value: Value of the property.

447 448


V
Vadim Pisarevsky 已提交
449 450
VideoWriter
-----------
451
.. ocv:class:: VideoWriter
452

453
Video writer class.
454

455 456 457 458 459 460 461


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

.. ocv:function:: VideoWriter::VideoWriter()
462

A
Andrey Kamaev 已提交
463
.. ocv:function:: VideoWriter::VideoWriter(const String& filename, int fourcc, double fps, Size frameSize, bool isColor=true)
464 465 466

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

467
.. ocv:cfunction:: CvVideoWriter* cvCreateVideoWriter( const char* filename, int fourcc, double fps, CvSize frame_size, int is_color=1 )
468 469 470 471 472

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

473
    :param filename: Name of the output video file.
474

475
    :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.
476

477
    :param fps: Framerate of the created video stream.
478

479
    :param frameSize: Size of the  video frames.
480

481
    :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).
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499

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.

A
Andrey Kamaev 已提交
500
.. ocv:function:: bool VideoWriter::open(const String& filename, int fourcc, double fps, Size frameSize, bool isColor=true)
501 502 503 504 505 506 507 508 509 510

.. 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.

511
.. ocv:function:: bool VideoWriter::isOpened()
512 513 514 515 516 517 518 519 520

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


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

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

522 523 524 525 526 527
.. ocv:function:: void VideoWriter::write(const Mat& image)

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

.. ocv:cfunction:: int cvWriteFrame( CvVideoWriter* writer, const IplImage* image )

528 529 530 531
    :param writer: Video writer structure (OpenCV 1.x API)

    :param image: The written frame

532
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.
533