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()
122 if (!m_initFileName) {
126 if (isImageExtensionSupported()) {
129 m_imSequence->
setStep(m_frameStep);
130 if (m_firstFrameIndexIsSet) {
134 }
else if (isVideoExtensionSupported()) {
135 #if VISP_HAVE_OPENCV_VERSION >= 0x020100 136 m_capture.open(m_fileName.c_str());
138 if (!m_capture.isOpened()) {
141 #if VISP_HAVE_OPENCV_VERSION >= 0x030000 142 width = (
unsigned int)m_capture.get(cv::CAP_PROP_FRAME_WIDTH);
143 height = (
unsigned int)m_capture.get(cv::CAP_PROP_FRAME_HEIGHT);
144 m_frameRate = (double)m_capture.get(cv::CAP_PROP_FPS);
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 = m_capture.get(CV_CAP_PROP_FPS);
153 "3rd >= 2.1.0 party libraries."));
155 }
else if (m_formatType == FORMAT_UNKNOWN) {
159 "not correspond to a readable " 160 "format supported by ViSP."));
163 findFirstFrameIndex();
165 findLastFrameIndex();
179 m_frameCount = m_firstFrame;
186 m_frameCount = m_firstFrame;
188 if (isVideoExtensionSupported()) {
189 #if VISP_HAVE_OPENCV_VERSION >= 0x020100 191 #if VISP_HAVE_OPENCV_VERSION >= 0x030000 192 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_firstFrame - 1);
194 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_firstFrame - 1);
212 m_frameCount = m_firstFrame;
219 m_frameCount = m_firstFrame;
221 if (isVideoExtensionSupported()) {
222 #if VISP_HAVE_OPENCV_VERSION >= 0x020100 224 #if VISP_HAVE_OPENCV_VERSION >= 0x030000 225 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_firstFrame - 1);
227 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_firstFrame - 1);
250 if (m_imSequence != NULL) {
251 m_imSequence->
setStep(m_frameStep);
254 if (m_frameCount + m_frameStep > m_lastFrame) {
256 }
else if (m_frameCount + m_frameStep < m_firstFrame) {
260 #if VISP_HAVE_OPENCV_VERSION >= 0x020100 262 m_capture >> m_frame;
263 if (m_frameStep == 1) {
266 #if VISP_HAVE_OPENCV_VERSION >= 0x030000 267 m_frameCount = (long)m_capture.get(cv::CAP_PROP_POS_FRAMES);
268 if (m_frameStep > 0) {
269 if (m_frameCount + m_frameStep <= m_lastFrame) {
270 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
272 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount - 1);
274 }
else if (m_frameStep < 0) {
275 if (m_frameCount + m_frameStep >= m_firstFrame) {
276 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
278 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_firstFrame - 1);
282 m_frameCount = (long)m_capture.get(CV_CAP_PROP_POS_FRAMES);
283 if (m_frameStep > 0) {
284 if (m_frameCount + m_frameStep <= m_lastFrame) {
285 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
287 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount - 1);
289 }
else if (m_frameStep < 0) {
290 if (m_frameCount + m_frameStep >= m_firstFrame) {
291 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
293 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_firstFrame - 1);
299 if (m_frame.empty()) {
300 std::cout <<
"Warning: Unable to decode image " << m_frameCount - m_frameStep << std::endl;
301 if (m_lastframe_unknown) {
327 if (m_imSequence != NULL) {
328 m_imSequence->
setStep(m_frameStep);
331 if (m_frameCount + m_frameStep > m_lastFrame) {
333 }
else if (m_frameCount + m_frameStep < m_firstFrame) {
337 #if VISP_HAVE_OPENCV_VERSION >= 0x020100 339 m_capture >> m_frame;
340 if (m_frameStep == 1) {
343 #if VISP_HAVE_OPENCV_VERSION >= 0x030000 344 m_frameCount = (long)m_capture.get(cv::CAP_PROP_POS_FRAMES);
345 if (m_frameStep > 0) {
346 if (m_frameCount + m_frameStep <= m_lastFrame) {
347 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
349 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount - 1);
351 }
else if (m_frameStep < 0) {
352 if (m_frameCount + m_frameStep >= m_firstFrame) {
353 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
355 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_firstFrame - 1);
359 m_frameCount = (long)m_capture.get(CV_CAP_PROP_POS_FRAMES);
360 if (m_frameStep > 0) {
361 if (m_frameCount + m_frameStep <= m_lastFrame) {
362 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
364 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount - 1);
366 }
else if (m_frameStep < 0) {
367 if (m_frameCount + m_frameStep >= m_firstFrame) {
368 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
370 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_firstFrame - 1);
376 if (m_frame.empty()) {
377 std::cout <<
"Warning: Unable to decode image " << m_frameCount - m_frameStep << std::endl;
401 if (m_imSequence != NULL) {
403 m_imSequence->
acquire(I, frame_index);
408 if (m_frameCount + m_frameStep > m_lastFrame) {
410 }
else if (m_frameCount + m_frameStep < m_firstFrame) {
418 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x030000) 419 if (!m_capture.set(cv::CAP_PROP_POS_FRAMES, frame_index)) {
420 vpERROR_TRACE(
"Couldn't find the %ld th frame", frame_index);
424 m_capture >> m_frame;
425 m_frameCount = frame_index + m_frameStep;
426 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount);
427 if (m_frame.empty()) {
429 m_capture >> m_frame;
430 if (m_frame.empty()) {
438 #elif defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100) 439 if (!m_capture.set(CV_CAP_PROP_POS_FRAMES, frame_index)) {
440 vpERROR_TRACE(
"Couldn't find the %ld th frame", frame_index);
444 m_capture >> m_frame;
445 m_frameCount = frame_index + m_frameStep;
446 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount);
471 if (m_imSequence != NULL) {
473 m_imSequence->
acquire(I, frame_index);
478 if (m_frameCount + m_frameStep > m_lastFrame) {
480 }
else if (m_frameCount + m_frameStep < m_firstFrame) {
488 #if VISP_HAVE_OPENCV_VERSION >= 0x030000 489 if (!m_capture.set(cv::CAP_PROP_POS_FRAMES, frame_index)) {
490 vpERROR_TRACE(
"Couldn't find the %ld th frame", frame_index);
493 m_capture >> m_frame;
494 if (m_frame.empty()) {
496 m_capture >> m_frame;
497 if (m_frame.empty()) {
506 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100 507 if (!m_capture.set(CV_CAP_PROP_POS_FRAMES, frame_index)) {
512 m_capture >> m_frame;
513 m_frameCount = (long)m_capture.get(CV_CAP_PROP_POS_FRAMES);
514 if (m_frameStep > 1) {
515 m_frameCount += m_frameStep - 1;
516 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount);
517 }
else if (m_frameStep < -1) {
518 m_frameCount += m_frameStep - 1;
519 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount);
535 vpVideoReader::vpVideoFormatType vpVideoReader::getFormat(
const std::string &filename)
const 537 std::string ext = vpVideoReader::getExtension(filename);
539 if (ext.compare(
".PGM") == 0)
541 else if (ext.compare(
".pgm") == 0)
543 else if (ext.compare(
".PPM") == 0)
545 else if (ext.compare(
".ppm") == 0)
547 else if (ext.compare(
".JPG") == 0)
549 else if (ext.compare(
".jpg") == 0)
551 else if (ext.compare(
".JPEG") == 0)
553 else if (ext.compare(
".jpeg") == 0)
555 else if (ext.compare(
".PNG") == 0)
557 else if (ext.compare(
".png") == 0)
559 else if (ext.compare(
".TIFF") == 0)
561 else if (ext.compare(
".tiff") == 0)
563 else if (ext.compare(
".BMP") == 0)
565 else if (ext.compare(
".bmp") == 0)
567 else if (ext.compare(
".DIB") == 0)
569 else if (ext.compare(
".dib") == 0)
571 else if (ext.compare(
".PBM") == 0)
573 else if (ext.compare(
".pbm") == 0)
575 else if (ext.compare(
".SR") == 0)
577 else if (ext.compare(
".sr") == 0)
579 else if (ext.compare(
".RAS") == 0)
580 return FORMAT_RASTER;
581 else if (ext.compare(
".ras") == 0)
582 return FORMAT_RASTER;
583 else if (ext.compare(
".JP2") == 0)
584 return FORMAT_JPEG2000;
585 else if (ext.compare(
".jp2") == 0)
586 return FORMAT_JPEG2000;
587 else if (ext.compare(
".AVI") == 0)
589 else if (ext.compare(
".avi") == 0)
591 else if (ext.compare(
".MPEG") == 0)
593 else if (ext.compare(
".mpeg") == 0)
595 else if (ext.compare(
".MPG") == 0)
597 else if (ext.compare(
".mpg") == 0)
599 else if (ext.compare(
".MPEG4") == 0)
601 else if (ext.compare(
".mpeg4") == 0)
603 else if (ext.compare(
".MP4") == 0)
605 else if (ext.compare(
".mp4") == 0)
607 else if (ext.compare(
".MOV") == 0)
609 else if (ext.compare(
".mov") == 0)
611 else if (ext.compare(
".OGV") == 0)
613 else if (ext.compare(
".ogv") == 0)
615 else if (ext.compare(
".WMV") == 0)
617 else if (ext.compare(
".wmv") == 0)
619 else if (ext.compare(
".FLV") == 0)
621 else if (ext.compare(
".flv") == 0)
623 else if (ext.compare(
".MKV") == 0)
625 else if (ext.compare(
".mkv") == 0)
627 else if (ext.compare(
".MTS") == 0)
629 else if (ext.compare(
".mts") == 0)
632 return FORMAT_UNKNOWN;
636 std::string vpVideoReader::getExtension(
const std::string &filename)
639 size_t dot = filename.find_last_of(
".");
640 std::string ext = filename.substr(dot, filename.size() - 1);
647 void vpVideoReader::findLastFrameIndex()
654 if (m_imSequence != NULL) {
655 if (!m_lastFrameIndexIsSet) {
663 for (
size_t i = 0; i < files.size(); i++) {
666 long imageIndex = extractImageIndex(files[i], imageNameFormat);
667 if ((imageIndex != -1) && (imageIndex > m_lastFrame)) {
668 m_lastFrame = imageIndex;
674 #if VISP_HAVE_OPENCV_VERSION >= 0x030000 675 else if (!m_lastFrameIndexIsSet) {
676 m_lastFrame = (long)m_capture.get(cv::CAP_PROP_FRAME_COUNT);
677 if (m_lastFrame <= 2) {
681 m_lastframe_unknown =
true;
682 m_lastFrame = 100000;
685 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100 686 else if (!m_lastFrameIndexIsSet) {
687 m_lastFrame = (long)m_capture.get(CV_CAP_PROP_FRAME_COUNT);
688 if (m_lastFrame <= 2) {
692 m_lastframe_unknown =
true;
693 m_lastFrame = 100000;
702 void vpVideoReader::findFirstFrameIndex()
704 if (m_imSequence != NULL) {
705 if (!m_firstFrameIndexIsSet) {
713 for (
size_t i = 0; i < files.size(); i++) {
716 long imageIndex = extractImageIndex(files[i], imageNameFormat);
717 if ((imageIndex != -1) && (imageIndex < m_firstFrame || m_firstFrame == -1)) {
718 m_firstFrame = imageIndex;
724 #if VISP_HAVE_OPENCV_VERSION >= 0x020100 725 else if (!m_firstFrameIndexIsSet) {
734 bool vpVideoReader::isImageExtensionSupported()
const 736 return (m_formatType == FORMAT_PGM || m_formatType == FORMAT_PPM || m_formatType == FORMAT_JPEG ||
737 m_formatType == FORMAT_PNG || m_formatType == FORMAT_TIFF || m_formatType == FORMAT_BMP ||
738 m_formatType == FORMAT_DIB || m_formatType == FORMAT_PBM || m_formatType == FORMAT_RASTER ||
739 m_formatType == FORMAT_JPEG2000);
745 bool vpVideoReader::isVideoExtensionSupported()
const 747 return (m_formatType == FORMAT_AVI || m_formatType == FORMAT_MPEG || m_formatType == FORMAT_MPEG4 ||
748 m_formatType == FORMAT_MOV || m_formatType == FORMAT_OGV || m_formatType == FORMAT_WMV ||
749 m_formatType == FORMAT_FLV || m_formatType == FORMAT_MKV || m_formatType == FORMAT_MTS);
823 long vpVideoReader::extractImageIndex(
const std::string &imageName,
const std::string &format)
const 825 size_t indexBegin = format.find_last_of(
'%');
826 size_t indexEnd = format.find_first_of(
'd', indexBegin);
827 size_t suffixLength = format.length() - indexEnd - 1;
830 if (imageName.length() <= suffixLength + indexBegin) {
833 size_t indexLength = imageName.length() - suffixLength - indexBegin;
834 std::string indexSubstr = imageName.substr(indexBegin, indexLength);
835 std::istringstream ss(indexSubstr);
838 if (ss.fail() || index < 0 || !ss.eof()) {
843 char nameByFormat[FILENAME_MAX];
844 sprintf(nameByFormat, format.c_str(), index);
845 if (std::string(nameByFormat) != imageName) {
855 bool vpVideoReader::checkImageNameFormat(
const std::string &format)
const 857 size_t indexBegin = format.find_last_of(
'%');
858 size_t indexEnd = format.find_first_of(
'd', indexBegin);
859 if (indexBegin == std::string::npos || indexEnd == std::string::npos) {
862 for (
size_t i = indexBegin + 1; i < indexEnd; i++) {
863 if (!std::isdigit(format[i])) {
Used to indicate that a value is not in the allowed range.
void setGenericName(const std::string &genericName)
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 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)
unsigned int getHeight() const
Used to indicate that a parameter is not initialized.
void setFileName(const std::string &filename)
void acquire(vpImage< unsigned char > &I)
unsigned int getWidth() const
unsigned int width
Number of columns in the image.