~paparazzi-uav/paparazzi/v5.0-manual

« back to all changes in this revision

Viewing changes to sw/ext/opencv_bebop/opencv/modules/videoio/src/cap.cpp

  • Committer: Paparazzi buildbot
  • Date: 2016-05-18 15:00:29 UTC
  • Revision ID: felix.ruess+docbot@gmail.com-20160518150029-e8lgzi5kvb4p7un9
Manual import commit 4b8bbb730080dac23cf816b98908dacfabe2a8ec from v5.0 branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*M///////////////////////////////////////////////////////////////////////////////////////
 
2
//
 
3
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
 
4
//
 
5
//  By downloading, copying, installing or using the software you agree to this license.
 
6
//  If you do not agree to this license, do not download, install,
 
7
//  copy or use the software.
 
8
//
 
9
//
 
10
//                        Intel License Agreement
 
11
//                For Open Source Computer Vision Library
 
12
//
 
13
// Copyright (C) 2000, Intel Corporation, all rights reserved.
 
14
// Third party copyrights are property of their respective owners.
 
15
//
 
16
// Redistribution and use in source and binary forms, with or without modification,
 
17
// are permitted provided that the following conditions are met:
 
18
//
 
19
//   * Redistribution's of source code must retain the above copyright notice,
 
20
//     this list of conditions and the following disclaimer.
 
21
//
 
22
//   * Redistribution's in binary form must reproduce the above copyright notice,
 
23
//     this list of conditions and the following disclaimer in the documentation
 
24
//     and/or other materials provided with the distribution.
 
25
//
 
26
//   * The name of Intel Corporation may not be used to endorse or promote products
 
27
//     derived from this software without specific prior written permission.
 
28
//
 
29
// This software is provided by the copyright holders and contributors "as is" and
 
30
// any express or implied warranties, including, but not limited to, the implied
 
31
// warranties of merchantability and fitness for a particular purpose are disclaimed.
 
32
// In no event shall the Intel Corporation or contributors be liable for any direct,
 
33
// indirect, incidental, special, exemplary, or consequential damages
 
34
// (including, but not limited to, procurement of substitute goods or services;
 
35
// loss of use, data, or profits; or business interruption) however caused
 
36
// and on any theory of liability, whether in contract, strict liability,
 
37
// or tort (including negligence or otherwise) arising in any way out of
 
38
// the use of this software, even if advised of the possibility of such damage.
 
39
//
 
40
//M*/
 
41
 
 
42
#include "precomp.hpp"
 
43
#include "cap_intelperc.hpp"
 
44
#include "cap_dshow.hpp"
 
45
 
 
46
// All WinRT versions older than 8.0 should provide classes used for video support
 
47
#if defined(WINRT) && !defined(WINRT_8_0) && defined(__cplusplus_winrt)
 
48
#   include "cap_winrt_capture.hpp"
 
49
#   include "cap_winrt_bridge.hpp"
 
50
#   define WINRT_VIDEO
 
51
#endif
 
52
 
 
53
#if defined _M_X64 && defined _MSC_VER && !defined CV_ICC
 
54
#pragma optimize("",off)
 
55
#pragma warning(disable: 4748)
 
56
#endif
 
57
 
 
58
namespace cv
 
59
{
 
60
 
 
61
template<> void DefaultDeleter<CvCapture>::operator ()(CvCapture* obj) const
 
62
{ cvReleaseCapture(&obj); }
 
63
 
 
64
template<> void DefaultDeleter<CvVideoWriter>::operator ()(CvVideoWriter* obj) const
 
65
{ cvReleaseVideoWriter(&obj); }
 
66
 
 
67
}
 
68
 
 
69
/************************* Reading AVIs & Camera data **************************/
 
70
 
 
71
static inline double icvGetCaptureProperty( const CvCapture* capture, int id )
 
72
{
 
73
    return capture ? capture->getProperty(id) : 0;
 
74
}
 
75
 
 
76
CV_IMPL void cvReleaseCapture( CvCapture** pcapture )
 
77
{
 
78
    if( pcapture && *pcapture )
 
79
    {
 
80
        delete *pcapture;
 
81
        *pcapture = 0;
 
82
    }
 
83
}
 
84
 
 
85
CV_IMPL IplImage* cvQueryFrame( CvCapture* capture )
 
