45 #include <visp3/core/vpDebug.h> 46 #include <visp3/core/vpIoTools.h> 47 #include <visp3/io/vpVideoReader.h> 59 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
60 m_capture(), m_frame(), m_lastframe_unknown(false),
62 m_formatType(FORMAT_UNKNOWN), m_fileName(), m_initFileName(false), m_isOpen(false), m_frameCount(0), m_firstFrame(0), m_lastFrame(0),
63 m_firstFrameIndexIsSet(false), m_lastFrameIndexIsSet(false), m_frameStep(1), m_frameRate(0.)
72 if (m_imSequence != NULL) {
94 if (filename.empty()) {
98 m_fileName = filename;
100 m_formatType = getFormat(m_fileName);
102 if (m_formatType == FORMAT_UNKNOWN) {
107 if (isImageExtensionSupported()) {
109 if (!checkImageNameFormat(format)) {
114 m_initFileName =
true;
120 void vpVideoReader::getProperties()
126 if (!m_initFileName) {
130 if (isImageExtensionSupported()) {
133 m_imSequence->
setStep(m_frameStep);
134 if (m_firstFrameIndexIsSet) {
138 }
else if (isVideoExtensionSupported()) {
139 #if VISP_HAVE_OPENCV_VERSION >= 0x020100 140 m_capture.open(m_fileName.c_str());
142 if (!m_capture.isOpened()) {
145 #if VISP_HAVE_OPENCV_VERSION >= 0x030000 146 width = (
unsigned int)m_capture.get(cv::CAP_PROP_FRAME_WIDTH);
147 height = (
unsigned int)m_capture.get(cv::CAP_PROP_FRAME_HEIGHT);
148 m_frameRate = (double)m_capture.get(cv::CAP_PROP_FPS);
150 width = (
unsigned int)m_capture.get(CV_CAP_PROP_FRAME_WIDTH);
151 height = (
unsigned int)m_capture.get(CV_CAP_PROP_FRAME_HEIGHT);
152 m_frameRate = m_capture.get(CV_CAP_PROP_FPS);
157 "3rd >= 2.1.0 party libraries."));
159 }
else if (m_formatType == FORMAT_UNKNOWN) {
163 "not correspond to a readable " 164 "format supported by ViSP."));
167 findFirstFrameIndex();
169 findLastFrameIndex();
183 m_frameCount = m_firstFrame;
190 m_frameCount = m_firstFrame;
192 if (isVideoExtensionSupported()) {
193 #if VISP_HAVE_OPENCV_VERSION >= 0x020100 195 #if VISP_HAVE_OPENCV_VERSION >= 0x030000 196 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_firstFrame - 1);
198 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_firstFrame - 1);
216 m_frameCount = m_firstFrame;
223 m_frameCount = m_firstFrame;
225 if (isVideoExtensionSupported()) {
226 #if VISP_HAVE_OPENCV_VERSION >= 0x020100 228 #if VISP_HAVE_OPENCV_VERSION >= 0x030000 229 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_firstFrame - 1);
231 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_firstFrame - 1);
254 if (m_imSequence != NULL) {
255 m_imSequence->
setStep(m_frameStep);
258 if (m_frameCount + m_frameStep > m_lastFrame) {
260 }
else if (m_frameCount + m_frameStep < m_firstFrame) {
264 #if VISP_HAVE_OPENCV_VERSION >= 0x020100 266 m_capture >> m_frame;
267 if (m_frameStep == 1) {
270 #if VISP_HAVE_OPENCV_VERSION >= 0x030000 271 m_frameCount = (long)m_capture.get(cv::CAP_PROP_POS_FRAMES);
272 if (m_frameStep > 0) {
273 if (m_frameCount + m_frameStep <= m_lastFrame) {
274 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
276 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount - 1);
278 }
else if (m_frameStep < 0) {
279 if (m_frameCount + m_frameStep >= m_firstFrame) {
280 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
282 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_firstFrame - 1);
286 m_frameCount = (long)m_capture.get(CV_CAP_PROP_POS_FRAMES);
287 if (m_frameStep > 0) {
288 if (m_frameCount + m_frameStep <= m_lastFrame) {
289 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
291 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount - 1);
293 }
else if (m_frameStep < 0) {
294 if (m_frameCount + m_frameStep >= m_firstFrame) {
295 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
297 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_firstFrame - 1);
303 if (m_frame.empty()) {
304 std::cout <<
"Warning: Unable to decode image " << m_frameCount - m_frameStep << std::endl;
305 if (m_lastframe_unknown) {
331 if (m_imSequence != NULL) {
332 m_imSequence->
setStep(m_frameStep);
335 if (m_frameCount + m_frameStep > m_lastFrame) {
337 }
else if (m_frameCount + m_frameStep < m_firstFrame) {
341 #if VISP_HAVE_OPENCV_VERSION >= 0x020100 343 m_capture >> m_frame;
344 if (m_frameStep == 1) {
347 #if VISP_HAVE_OPENCV_VERSION >= 0x030000 348 m_frameCount = (long)m_capture.get(cv::CAP_PROP_POS_FRAMES);
349 if (m_frameStep > 0) {
350 if (m_frameCount + m_frameStep <= m_lastFrame) {
351 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
353 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount - 1);
355 }
else if (m_frameStep < 0) {
356 if (m_frameCount + m_frameStep >= m_firstFrame) {
357 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
359 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_firstFrame - 1);
363 m_frameCount = (long)m_capture.get(CV_CAP_PROP_POS_FRAMES);
364 if (m_frameStep > 0) {
365 if (m_frameCount + m_frameStep <= m_lastFrame) {
366 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
368 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount - 1);
370 }
else if (m_frameStep < 0) {
371 if (m_frameCount + m_frameStep >= m_firstFrame) {
372 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
374 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_firstFrame - 1);
380 if (m_frame.empty()) {
381 std::cout <<
"Warning: Unable to decode image " << m_frameCount - m_frameStep << std::endl;
405 if (m_imSequence != NULL) {
407 m_imSequence->
acquire(I, frame_index);
412 if (m_frameCount + m_frameStep > m_lastFrame) {
414 }
else if (m_frameCount + m_frameStep < m_firstFrame) {
422 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x030000) 423 if (!m_capture.set(cv::CAP_PROP_POS_FRAMES, frame_index)) {
424 vpERROR_TRACE(
"Couldn't find the %ld th frame", frame_index);
428 m_capture >> m_frame;
429 m_frameCount = frame_index + m_frameStep;
430 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount);
431 if (m_frame.empty()) {
433 m_capture >> m_frame;
434 if (m_frame.empty()) {
442 #elif defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100) 443 if (!m_capture.set(CV_CAP_PROP_POS_FRAMES, frame_index)) {
444 vpERROR_TRACE(
"Couldn't find the %ld th frame", frame_index);
448 m_capture >> m_frame;
449 m_frameCount = frame_index + m_frameStep;
450 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount);
475 if (m_imSequence != NULL) {
477 m_imSequence->
acquire(I, frame_index);
482 if (m_frameCount + m_frameStep > m_lastFrame) {
484 }
else if (m_frameCount + m_frameStep < m_firstFrame) {
492 #if VISP_HAVE_OPENCV_VERSION >= 0x030000 493 if (!m_capture.set(cv::CAP_PROP_POS_FRAMES, frame_index)) {
494 vpERROR_TRACE(
"Couldn't find the %ld th frame", frame_index);
497 m_capture >> m_frame;
498 if (m_frame.empty()) {
500 m_capture >> m_frame;
501 if (m_frame.empty()) {
510 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100 511 if (!m_capture.set(CV_CAP_PROP_POS_FRAMES, frame_index)) {
516 m_capture >> m_frame;
517 m_frameCount = (long)m_capture.get(CV_CAP_PROP_POS_FRAMES);
518 if (m_frameStep > 1) {
519 m_frameCount += m_frameStep - 1;
520 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount);
521 }
else if (m_frameStep < -1) {
522 m_frameCount += m_frameStep - 1;
523 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount);
539 vpVideoReader::vpVideoFormatType vpVideoReader::getFormat(
const std::string &filename)
const 541 std::string ext = vpVideoReader::getExtension(filename);
543 if (ext.compare(
".PGM") == 0)
545 else if (ext.compare(
".pgm") == 0)
547 else if (ext.compare(
".PPM") == 0)
549 else if (ext.compare(
".ppm") == 0)
551 else if (ext.compare(
".JPG") == 0)
553 else if (ext.compare(
".jpg") == 0)
555 else if (ext.compare(
".JPEG") == 0)
557 else if (ext.compare(
".jpeg") == 0)
559 else if (ext.compare(
".PNG") == 0)
561 else if (ext.compare(
".png") == 0)
563 else if (ext.compare(
".TIFF") == 0)
565 else if (ext.compare(
".tiff") == 0)
567 else if (ext.compare(
".BMP") == 0)
569 else if (ext.compare(
".bmp") == 0)
571 else if (ext.compare(
".DIB") == 0)
573 else if (ext.compare(
".dib") == 0)
575 else if (ext.compare(
".PBM") == 0)
577 else if (ext.compare(
".pbm") == 0)
579 else if (ext.compare(
".SR") == 0)
581 else if (ext.compare(
".sr") == 0)
583 else if (ext.compare(
".RAS") == 0)
584 return FORMAT_RASTER;
585 else if (ext.compare(
".ras") == 0)
586 return FORMAT_RASTER;
587 else if (ext.compare(
".JP2") == 0)
588 return FORMAT_JPEG2000;
589 else if (ext.compare(
".jp2") == 0)
590 return FORMAT_JPEG2000;
591 else if (ext.compare(
".AVI") == 0)
593 else if (ext.compare(
".avi") == 0)
595 else if (ext.compare(
".MPEG") == 0)
597 else if (ext.compare(
".mpeg") == 0)
599 else if (ext.compare(
".MPG") == 0)
601 else if (ext.compare(
".mpg") == 0)
603 else if (ext.compare(
".MPEG4") == 0)
605 else if (ext.compare(
".mpeg4") == 0)
607 else if (ext.compare(
".MP4") == 0)
609 else if (ext.compare(
".mp4") == 0)
611 else if (ext.compare(
".MOV") == 0)
613 else if (ext.compare(
".mov") == 0)
615 else if (ext.compare(
".OGV") == 0)
617 else if (ext.compare(
".ogv") == 0)
619 else if (ext.compare(
".WMV") == 0)
621 else if (ext.compare(
".wmv") == 0)
623 else if (ext.compare(
".FLV") == 0)
625 else if (ext.compare(
".flv") == 0)
627 else if (ext.compare(
".MKV") == 0)
629 else if (ext.compare(
".mkv") == 0)
631 else if (ext.compare(
".MTS") == 0)
633 else if (ext.compare(
".mts") == 0)
636 return FORMAT_UNKNOWN;
640 std::string vpVideoReader::getExtension(
const std::string &filename)
643 size_t dot = filename.find_last_of(
".");
644 std::string ext = filename.substr(dot, filename.size() - 1);
651 void vpVideoReader::findLastFrameIndex()
658 if (m_imSequence != NULL) {
659 if (!m_lastFrameIndexIsSet) {
667 for (
size_t i = 0; i < files.size(); i++) {
670 long imageIndex = extractImageIndex(files[i], imageNameFormat);
671 if ((imageIndex != -1) && (imageIndex > m_lastFrame)) {
672 m_lastFrame = imageIndex;
678 #if VISP_HAVE_OPENCV_VERSION >= 0x030000 679 else if (!m_lastFrameIndexIsSet) {
680 m_lastFrame = (long)m_capture.get(cv::CAP_PROP_FRAME_COUNT);
681 if (m_lastFrame <= 2) {
685 m_lastframe_unknown =
true;
686 m_lastFrame = 100000;
689 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100 690 else if (!m_lastFrameIndexIsSet) {
691 m_lastFrame = (long)m_capture.get(CV_CAP_PROP_FRAME_COUNT);
692 if (m_lastFrame <= 2) {
696 m_lastframe_unknown =
true;
697 m_lastFrame = 100000;
706 void vpVideoReader::findFirstFrameIndex()
708 if (m_imSequence != NULL) {
709 if (!m_firstFrameIndexIsSet) {
717 for (
size_t i = 0; i < files.size(); i++) {
720 long imageIndex = extractImageIndex(files[i], imageNameFormat);
721 if ((imageIndex != -1) && (imageIndex < m_firstFrame || m_firstFrame == -1)) {
722 m_firstFrame = imageIndex;
728 #if VISP_HAVE_OPENCV_VERSION >= 0x020100 729 else if (!m_firstFrameIndexIsSet) {
738 bool vpVideoReader::isImageExtensionSupported()
const 740 return (m_formatType == FORMAT_PGM || m_formatType == FORMAT_PPM || m_formatType == FORMAT_JPEG ||
741 m_formatType == FORMAT_PNG || m_formatType == FORMAT_TIFF || m_formatType == FORMAT_BMP ||
742 m_formatType == FORMAT_DIB || m_formatType == FORMAT_PBM || m_formatType == FORMAT_RASTER ||
743 m_formatType == FORMAT_JPEG2000);
749 bool vpVideoReader::isVideoExtensionSupported()
const 751 return (m_formatType == FORMAT_AVI || m_formatType == FORMAT_MPEG || m_formatType == FORMAT_MPEG4 ||
752 m_formatType == FORMAT_MOV || m_formatType == FORMAT_OGV || m_formatType == FORMAT_WMV ||
753 m_formatType == FORMAT_FLV || m_formatType == FORMAT_MKV || m_formatType == FORMAT_MTS);
827 long vpVideoReader::extractImageIndex(
const std::string &imageName,
const std::string &format)
const 829 size_t indexBegin = format.find_last_of(
'%');
830 size_t indexEnd = format.find_first_of(
'd', indexBegin);
831 size_t suffixLength = format.length() - indexEnd - 1;
834 if (imageName.length() <= suffixLength + indexBegin) {
837 size_t indexLength = imageName.length() - suffixLength - indexBegin;
838 std::string indexSubstr = imageName.substr(indexBegin, indexLength);
839 std::istringstream ss(indexSubstr);
842 if (ss.fail() || index < 0 || !ss.eof()) {
847 char nameByFormat[FILENAME_MAX];
848 sprintf(nameByFormat, format.c_str(), index);
849 if (std::string(nameByFormat) != imageName) {
859 bool vpVideoReader::checkImageNameFormat(
const std::string &format)
const 861 size_t indexBegin = format.find_last_of(
'%');
862 size_t indexEnd = format.find_first_of(
'd', indexBegin);
863 if (indexBegin == std::string::npos || indexEnd == std::string::npos) {
866 for (
size_t i = indexBegin + 1; i < indexEnd; i++) {
867 if (!std::isdigit(format[i])) {
884 switch (m_formatType) {
Used to indicate that a value is not in the allowed range.
void setGenericName(const std::string &genericName)
unsigned int getWidth() const
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
error that can be emited by ViSP classes.
Error that can be emited by the vpImage class and its derivates.
void open(vpImage< vpRGBa > &I)
bool isVideoFormat() const
bool getFrame(vpImage< vpRGBa > &I, long frame)
unsigned int height
Number of rows in the image.
void acquire(vpImage< vpRGBa > &I)
void setImageNumber(long number)
vpVideoReader & operator>>(vpImage< unsigned char > &I)
Class to grab (ie. read) images from the disk.
Base class for all video devices. It is designed to provide a front end to video sources.
void setLastFrameIndex(const long last_frame)
Used to indicate that a parameter is not initialized.
void setFileName(const std::string &filename)
unsigned int getHeight() const
void acquire(vpImage< unsigned char > &I)
unsigned int width
Number of columns in the image.