From 41d8c4d8790ac46bffec1206daccf5d61fefc0ab Mon Sep 17 00:00:00 2001 From: Pavel Rojtberg Date: Tue, 12 May 2015 17:43:28 +0200 Subject: [PATCH] allow specifying apiPreference in VideoCapture when opening a file. Add a separate function instead of an overload not to change the ABI. rename VideoCapture paramter 'device' to 'index' in CPP to reflect that it allows specifying the API. update comments to explain how to specify the API. --- modules/videoio/include/opencv2/videoio.hpp | 37 ++++++-- .../include/opencv2/videoio/videoio_c.h | 9 +- modules/videoio/src/cap.cpp | 90 +++++++++++-------- 3 files changed, 92 insertions(+), 44 deletions(-) diff --git a/modules/videoio/include/opencv2/videoio.hpp b/modules/videoio/include/opencv2/videoio.hpp index 6daccc543d..f37a8036c1 100644 --- a/modules/videoio/include/opencv2/videoio.hpp +++ b/modules/videoio/include/opencv2/videoio.hpp @@ -90,7 +90,10 @@ enum { CAP_ANY = 0, // autodetect CAP_INTELPERC = 1500, // Intel Perceptual Computing SDK CAP_OPENNI2 = 1600, // OpenNI2 (for Kinect) CAP_OPENNI2_ASUS = 1610, // OpenNI2 (for Asus Xtion and Occipital Structure sensors) - CAP_GPHOTO2 = 1700 // gPhoto2 connection + CAP_GPHOTO2 = 1700, // gPhoto2 connection + CAP_GSTREAMER = 1800, // GStreamer + CAP_FFMPEG = 1900, // FFMPEG + CAP_IMAGES = 2000 // OpenCV Image Sequence (e.g. img_%02d.jpg) }; // generic properties (based on DC1394 properties) @@ -398,10 +401,19 @@ public: CV_WRAP VideoCapture(const String& filename); /** @overload - @param device id of the opened video capturing device (i.e. a camera index). If there is a single - camera connected, just pass 0. + @param filename name of the opened video file (eg. video.avi) or image sequence (eg. + img_%02d.jpg, which will read samples like img_00.jpg, img_01.jpg, img_02.jpg, ...) + + @param apiPreference preferred Capture API to use. Can be used to enforce a specific reader + implementation if multiple are available: e.g. CAP_FFMPEG or CAP_IMAGES */ - CV_WRAP VideoCapture(int device); + CV_WRAP VideoCapture(const String& filename, int apiPreference); + + /** @overload + @param index = camera_id + domain_offset (CAP_*). id of the video capturing device to open. If there is a single + camera connected, just pass 0. Advanced Usage: to open Camera 1 using the MS Media Foundation API: index = 1 + CAP_MSMF + */ + CV_WRAP VideoCapture(int index); virtual ~VideoCapture(); @@ -415,9 +427,10 @@ public: CV_WRAP virtual bool open(const String& filename); /** @overload - @param device id of the opened video capturing device (i.e. a camera index). + @param index = camera_id + domain_offset (CAP_*). id of the video capturing device to open. If there is a single + camera connected, just pass 0. Advanced Usage: to open Camera 1 using the MS Media Foundation API: index = 1 + CAP_MSMF */ - CV_WRAP virtual bool open(int device); + CV_WRAP virtual bool open(int index); /** @brief Returns true if video capturing has been initialized already. @@ -541,6 +554,18 @@ public: */ CV_WRAP virtual double get(int propId) const; + /** @overload + + @param filename name of the opened video file (eg. video.avi) or image sequence (eg. + img_%02d.jpg, which will read samples like img_00.jpg, img_01.jpg, img_02.jpg, ...) + + @param apiPreference preferred Capture API to use. Can be used to enforce a specific reader + implementation if multiple are available: e.g. CAP_FFMPEG or CAP_IMAGES + + The methods first call VideoCapture::release to close the already opened file or camera. + */ + CV_WRAP virtual bool open(const String& filename, int apiPreference); + protected: Ptr cap; Ptr icap; diff --git a/modules/videoio/include/opencv2/videoio/videoio_c.h b/modules/videoio/include/opencv2/videoio/videoio_c.h index b8973850cf..0365b9223a 100644 --- a/modules/videoio/include/opencv2/videoio/videoio_c.h +++ b/modules/videoio/include/opencv2/videoio/videoio_c.h @@ -63,6 +63,9 @@ typedef struct CvCapture CvCapture; /* start capturing frames from video file */ CVAPI(CvCapture*) cvCreateFileCapture( const char* filename ); +/* start capturing frames from video file. allows specifying a preferred API to use */ +CVAPI(CvCapture*) cvCreateFileCaptureWithPreference( const char* filename , int apiPreference); + enum { CV_CAP_ANY =0, // autodetect @@ -111,8 +114,10 @@ enum CV_CAP_INTELPERC = 1500, // Intel Perceptual Computing CV_CAP_OPENNI2 = 1600, // OpenNI2 (for Kinect) - - CV_CAP_GPHOTO2 = 1700 + CV_CAP_GPHOTO2 = 1700, + CV_CAP_GSTREAMER = 1800, // GStreamer + CV_CAP_FFMPEG = 1900, // FFMPEG + CV_CAP_IMAGES = 2000 // OpenCV Image Sequence (e.g. img_%02d.jpg) }; /* start capturing frames from camera: index = camera_index + domain_offset (CV_CAP_*) */ diff --git a/modules/videoio/src/cap.cpp b/modules/videoio/src/cap.cpp index f2b5a4a305..c222f0edd9 100644 --- a/modules/videoio/src/cap.cpp +++ b/modules/videoio/src/cap.cpp @@ -359,56 +359,64 @@ CV_IMPL CvCapture * cvCreateCameraCapture (int index) * Videoreader dispatching method: it tries to find the first * API that can access a given filename. */ -CV_IMPL CvCapture * cvCreateFileCapture (const char * filename) +CV_IMPL CvCapture * cvCreateFileCaptureWithPreference (const char * filename, int apiPreference) { CvCapture * result = 0; + switch(apiPreference) { + default: + case CV_CAP_FFMPEG: #ifdef HAVE_FFMPEG - if (! result) - result = cvCreateFileCapture_FFMPEG_proxy (filename); + if (! result) + result = cvCreateFileCapture_FFMPEG_proxy (filename); #endif - + case CV_CAP_VFW: #ifdef HAVE_VFW - if (! result) - result = cvCreateFileCapture_VFW (filename); + if (! result) + result = cvCreateFileCapture_VFW (filename); #endif - + case CV_CAP_MSMF: #ifdef HAVE_MSMF - if (! result) - result = cvCreateFileCapture_MSMF (filename); + if (! result) + result = cvCreateFileCapture_MSMF (filename); #endif - #ifdef HAVE_XINE - if (! result) - result = cvCreateFileCapture_XINE (filename); + if (! result) + result = cvCreateFileCapture_XINE (filename); #endif - + case CV_CAP_GSTREAMER: #ifdef HAVE_GSTREAMER - if (! result) - result = cvCreateCapture_GStreamer (CV_CAP_GSTREAMER_FILE, filename); + if (! result) + result = cvCreateCapture_GStreamer (CV_CAP_GSTREAMER_FILE, filename); #endif - + case CV_CAP_QT: #if defined(HAVE_QUICKTIME) || defined(HAVE_QTKIT) - if (! result) - result = cvCreateFileCapture_QT (filename); + if (! result) + result = cvCreateFileCapture_QT (filename); #endif - + case CV_CAP_AVFOUNDATION: #ifdef HAVE_AVFOUNDATION - if (! result) - result = cvCreateFileCapture_AVFoundation (filename); + if (! result) + result = cvCreateFileCapture_AVFoundation (filename); #endif - + case CV_CAP_OPENNI: #ifdef HAVE_OPENNI - if (! result) - result = cvCreateFileCapture_OpenNI (filename); + if (! result) + result = cvCreateFileCapture_OpenNI (filename); #endif - - if (! result) - result = cvCreateFileCapture_Images (filename); + case CV_CAP_IMAGES: + if (! result) + result = cvCreateFileCapture_Images (filename); + } return result; } +CV_IMPL CvCapture * cvCreateFileCapture (const char * filename) +{ + return cvCreateFileCaptureWithPreference(filename, CV_CAP_ANY); +} + /** * Videowriter dispatching method: it tries to find the first * API that can write a given stream. @@ -615,14 +623,19 @@ static Ptr IVideoWriter_create(const String& filename, int _fourcc VideoCapture::VideoCapture() {} -VideoCapture::VideoCapture(const String& filename) +VideoCapture::VideoCapture(const String& filename, int apiPreference) { - open(filename); + open(filename, apiPreference); } -VideoCapture::VideoCapture(int device) +VideoCapture::VideoCapture(const String& filename) { - open(device); + open(filename, CAP_ANY); +} + +VideoCapture::VideoCapture(int index) +{ + open(index); } VideoCapture::~VideoCapture() @@ -631,24 +644,29 @@ VideoCapture::~VideoCapture() cap.release(); } -bool VideoCapture::open(const String& filename) +bool VideoCapture::open(const String& filename, int apiPreference) { if (isOpened()) release(); icap = IVideoCapture_create(filename); if (!icap.empty()) return true; - cap.reset(cvCreateFileCapture(filename.c_str())); + cap.reset(cvCreateFileCaptureWithPreference(filename.c_str(), apiPreference)); return isOpened(); } -bool VideoCapture::open(int device) +bool VideoCapture::open(const String& filename) +{ + return open(filename, CAP_ANY); +} + +bool VideoCapture::open(int index) { if (isOpened()) release(); - icap = IVideoCapture_create(device); + icap = IVideoCapture_create(index); if (!icap.empty()) return true; - cap.reset(cvCreateCameraCapture(device)); + cap.reset(cvCreateCameraCapture(index)); return isOpened(); }