86
{
 
87
    if(!capture)
 
88
        return 0;
 
89
    if(!capture->grabFrame())
 
90
        return 0;
 
91
    return capture->retrieveFrame(0);
 
92
}
 
93
 
 
94
 
 
95
CV_IMPL int cvGrabFrame( CvCapture* capture )
 
96
{
 
97
    return capture ? capture->grabFrame() : 0;
 
98
}
 
99
 
 
100
CV_IMPL IplImage* cvRetrieveFrame( CvCapture* capture, int idx )
 
101
{
 
102
    return capture ? capture->retrieveFrame(idx) : 0;
 
103
}
 
104
 
 
105
CV_IMPL double cvGetCaptureProperty( CvCapture* capture, int id )
 
106
{
 
107
    return icvGetCaptureProperty(capture, id);
 
108
}
 
109
 
 
110
CV_IMPL int cvSetCaptureProperty( CvCapture* capture, int id, double value )
 
111
{
 
112
    return capture ? capture->setProperty(id, value) : 0;
 
113
}
 
114
 
 
115
CV_IMPL int cvGetCaptureDomain( CvCapture* capture)
 
116
{
 
117
    return capture ? capture->getCaptureDomain() : 0;
 
118
}
 
119
 
 
120
 
 
121
/**
 
122
 * Camera dispatching method: index is the camera number.
 
123
 * If given an index from 0 to 99, it tries to find the first
 
124
 * API that can access a given camera index.
 
125
 * Add multiples of 100 to select an API.
 
126
 */
 
127
CV_IMPL CvCapture * cvCreateCameraCapture (int index)
 
128
{
 
129
    // interpret preferred interface (0 = autodetect)
 
130
    int pref = (index / 100) * 100;
 
131
 
 
132
    // remove pref from index
 
133
    index -= pref;
 
134
 
 
135
    // local variable to memorize the captured device
 
136
    CvCapture *capture = 0;
 
137
 
 
138
    switch (pref)
 
139
    {
 
140
    default:
 
141
        // user specified an API we do not know
 
142
        // bail out to let the user know that it is not available
 
143
        if (pref) break;
 
144
 
 
145
#ifdef HAVE_MSMF
 
146
    case CV_CAP_MSMF:
 
147
        if (!capture)
 
148
            capture = cvCreateCameraCapture_MSMF(index);
 
149
        if (pref) break;
 
150
#endif
 
151
#ifdef HAVE_TYZX
 
152
    case CV_CAP_STEREO:
 
153
        if (!capture)
 
154
            capture = cvCreateCameraCapture_TYZX(index);
 
155
        if (pref) break;
 
156
#endif
 
157
    case CV_CAP_VFW:
 
158
#ifdef HAVE_VFW
 
159
        if (!capture)
 
160
            capture = cvCreateCameraCapture_VFW(index);
 
161
#endif
 
162
#if defined HAVE_LIBV4L || defined HAVE_CAMV4L || defined HAVE_CAMV4L2 || defined HAVE_VIDEOIO
 
163
        if (!capture)
 
164
            capture = cvCreateCameraCapture_V4L(index);
 
165
#endif
 
166
 
 
167
#ifdef HAVE_GSTREAMER
 
168
        if (!capture)
 
169
            capture = cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_V4L2,
 
170
                                                reinterpret_cast<char *>(index));
 
171
 
 
172
        if (!capture)
 
173
            capture = cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_V4L,
 
174
                                                reinterpret_cast<char *>(index));
 
175
#endif
 
176
        if (pref) break; // CV_CAP_VFW
 
177
 
 
178
    case CV_CAP_FIREWIRE:
 
179
#ifdef HAVE_DC1394_2
 
180
        if (!capture)
 
181
            capture = cvCreateCameraCapture_DC1394_2(index);
 
182
#endif
 
183
 
 
184
#ifdef HAVE_DC1394
 
185
        if (!capture)
 
186
            capture = cvCreateCameraCapture_DC1394(index);
 
187
#endif
 
188
 
 
189
#ifdef HAVE_CMU1394
 
190
        if (!capture)
 
191
            capture = cvCreateCameraCapture_CMU(index);
 
192
#endif
 
193
 
 
194
#if defined(HAVE_GSTREAMER) && 0
 
195
        // Re-enable again when gstreamer 1394 support will land in the backend code
 
