From 9d78a1ea9fdf38ccaa1084c7fd837ca0eba3b1e7 Mon Sep 17 00:00:00 2001 From: Ilya Lavrenov Date: Mon, 26 Oct 2015 14:37:38 +0300 Subject: [PATCH] allow to retrieve videocapture properties before first frame reading --- modules/highgui/src/cap_images.cpp | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/modules/highgui/src/cap_images.cpp b/modules/highgui/src/cap_images.cpp index e1a8b8b1c..f6abc3a10 100644 --- a/modules/highgui/src/cap_images.cpp +++ b/modules/highgui/src/cap_images.cpp @@ -67,10 +67,11 @@ class CvCapture_Images : public CvCapture public: CvCapture_Images() { - filename = 0; + filename = NULL; currentframe = firstframe = 0; length = 0; - frame = 0; + frame = NULL; + grabbedInOpen = false; } virtual ~CvCapture_Images() @@ -92,6 +93,7 @@ protected: unsigned length; // length of sequence IplImage* frame; + bool grabbedInOpen; }; @@ -100,7 +102,7 @@ void CvCapture_Images::close() if( filename ) { free(filename); - filename = 0; + filename = NULL; } currentframe = firstframe = 0; length = 0; @@ -113,6 +115,14 @@ bool CvCapture_Images::grabFrame() char str[_MAX_PATH]; sprintf(str, filename, firstframe + currentframe); + if (grabbedInOpen) + { + grabbedInOpen = false; + ++currentframe; + + return true; + } + cvReleaseImage(&frame); frame = cvLoadImage(str, CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR); if( frame ) @@ -123,7 +133,7 @@ bool CvCapture_Images::grabFrame() IplImage* CvCapture_Images::retrieveFrame(int) { - return frame; + return grabbedInOpen ? NULL : frame; } double CvCapture_Images::getProperty(int id) @@ -166,6 +176,8 @@ bool CvCapture_Images::setProperty(int id, double value) value = length - 1; } currentframe = cvRound(value); + if (currentframe != 0) + grabbedInOpen = false; // grabbed frame is not valid anymore return true; case CV_CAP_PROP_POS_AVI_RATIO: if(value > 1) { @@ -176,6 +188,8 @@ bool CvCapture_Images::setProperty(int id, double value) value = 0; } currentframe = cvRound((length - 1) * value); + if (currentframe != 0) + grabbedInOpen = false; // grabbed frame is not valid anymore return true; } CV_WARN("unknown/unhandled property\n"); @@ -278,6 +292,12 @@ bool CvCapture_Images::open(const char * _filename) } firstframe = offset; + + // grab frame to enable properties retrieval + grabFrame(); + grabbedInOpen = true; + currentframe = 0; + return true; } @@ -290,7 +310,7 @@ CvCapture* cvCreateFileCapture_Images(const char * filename) return capture; delete capture; - return 0; + return NULL; } //