2010-05-12 01:44:00 +08:00
|
|
|
/*M///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
|
|
//
|
|
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
|
|
// If you do not agree to this license, do not download, install,
|
|
|
|
// copy or use the software.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Intel License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
|
|
|
// Copyright (C) 2000, Intel Corporation, all rights reserved.
|
|
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
// are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistribution's in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// * The name of Intel Corporation may not be used to endorse or promote products
|
|
|
|
// derived from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// This software is provided by the copyright holders and contributors "as is" and
|
|
|
|
// any express or implied warranties, including, but not limited to, the implied
|
|
|
|
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
|
|
|
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
|
|
|
// indirect, incidental, special, exemplary, or consequential damages
|
|
|
|
// (including, but not limited to, procurement of substitute goods or services;
|
|
|
|
// loss of use, data, or profits; or business interruption) however caused
|
|
|
|
// and on any theory of liability, whether in contract, strict liability,
|
|
|
|
// or tort (including negligence or otherwise) arising in any way out of
|
|
|
|
// the use of this software, even if advised of the possibility of such damage.
|
|
|
|
//
|
|
|
|
//M*/
|
|
|
|
|
|
|
|
#include "precomp.hpp"
|
|
|
|
|
2018-05-21 22:06:31 +08:00
|
|
|
#include "videoio_registry.hpp"
|
2018-04-20 00:54:03 +08:00
|
|
|
|
2018-05-21 22:06:31 +08:00
|
|
|
namespace cv {
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-08-13 20:47:18 +08:00
|
|
|
template<> void DefaultDeleter<CvCapture>::operator ()(CvCapture* obj) const
|
2010-05-12 01:44:00 +08:00
|
|
|
{ cvReleaseCapture(&obj); }
|
|
|
|
|
2013-08-13 20:47:18 +08:00
|
|
|
template<> void DefaultDeleter<CvVideoWriter>::operator ()(CvVideoWriter* obj) const
|
2010-05-12 01:44:00 +08:00
|
|
|
{ cvReleaseVideoWriter(&obj); }
|
|
|
|
|
2015-08-14 19:40:24 +08:00
|
|
|
|
2015-03-26 05:39:29 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
VideoCapture::VideoCapture()
|
|
|
|
{}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2015-05-12 23:43:28 +08:00
|
|
|
VideoCapture::VideoCapture(const String& filename, int apiPreference)
|
|
|
|
{
|
2017-05-25 23:59:01 +08:00
|
|
|
CV_TRACE_FUNCTION();
|
2015-05-12 23:43:28 +08:00
|
|
|
open(filename, apiPreference);
|
|
|
|
}
|
|
|
|
|
2013-03-23 00:37:49 +08:00
|
|
|
VideoCapture::VideoCapture(const String& filename)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2017-05-25 23:59:01 +08:00
|
|
|
CV_TRACE_FUNCTION();
|
2015-05-12 23:43:28 +08:00
|
|
|
open(filename, CAP_ANY);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2015-05-12 23:43:28 +08:00
|
|
|
VideoCapture::VideoCapture(int index)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2017-05-25 23:59:01 +08:00
|
|
|
CV_TRACE_FUNCTION();
|
2015-05-12 23:43:28 +08:00
|
|
|
open(index);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
VideoCapture::~VideoCapture()
|
|
|
|
{
|
2017-05-25 23:59:01 +08:00
|
|
|
CV_TRACE_FUNCTION();
|
|
|
|
|
2014-02-17 21:50:15 +08:00
|
|
|
icap.release();
|
2010-05-12 01:44:00 +08:00
|
|
|
cap.release();
|
|
|
|
}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2015-05-12 23:43:28 +08:00
|
|
|
bool VideoCapture::open(const String& filename, int apiPreference)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2017-05-25 23:59:01 +08:00
|
|
|
CV_TRACE_FUNCTION();
|
2016-08-18 14:53:00 +08:00
|
|
|
|
2013-07-10 21:43:47 +08:00
|
|
|
if (isOpened()) release();
|
2015-04-06 23:19:22 +08:00
|
|
|
|
2018-05-21 22:06:31 +08:00
|
|
|
const std::vector<VideoBackendInfo> backends = cv::videoio_registry::getAvailableBackends_CaptureByFilename();
|
|
|
|
for (size_t i = 0; i < backends.size(); i++)
|
|
|
|
{
|
|
|
|
const VideoBackendInfo& info = backends[i];
|
|
|
|
if (apiPreference == CAP_ANY || apiPreference == info.id)
|
|
|
|
{
|
|
|
|
CvCapture* capture = NULL;
|
|
|
|
VideoCapture_create(capture, icap, info.id, filename);
|
|
|
|
if (!icap.empty())
|
|
|
|
{
|
|
|
|
if (icap->isOpened())
|
|
|
|
return true;
|
|
|
|
icap.release();
|
|
|
|
}
|
|
|
|
if (capture)
|
|
|
|
{
|
|
|
|
cap.reset(capture);
|
|
|
|
// assume it is opened
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2015-05-12 23:43:28 +08:00
|
|
|
bool VideoCapture::open(const String& filename)
|
|
|
|
{
|
2017-05-25 23:59:01 +08:00
|
|
|
CV_TRACE_FUNCTION();
|
2016-08-18 14:53:00 +08:00
|
|
|
|
2015-05-12 23:43:28 +08:00
|
|
|
return open(filename, CAP_ANY);
|
|
|
|
}
|
|
|
|
|
2018-05-21 22:06:31 +08:00
|
|
|
bool VideoCapture::open(int cameraNum, int apiPreference)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2017-05-25 23:59:01 +08:00
|
|
|
CV_TRACE_FUNCTION();
|
2016-08-18 14:53:00 +08:00
|
|
|
|
2013-07-10 21:43:47 +08:00
|
|
|
if (isOpened()) release();
|
2018-05-21 22:06:31 +08:00
|
|
|
|
|
|
|
const std::vector<VideoBackendInfo> backends = cv::videoio_registry::getAvailableBackends_CaptureByIndex();
|
|
|
|
for (size_t i = 0; i < backends.size(); i++)
|
|
|
|
{
|
|
|
|
const VideoBackendInfo& info = backends[i];
|
|
|
|
if (apiPreference == CAP_ANY || apiPreference == info.id)
|
|
|
|
{
|
|
|
|
CvCapture* capture = NULL;
|
|
|
|
VideoCapture_create(capture, icap, info.id, cameraNum);
|
|
|
|
if (!icap.empty())
|
|
|
|
{
|
|
|
|
if (icap->isOpened())
|
|
|
|
return true;
|
|
|
|
icap.release();
|
|
|
|
}
|
|
|
|
if (capture)
|
|
|
|
{
|
|
|
|
cap.reset(capture);
|
|
|
|
// assume it is opened
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2018-05-21 22:06:31 +08:00
|
|
|
|
|
|
|
bool VideoCapture::open(int index)
|
2016-11-09 19:19:57 +08:00
|
|
|
{
|
2017-05-25 23:59:01 +08:00
|
|
|
CV_TRACE_FUNCTION();
|
|
|
|
|
2018-05-21 22:06:31 +08:00
|
|
|
// interpret preferred interface (0 = autodetect)
|
|
|
|
int backendID = (index / 100) * 100;
|
|
|
|
if (backendID)
|
|
|
|
{
|
|
|
|
index %= 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
return open(index, backendID);
|
2016-11-09 19:19:57 +08:00
|
|
|
}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2014-02-17 21:50:15 +08:00
|
|
|
bool VideoCapture::isOpened() const
|
|
|
|
{
|
2018-05-21 22:06:31 +08:00
|
|
|
if (!icap.empty())
|
|
|
|
return icap->isOpened();
|
2018-05-31 19:07:22 +08:00
|
|
|
return !cap.empty(); // legacy interface doesn't support closed files
|
2014-02-17 21:50:15 +08:00
|
|
|
}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
void VideoCapture::release()
|
|
|
|
{
|
2017-05-25 23:59:01 +08:00
|
|
|
CV_TRACE_FUNCTION();
|
2014-02-17 21:50:15 +08:00
|
|
|
icap.release();
|
2010-05-12 01:44:00 +08:00
|
|
|
cap.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VideoCapture::grab()
|
|
|
|
{
|
2018-09-14 05:35:26 +08:00
|
|
|
CV_INSTRUMENT_REGION();
|
2016-08-18 14:53:00 +08:00
|
|
|
|
2014-02-17 21:50:15 +08:00
|
|
|
if (!icap.empty())
|
|
|
|
return icap->grabFrame();
|
2010-05-12 01:44:00 +08:00
|
|
|
return cvGrabFrame(cap) != 0;
|
|
|
|
}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2013-11-19 00:48:00 +08:00
|
|
|
bool VideoCapture::retrieve(OutputArray image, int channel)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-09-14 05:35:26 +08:00
|
|
|
CV_INSTRUMENT_REGION();
|
2016-08-18 14:53:00 +08:00
|
|
|
|
2014-02-17 21:50:15 +08:00
|
|
|
if (!icap.empty())
|
|
|
|
return icap->retrieveFrame(channel, image);
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
IplImage* _img = cvRetrieveFrame(cap, channel);
|
|
|
|
if( !_img )
|
|
|
|
{
|
|
|
|
image.release();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(_img->origin == IPL_ORIGIN_TL)
|
2013-11-11 20:55:36 +08:00
|
|
|
cv::cvarrToMat(_img).copyTo(image);
|
2010-05-12 01:44:00 +08:00
|
|
|
else
|
|
|
|
{
|
2013-03-29 01:01:12 +08:00
|
|
|
Mat temp = cv::cvarrToMat(_img);
|
2010-05-12 01:44:00 +08:00
|
|
|
flip(temp, image, 0);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2010-11-03 01:58:22 +08:00
|
|
|
|
2013-11-19 00:48:00 +08:00
|
|
|
bool VideoCapture::read(OutputArray image)
|
2010-11-03 01:58:22 +08:00
|
|
|
{
|
2018-09-14 05:35:26 +08:00
|
|
|
CV_INSTRUMENT_REGION();
|
2016-08-18 14:53:00 +08:00
|
|
|
|
2012-06-01 16:55:16 +08:00
|
|
|
if(grab())
|
|
|
|
retrieve(image);
|
2012-05-06 01:28:05 +08:00
|
|
|
else
|
|
|
|
image.release();
|
2010-11-03 01:58:22 +08:00
|
|
|
return !image.empty();
|
|
|
|
}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
VideoCapture& VideoCapture::operator >> (Mat& image)
|
|
|
|
{
|
2015-05-15 21:28:47 +08:00
|
|
|
#ifdef WINRT_VIDEO
|
2018-05-21 22:06:31 +08:00
|
|
|
// FIXIT grab/retrieve methods() should work too
|
2015-05-15 21:28:47 +08:00
|
|
|
if (grab())
|
|
|
|
{
|
|
|
|
if (retrieve(image))
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(VideoioBridge::getInstance().inputBufferMutex);
|
|
|
|
VideoioBridge& bridge = VideoioBridge::getInstance();
|
|
|
|
|
|
|
|
// double buffering
|
|
|
|
bridge.swapInputBuffers();
|
|
|
|
auto p = bridge.frontInputPtr;
|
|
|
|
|
|
|
|
bridge.bIsFrameNew = false;
|
|
|
|
|
|
|
|
// needed here because setting Mat 'image' is not allowed by OutputArray in read()
|
2015-05-18 20:21:49 +08:00
|
|
|
Mat m(bridge.getHeight(), bridge.getWidth(), CV_8UC3, p);
|
2015-05-15 21:28:47 +08:00
|
|
|
image = m;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2012-05-22 15:58:31 +08:00
|
|
|
read(image);
|
2015-05-15 21:28:47 +08:00
|
|
|
#endif
|
2010-05-12 01:44:00 +08:00
|
|
|
return *this;
|
|
|
|
}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2013-11-19 00:48:00 +08:00
|
|
|
VideoCapture& VideoCapture::operator >> (UMat& image)
|
|
|
|
{
|
2018-09-14 05:35:26 +08:00
|
|
|
CV_INSTRUMENT_REGION();
|
2016-08-18 14:53:00 +08:00
|
|
|
|
2013-11-19 00:48:00 +08:00
|
|
|
read(image);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
bool VideoCapture::set(int propId, double value)
|
|
|
|
{
|
2014-02-17 21:50:15 +08:00
|
|
|
if (!icap.empty())
|
|
|
|
return icap->setProperty(propId, value);
|
2010-05-12 01:44:00 +08:00
|
|
|
return cvSetCaptureProperty(cap, propId, value) != 0;
|
|
|
|
}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2014-12-11 01:17:35 +08:00
|
|
|
double VideoCapture::get(int propId) const
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-02-17 21:50:15 +08:00
|
|
|
if (!icap.empty())
|
|
|
|
return icap->getProperty(propId);
|
2018-05-21 22:06:31 +08:00
|
|
|
return cap ? cap->getProperty(propId) : 0;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2014-02-17 21:50:15 +08:00
|
|
|
|
2018-05-21 22:06:31 +08:00
|
|
|
//=================================================================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
VideoWriter::VideoWriter()
|
|
|
|
{}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2013-04-08 02:45:38 +08:00
|
|
|
VideoWriter::VideoWriter(const String& filename, int _fourcc, double fps, Size frameSize, bool isColor)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2013-04-08 02:45:38 +08:00
|
|
|
open(filename, _fourcc, fps, frameSize, isColor);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2017-06-14 22:09:32 +08:00
|
|
|
|
2017-06-26 03:53:11 +08:00
|
|
|
VideoWriter::VideoWriter(const String& filename, int apiPreference, int _fourcc, double fps, Size frameSize, bool isColor)
|
2017-06-14 22:09:32 +08:00
|
|
|
{
|
2017-06-26 03:53:11 +08:00
|
|
|
open(filename, apiPreference, _fourcc, fps, frameSize, isColor);
|
2017-06-14 22:09:32 +08:00
|
|
|
}
|
|
|
|
|
2012-04-14 05:50:59 +08:00
|
|
|
void VideoWriter::release()
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2015-03-26 05:39:29 +08:00
|
|
|
iwriter.release();
|
2010-05-12 01:44:00 +08:00
|
|
|
writer.release();
|
2012-06-08 01:21:29 +08:00
|
|
|
}
|
|
|
|
|
2012-04-14 05:50:59 +08:00
|
|
|
VideoWriter::~VideoWriter()
|
|
|
|
{
|
|
|
|
release();
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2013-04-08 02:45:38 +08:00
|
|
|
bool VideoWriter::open(const String& filename, int _fourcc, double fps, Size frameSize, bool isColor)
|
2017-06-14 22:09:32 +08:00
|
|
|
{
|
2017-06-26 03:53:11 +08:00
|
|
|
return open(filename, CAP_ANY, _fourcc, fps, frameSize, isColor);
|
2017-06-14 22:09:32 +08:00
|
|
|
}
|
|
|
|
|
2017-06-26 03:53:11 +08:00
|
|
|
bool VideoWriter::open(const String& filename, int apiPreference, int _fourcc, double fps, Size frameSize, bool isColor)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-09-14 05:35:26 +08:00
|
|
|
CV_INSTRUMENT_REGION();
|
2016-08-18 14:53:00 +08:00
|
|
|
|
2015-03-26 05:39:29 +08:00
|
|
|
if (isOpened()) release();
|
2018-05-21 22:06:31 +08:00
|
|
|
|
|
|
|
const std::vector<VideoBackendInfo> backends = cv::videoio_registry::getAvailableBackends_Writer();
|
|
|
|
for (size_t i = 0; i < backends.size(); i++)
|
|
|
|
{
|
|
|
|
const VideoBackendInfo& info = backends[i];
|
|
|
|
if (apiPreference == CAP_ANY || apiPreference == info.id)
|
|
|
|
{
|
|
|
|
CvVideoWriter* writer_ = NULL;
|
|
|
|
VideoWriter_create(writer_, iwriter, info.id, filename, _fourcc, fps, frameSize, isColor);
|
|
|
|
if (!iwriter.empty())
|
|
|
|
{
|
|
|
|
if (iwriter->isOpened())
|
|
|
|
return true;
|
|
|
|
iwriter.release();
|
|
|
|
}
|
|
|
|
if (writer_)
|
|
|
|
{
|
|
|
|
// assume it is opened
|
|
|
|
writer.reset(writer_);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool VideoWriter::isOpened() const
|
|
|
|
{
|
2015-03-26 05:39:29 +08:00
|
|
|
return !iwriter.empty() || !writer.empty();
|
2012-06-08 01:21:29 +08:00
|
|
|
}
|
2010-11-03 01:58:22 +08:00
|
|
|
|
2015-03-27 20:15:59 +08:00
|
|
|
|
|
|
|
bool VideoWriter::set(int propId, double value)
|
|
|
|
{
|
|
|
|
if (!iwriter.empty())
|
|
|
|
return iwriter->setProperty(propId, value);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
double VideoWriter::get(int propId) const
|
|
|
|
{
|
|
|
|
if (!iwriter.empty())
|
|
|
|
return iwriter->getProperty(propId);
|
|
|
|
return 0.;
|
|
|
|
}
|
|
|
|
|
2010-11-03 01:58:22 +08:00
|
|
|
void VideoWriter::write(const Mat& image)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-09-14 05:35:26 +08:00
|
|
|
CV_INSTRUMENT_REGION();
|
2016-08-18 14:53:00 +08:00
|
|
|
|
2015-03-26 05:39:29 +08:00
|
|
|
if( iwriter )
|
|
|
|
iwriter->write(image);
|
|
|
|
else
|
|
|
|
{
|
2018-09-06 19:34:16 +08:00
|
|
|
IplImage _img = cvIplImage(image);
|
2015-03-26 05:39:29 +08:00
|
|
|
cvWriteFrame(writer, &_img);
|
|
|
|
}
|
2010-11-03 01:58:22 +08:00
|
|
|
}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2010-11-03 01:58:22 +08:00
|
|
|
VideoWriter& VideoWriter::operator << (const Mat& image)
|
|
|
|
{
|
2018-09-14 05:35:26 +08:00
|
|
|
CV_INSTRUMENT_REGION();
|
2016-08-18 14:53:00 +08:00
|
|
|
|
2010-11-03 01:58:22 +08:00
|
|
|
write(image);
|
2012-06-08 01:21:29 +08:00
|
|
|
return *this;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2018-05-21 22:06:31 +08:00
|
|
|
// FIXIT OpenCV 4.0: make inline
|
2013-04-08 02:45:38 +08:00
|
|
|
int VideoWriter::fourcc(char c1, char c2, char c3, char c4)
|
|
|
|
{
|
|
|
|
return (c1 & 255) + ((c2 & 255) << 8) + ((c3 & 255) << 16) + ((c4 & 255) << 24);
|
|
|
|
}
|
|
|
|
|
2018-05-21 22:06:31 +08:00
|
|
|
} // namespace
|