196
        if (!capture)
 
197
            capture = cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_1394, 0);
 
198
#endif
 
199
        if (pref) break; // CV_CAP_FIREWIRE
 
200
 
 
201
#ifdef HAVE_MIL
 
202
    case CV_CAP_MIL:
 
203
        if (!capture)
 
204
            capture = cvCreateCameraCapture_MIL(index);
 
205
        if (pref) break;
 
206
#endif
 
207
 
 
208
#if defined(HAVE_QUICKTIME) || defined(HAVE_QTKIT)
 
209
    case CV_CAP_QT:
 
210
        if (!capture)
 
211
            capture = cvCreateCameraCapture_QT(index);
 
212
        if (pref) break;
 
213
#endif
 
214
 
 
215
#ifdef HAVE_UNICAP
 
216
    case CV_CAP_UNICAP:
 
217
        if (!capture)
 
218
            capture = cvCreateCameraCapture_Unicap(index);
 
219
        if (pref) break;
 
220
#endif
 
221
 
 
222
#ifdef HAVE_PVAPI
 
223
    case CV_CAP_PVAPI:
 
224
        if (!capture)
 
225
            capture = cvCreateCameraCapture_PvAPI(index);
 
226
        if (pref) break;
 
227
#endif
 
228
 
 
229
#ifdef HAVE_OPENNI
 
230
    case CV_CAP_OPENNI:
 
231
        if (!capture)
 
232
            capture = cvCreateCameraCapture_OpenNI(index);
 
233
        if (pref) break;
 
234
#endif
 
235
 
 
236
#ifdef HAVE_OPENNI2
 
237
    case CV_CAP_OPENNI2:
 
238
        if (!capture)
 
239
            capture = cvCreateCameraCapture_OpenNI2(index);
 
240
        if (pref) break;
 
241
#endif
 
242
 
 
243
#ifdef HAVE_XIMEA
 
244
    case CV_CAP_XIAPI:
 
245
        if (!capture)
 
246
            capture = cvCreateCameraCapture_XIMEA(index);
 
247
        if (pref) break;
 
248
#endif
 
249
 
 
250
#ifdef HAVE_AVFOUNDATION
 
251
    case CV_CAP_AVFOUNDATION:
 
252
        if (!capture)
 
253
            capture = cvCreateCameraCapture_AVFoundation(index);
 
254
        if (pref) break;
 
255
#endif
 
256
 
 
257
#ifdef HAVE_GIGE_API
 
258
    case CV_CAP_GIGANETIX:
 
259
        if (!capture)
 
260
            capture = cvCreateCameraCapture_Giganetix(index);
 
261
        if (pref) break; // CV_CAP_GIGANETIX
 
262
#endif
 
263
    }
 
264
 
 
265
    return capture;
 
266
}
 
267
 
 
268
/**
 
269
 * Videoreader dispatching method: it tries to find the first
 
270
 * API that can access a given filename.
 
271
 */
 
272
CV_IMPL CvCapture * cvCreateFileCaptureWithPreference (const char * filename, int apiPreference)
 
273
{
 
274
    CvCapture * result = 0;
 
275
 
 
276
    switch(apiPreference) {
 
277
    default:
 
278
        // user specified an API we do not know
 
279
        // bail out to let the user know that it is not available
 
280
        if (apiPreference) break;
 
281
 
 
282
#ifdef HAVE_FFMPEG
 
283
    case CV_CAP_FFMPEG:
 
284
        if (! result)
 
285
            result = cvCreateFileCapture_FFMPEG_proxy (filename);
 
286
        if (apiPreference) break;
 
287
#endif
 
288
 
 
289
#ifdef HAVE_VFW
 
290
    case CV_CAP_VFW:
 
291
        if (! result)
 
292
            result = cvCreateFileCapture_VFW (filename);
 
293
        if (apiPreference) break;
 
294
#endif
 
295
 
 
296
    case CV_CAP_MSMF:
 
297
#ifdef HAVE_MSMF
 
298
        if (! result)
 
299
            result = cvCreateFileCapture_MSMF (filename);
 
300
#endif
 
301
 
 
302
#ifdef HAVE_XINE
 
303
        if (! result)
 
304
            result = cvCreateFileCapture_XINE (filename);
 
305
#endif
 
306
        if (apiPreference) break;
 
307
 
 
308
#ifdef HAVE_GSTREAMER
 
309
    case CV_CAP_GSTREAMER:
 
310
        if (! result)
 
311
            result = cvCreateCapture_GStreamer (CV_CAP_GSTREAMER_FILE, filename);
 
312
        if (apiPreference) break;
 
313
#endif
 
314
 
 
315
#if defined(HAVE_QUICKTIME) || defined(HAVE_QTKIT)
 
316
    case CV_CAP_QT:
 
317
        if (! result)
 
318
            result = cvCreateFileCapture_QT (filename);
 
319
        if (apiPreference) break;
 
320
#endif
 
321
 
 
322
#ifdef HAVE_AVFOUNDATION
 
323
    case CV_CAP_AVFOUNDATION:
 
324
        if (! result)
 
325
            result = cvCreateFileCapture_AVFoundation (filename);
 
326
        if (apiPreference) break;
 
327
#endif
 
328
 
 
329
#ifdef HAVE_OPENNI
 
330
    case CV_CAP_OPENNI:
 
331
        if (! result)
 
332
            result = cvCreateFileCapture_OpenNI (filename);
 
333
        if (apiPreference) break;
 
334
#endif
 
335
 
 
336
    case CV_CAP_IMAGES:
 
337
        if (! result)
 
338
            result = cvCreateFileCapture_Images (filename);
 
339
    }
 
340
 
 
341
    return result;
 
342
}
 
343
 
 
344
CV_IMPL CvCapture * cvCreateFileCapture (const char * filename)
 
345
{
 
346
    return cvCreateFileCaptureWithPreference(filename, CV_CAP_ANY);
 
347
}
 
348
 
 
349
/**
 
350
 * Videowriter dispatching method: it tries to find the first
 
351
 * API that can write a given stream.
 
352
 */
 
353
CV_IMPL CvVideoWriter* cvCreateVideoWriter( const char* filename, int fourcc,
 
354
                                            double fps, CvSize frameSize, int is_color )
 
355
{
 
356
    //CV_FUNCNAME( "cvCreateVideoWriter" );
 
357
 
 
358
    CvVideoWriter *result = 0;
 
359
 
 
360
    if(!fourcc || !fps)
 
361
        result = cvCreateVideoWriter_Images(filename);
 
362
 
 
363
#ifdef HAVE_FFMPEG
 
364
    if(!result)
 
365
        result = cvCreateVideoWriter_FFMPEG_proxy (filename, fourcc, fps, frameSize, is_color);
 
366
#endif
 
367
 
 
368
#ifdef HAVE_VFW
 
369
    if(!result)
 
370
        result = cvCreateVideoWriter_VFW(filename, fourcc, fps, frameSize, is_color);
 
371
#endif
 
372
 
 
373
#ifdef HAVE_MSMF
 
374
    if (!result)
 
375
        result = cvCreateVideoWriter_MSMF(filename, fourcc, fps, frameSize, is_color);
 
376
#endif
 
377
 
 
378
/*  #ifdef HAVE_XINE
 
379
    if(!result)
 
380
        result = cvCreateVideoWriter_XINE(filename, fourcc, fps, frameSize, is_color);
 
381
    #endif
 
382
*/
 
383
#ifdef HAVE_AVFOUNDATION
 
384
    if (! result)
 
385
        result = cvCreateVideoWriter_AVFoundation(filename, fourcc, fps, frameSize, is_color);
 
386
#endif
 
387
 
 
388
#if defined(HAVE_QUICKTIME) || defined(HAVE_QTKIT)
 
389
    if(!result)
 
390
        result = cvCreateVideoWriter_QT(filename, fourcc, fps, frameSize, is_color);
 
391
#endif
 
392
 
 
393
#ifdef HAVE_GSTREAMER
 
394
    if (! result)
 
395
        result = cvCreateVideoWriter_GStreamer(filename, fourcc, fps, frameSize, is_color);
 
396
#endif
 
397
 
 
398
#if !defined(HAVE_FFMPEG) && \
 
399
    !defined(HAVE_VFW) && \
 
400
    !defined(HAVE_MSMF) && \
 
401
    !defined(HAVE_AVFOUNDATION) && \
 
402
    !defined(HAVE_QUICKTIME) && \
 
403
    !defined(HAVE_QTKIT) && \
 
404
    !defined(HAVE_GSTREAMER)
 
405
// If none of the writers is used
 
406
// these statements suppress 'unused parameter' warnings.
 
407
    (void)frameSize;
 
408
    (void)is_color;
 
409
#endif
 
410
 
 
411
    if(!result)
 
412
        result = cvCreateVideoWriter_Images(filename);
 
413
 
 
414
    return result;
 
415
}
 
416
 
 
417
CV_IMPL int cvWriteFrame( CvVideoWriter* writer, const IplImage* image )
 
418
{
 
419
    return writer ? writer->writeFrame(image) : 0;
 
420
}
 
421
 
 
422
CV_IMPL void cvReleaseVideoWriter( CvVideoWriter** pwriter )
 
423
{
 
424
    if( pwriter && *pwriter )
 
425
    {
 
426
        delete *pwriter;
 
427
        *pwriter = 0;
 
428
    }
 
429
}
 
430
 
 
431
namespace cv
 
432
{
 
433
 
 
434
static Ptr<IVideoCapture> IVideoCapture_create(int index)
 
435
{
 
436
    int  domains[] =
 
437
    {
 
438
#ifdef HAVE_DSHOW
 
439
        CV_CAP_DSHOW,
 
440
#endif
 
441
#ifdef HAVE_INTELPERC
 
442
        CV_CAP_INTELPERC,
 
443
#endif
 
444
#ifdef WINRT_VIDEO
 
445
        CAP_WINRT,
 
446
#endif
 
447
#ifdef HAVE_GPHOTO2
 
448
        CV_CAP_GPHOTO2,
 
449
#endif
 
450
        -1, -1
 
451
    };
 
452
 
 
453
    // interpret preferred interface (0 = autodetect)
 
454
    int pref = (index / 100) * 100;
 
455
    if (pref)
 
456
    {
 
457
        domains[0]=pref;
 
458
        index %= 100;
 
459
        domains[1]=-1;
 
460
    }
 
461
 
 
462
    // try every possibly installed camera API
 
463
    for (int i = 0; domains[i] >= 0; i++)
 
464
    {
 
465
#if defined(HAVE_DSHOW)        || \
 
466
    defined(HAVE_INTELPERC)    || \
 
467
    defined(WINRT_VIDEO)       || \
 
468
    defined(HAVE_GPHOTO2)      || \
 
469
    (0)
 
470
        Ptr<IVideoCapture> capture;
 
471
 
 
472
        switch (domains[i])
 
473
        {
 
474
#ifdef HAVE_DSHOW
 
475
            case CV_CAP_DSHOW:
 
476
                capture = makePtr<VideoCapture_DShow>(index);
 
477
                break; // CV_CAP_DSHOW
 
478
#endif
 
479
#ifdef HAVE_INTELPERC
 
480
            case CV_CAP_INTELPERC:
 
481
                capture = makePtr<VideoCapture_IntelPerC>();
 
482
                break; // CV_CAP_INTEL_PERC
 
483
#endif
 
484
#ifdef WINRT_VIDEO
 
485
        case CAP_WINRT:
 
486
            capture = Ptr<IVideoCapture>(new cv::VideoCapture_WinRT(index));
 
487
            if (capture)
 
488
                return capture;
 
489
            break; // CAP_WINRT
 
490
#endif
 
491
#ifdef HAVE_GPHOTO2
 
492
            case CV_CAP_GPHOTO2:
 
493
                capture = createGPhoto2Capture(index);
 
494
                break;
 
495
#endif
 
496
        }
 
497
        if (capture && capture->isOpened())
 
498
            return capture;
 
499
#endif
 
500
    }
 
501
 
 
502
    // failed open a camera
 
503
    return Ptr<IVideoCapture>();
 
504
}
 
505
 
 
506
 
 
507
static Ptr<IVideoCapture> IVideoCapture_create(const String& filename)
 
508
{
 
509
    int  domains[] =
 
510
    {
 
511
        CV_CAP_ANY,
 
512
#ifdef HAVE_GPHOTO2
 
513
        CV_CAP_GPHOTO2,
 
514
#endif
 
515
        -1, -1
 
516
    };
 
517
 
 
518
    // try every possibly installed camera API
 
519
    for (int i = 0; domains[i] >= 0; i++)
 
520
    {
 
521
        Ptr<IVideoCapture> capture;
 
522
 
 
523
        switch (domains[i])
 
524
        {
 
525
        case CV_CAP_ANY:
 
526
            capture = createMotionJpegCapture(filename);
 
527
            break;
 
528
#ifdef HAVE_GPHOTO2
 
529
        case CV_CAP_GPHOTO2:
 
530
            capture = createGPhoto2Capture(filename);
 
531
            break;
 
532
#endif
 
533
        }
 
534
 
 
535
        if (capture && capture->isOpened())
 
536
        {
 
537
            return capture;
 
538
        }
 
539
    }
 
540
    // failed open a camera
 
541
    return Ptr<IVideoCapture>();
 
542
}
 
543
 
 
544
static Ptr<IVideoWriter> IVideoWriter_create(const String& filename, int _fourcc, double fps, Size frameSize, bool isColor)
 
545
{
 
546
    Ptr<IVideoWriter> iwriter;
 
547
    if( _fourcc == CV_FOURCC('M', 'J', 'P', 'G') )
 
548
        iwriter = createMotionJpegWriter(filename, fps, frameSize, isColor);
 
549
    return iwriter;
 
550
}
 
551
 
 
552
VideoCapture::VideoCapture()
 
553
{}
 
554
 
 
555
VideoCapture::VideoCapture(const String& filename, int apiPreference)
 
556
{
 
557
    open(filename, apiPreference);
 
558
}
 
559
 
 
560
VideoCapture::VideoCapture(const String& filename)
 
561
{
 
562
    open(filename, CAP_ANY);
 
563
}
 
564
 
 
565
VideoCapture::VideoCapture(int index)
 
566
{
 
567
    open(index);
 
568
}
 
569
 
 
570
VideoCapture::~VideoCapture()
 
571
{
 
572
    icap.release();
 
573
    cap.release();
 
574
}
 
575
 
 
576
bool VideoCapture::open(const String& filename, int apiPreference)
 
577
{
 
578
    if (isOpened()) release();
 
579
    icap = IVideoCapture_create(filename);
 
580
    if (!icap.empty())
 
581
        return true;
 
582
 
 
583
    cap.reset(cvCreateFileCaptureWithPreference(filename.c_str(), apiPreference));
 
584
    return isOpened();
 
585
}
 
586
 
 
587
bool VideoCapture::open(const String& filename)
 
588
{
 
589
    return open(filename, CAP_ANY);
 
590
}
 
591
 
 
592
bool VideoCapture::open(int index)
 
593
{
 
594
    if (isOpened()) release();
 
595
    icap = IVideoCapture_create(index);
 
596
    if (!icap.empty())
 
597
        return true;
 
598
    cap.reset(cvCreateCameraCapture(index));
 
599
    return isOpened();
 
600
}
 
601
 
 
602
bool VideoCapture::isOpened() const
 
603
{
 
604
    return (!cap.empty() || !icap.empty());
 
605
}
 
606
 
 
607
void VideoCapture::release()
 
608
{
 
609
    icap.release();
 
610
    cap.release();
 
611
}
 
612
 
 
613
bool VideoCapture::grab()
 
614
{
 
615
    if (!icap.empty())
 
616
        return icap->grabFrame();
 
617
    return cvGrabFrame(cap) != 0;
 
618
}
 
619
 
 
620
bool VideoCapture::retrieve(OutputArray image, int channel)
 
621
{
 
622
    if (!icap.empty())
 
623
        return icap->retrieveFrame(channel, image);
 
624
 
 
625
    IplImage* _img = cvRetrieveFrame(cap, channel);
 
626
    if( !_img )
 
627
    {
 
628
        image.release();
 
629
        return false;
 
630
    }
 
631
    if(_img->origin == IPL_ORIGIN_TL)
 
632
        cv::cvarrToMat(_img).copyTo(image);
 
633
    else
 
634
    {
 
635
        Mat temp = cv::cvarrToMat(_img);
 
636
        flip(temp, image, 0);
 
637
    }
 
638
    return true;
 
639
}
 
640
 
 
641
bool VideoCapture::read(OutputArray image)
 
642
{
 
643
    if(grab())
 
644
        retrieve(image);
 
645
    else
 
646
        image.release();
 
647
    return !image.empty();
 
648
}
 
649
 
 
650
VideoCapture& VideoCapture::operator >> (Mat& image)
 
651
{
 
652
#ifdef WINRT_VIDEO
 
653
    if (grab())
 
654
    {
 
655
        if (retrieve(image))
 
656
        {
 
657
            std::lock_guard<std::mutex> lock(VideoioBridge::getInstance().inputBufferMutex);
 
658
            VideoioBridge& bridge = VideoioBridge::getInstance();
 
659
 
 
660
            // double buffering
 
661
            bridge.swapInputBuffers();
 
662
            auto p = bridge.frontInputPtr;
 
663
 
 
664
            bridge.bIsFrameNew = false;
 
665
 
 
666
            // needed here because setting Mat 'image' is not allowed by OutputArray in read()
 
667
            Mat m(bridge.getHeight(), bridge.getWidth(), CV_8UC3, p);
 
668
            image = m;
 
669
        }
 
670
    }
 
671
#else
 
672
    read(image);
 
673
#endif
 
674
 
 
675
    return *this;
 
676
}
 
677
 
 
678
VideoCapture& VideoCapture::operator >> (UMat& image)
 
679
{
 
680
    read(image);
 
681
    return *this;
 
682
}
 
683
 
 
684
bool VideoCapture::set(int propId, double value)
 
685
{
 
686
    if (!icap.empty())
 
687
        return icap->setProperty(propId, value);
 
688
    return cvSetCaptureProperty(cap, propId, value) != 0;
 
689
}
 
690
 
 
691
double VideoCapture::get(int propId) const
 
692
{
 
693
    if (!icap.empty())
 
694
        return icap->getProperty(propId);
 
695
    return icvGetCaptureProperty(cap, propId);
 
696
}
 
697
 
 
698
 
 
699
VideoWriter::VideoWriter()
 
700
{}
 
701
 
 
702
VideoWriter::VideoWriter(const String& filename, int _fourcc, double fps, Size frameSize, bool isColor)
 
703
{
 
704
    open(filename, _fourcc, fps, frameSize, isColor);
 
705
}
 
706
 
 
707
void VideoWriter::release()
 
708
{
 
709
    iwriter.release();
 
710
    writer.release();
 
711
}
 
712
 
 
713
VideoWriter::~VideoWriter()
 
714
{
 
715
    release();
 
716
}
 
717
 
 
718
bool VideoWriter::open(const String& filename, int _fourcc, double fps, Size frameSize, bool isColor)
 
719
{
 
720
    if (isOpened()) release();
 
721
    iwriter = IVideoWriter_create(filename, _fourcc, fps, frameSize, isColor);
 
722
    if (!iwriter.empty())
 
723
        return true;
 
724
    writer.reset(cvCreateVideoWriter(filename.c_str(), _fourcc, fps, frameSize, isColor));
 
725
    return isOpened();
 
726
}
 
727
 
 
728
bool VideoWriter::isOpened() const
 
729
{
 
730
    return !iwriter.empty() || !writer.empty();
 
731
}
 
732
 
 
733
 
 
734
bool VideoWriter::set(int propId, double value)
 
735
{
 
736
    if (!iwriter.empty())
 
737
        return iwriter->setProperty(propId, value);
 
738
    return false;
 
739
}
 
740
 
 
741
double VideoWriter::get(int propId) const
 
742
{
 
743
    if (!iwriter.empty())
 
744
        return iwriter->getProperty(propId);
 
745
    return 0.;
 
746
}
 
747
 
 
748
void VideoWriter::write(const Mat& image)
 
749
{
 
750
    if( iwriter )
 
751
        iwriter->write(image);
 
752
    else
 
753
    {
 
754
        IplImage _img = image;
 
755
        cvWriteFrame(writer, &_img);
 
756
    }
 
757
}
 
758
 
 
759
VideoWriter& VideoWriter::operator << (const Mat& image)
 
760
{
 
761
    write(image);
 
762
    return *this;
 
763
}
 
764
 
 
765
int VideoWriter::fourcc(char c1, char c2, char c3, char c4)
 
766
{
 
767
    return (c1 & 255) + ((c2 & 255) << 8) + ((c3 & 255) << 16) + ((c4 & 255) << 24);
 
768
}
 
769
 
 
770
}