39 #include <visp3/core/vpDebug.h>
40 #include <visp3/core/vpIoTools.h>
41 #include <visp3/io/vpVideoReader.h>
53 #if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
54 m_capture(), m_frame(), m_lastframe_unknown(false),
56 m_formatType(FORMAT_UNKNOWN), m_videoName(), m_frameName(), m_initFileName(false), m_isOpen(false), m_frameCount(0),
57 m_firstFrame(0), m_lastFrame(0), m_firstFrameIndexIsSet(false), m_lastFrameIndexIsSet(false), m_frameStep(1),
67 if (m_imSequence != NULL) {
89 if (filename.empty()) {
93 m_videoName = filename;
94 m_frameName = filename;
96 m_formatType = getFormat(filename);
98 if (m_formatType == FORMAT_UNKNOWN) {
103 if (isImageExtensionSupported()) {
105 if (!checkImageNameFormat(format)) {
110 m_initFileName =
true;
116 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 defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
136 m_capture.open(m_videoName.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 defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
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 defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
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);
249 if (m_imSequence != NULL) {
250 m_imSequence->
setStep(m_frameStep);
251 bool skip_frame =
false;
259 }
while (skip_frame && m_imSequence->
getImageNumber() < m_lastFrame);
262 if (m_frameCount + m_frameStep > m_lastFrame) {
264 }
else if (m_frameCount + m_frameStep < m_firstFrame) {
268 #if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
270 m_capture >> m_frame;
271 if (m_frameStep == 1) {
274 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
275 m_frameCount = (long)m_capture.get(cv::CAP_PROP_POS_FRAMES);
276 if (m_frameStep > 0) {
277 if (m_frameCount + m_frameStep <= m_lastFrame) {
278 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
280 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount - 1);
282 }
else if (m_frameStep < 0) {
283 if (m_frameCount + m_frameStep >= m_firstFrame) {
284 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
286 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_firstFrame - 1);
290 m_frameCount = (long)m_capture.get(CV_CAP_PROP_POS_FRAMES);
291 if (m_frameStep > 0) {
292 if (m_frameCount + m_frameStep <= m_lastFrame) {
293 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
295 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount - 1);
297 }
else if (m_frameStep < 0) {
298 if (m_frameCount + m_frameStep >= m_firstFrame) {
299 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
301 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_firstFrame - 1);
307 if (m_frame.empty()) {
308 std::cout <<
"Warning: Unable to decode image " << m_frameCount - m_frameStep << std::endl;
309 if (m_lastframe_unknown) {
334 if (m_imSequence != NULL) {
335 m_imSequence->
setStep(m_frameStep);
336 bool skip_frame =
false;
344 }
while (skip_frame && m_imSequence->
getImageNumber() < m_lastFrame);
347 if (m_frameCount + m_frameStep > m_lastFrame) {
349 }
else if (m_frameCount + m_frameStep < m_firstFrame) {
353 #if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
355 m_capture >> m_frame;
356 if (m_frameStep == 1) {
359 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
360 m_frameCount = (long)m_capture.get(cv::CAP_PROP_POS_FRAMES);
361 if (m_frameStep > 0) {
362 if (m_frameCount + m_frameStep <= m_lastFrame) {
363 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
365 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount - 1);
367 }
else if (m_frameStep < 0) {
368 if (m_frameCount + m_frameStep >= m_firstFrame) {
369 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
371 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_firstFrame - 1);
375 m_frameCount = (long)m_capture.get(CV_CAP_PROP_POS_FRAMES);
376 if (m_frameStep > 0) {
377 if (m_frameCount + m_frameStep <= m_lastFrame) {
378 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
380 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount - 1);
382 }
else if (m_frameStep < 0) {
383 if (m_frameCount + m_frameStep >= m_firstFrame) {
384 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
386 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_firstFrame - 1);
392 if (m_frame.empty()) {
393 std::cout <<
"Warning: Unable to decode image " << m_frameCount - m_frameStep << std::endl;
416 if (m_imSequence != NULL) {
418 m_imSequence->
acquire(I, frame_index);
423 if (m_frameCount + m_frameStep > m_lastFrame) {
425 }
else if (m_frameCount + m_frameStep < m_firstFrame) {
433 #if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
434 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
435 if (!m_capture.set(cv::CAP_PROP_POS_FRAMES, frame_index)) {
436 vpERROR_TRACE(
"Couldn't find the %ld th frame", frame_index);
440 m_capture >> m_frame;
441 m_frameCount = frame_index + m_frameStep;
442 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount);
443 if (m_frame.empty()) {
445 m_capture >> m_frame;
446 if (m_frame.empty()) {
455 if (!m_capture.set(CV_CAP_PROP_POS_FRAMES, frame_index)) {
456 vpERROR_TRACE(
"Couldn't find the %ld th frame", frame_index);
460 m_capture >> m_frame;
461 m_frameCount = frame_index + m_frameStep;
462 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount);
488 if (m_imSequence != NULL) {
490 m_imSequence->
acquire(I, frame_index);
495 if (m_frameCount + m_frameStep > m_lastFrame) {
497 }
else if (m_frameCount + m_frameStep < m_firstFrame) {
505 #if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
506 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
507 if (!m_capture.set(cv::CAP_PROP_POS_FRAMES, frame_index)) {
508 vpERROR_TRACE(
"Couldn't find the %ld th frame", frame_index);
511 m_capture >> m_frame;
512 if (m_frame.empty()) {
514 m_capture >> m_frame;
515 if (m_frame.empty()) {
525 if (!m_capture.set(CV_CAP_PROP_POS_FRAMES, frame_index)) {
530 m_capture >> m_frame;
531 m_frameCount = (long)m_capture.get(CV_CAP_PROP_POS_FRAMES);
532 if (m_frameStep > 1) {
533 m_frameCount += m_frameStep - 1;
534 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount);
535 }
else if (m_frameStep < -1) {
536 m_frameCount += m_frameStep - 1;
537 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount);
554 vpVideoReader::vpVideoFormatType vpVideoReader::getFormat(
const std::string &filename)
const
556 std::string ext = vpVideoReader::getExtension(filename);
558 if (ext.compare(
".PGM") == 0)
560 else if (ext.compare(
".pgm") == 0)
562 else if (ext.compare(
".PPM") == 0)
564 else if (ext.compare(
".ppm") == 0)
566 else if (ext.compare(
".JPG") == 0)
568 else if (ext.compare(
".jpg") == 0)
570 else if (ext.compare(
".JPEG") == 0)
572 else if (ext.compare(
".jpeg") == 0)
574 else if (ext.compare(
".PNG") == 0)
576 else if (ext.compare(
".png") == 0)
578 else if (ext.compare(
".TIFF") == 0)
580 else if (ext.compare(
".tiff") == 0)
582 else if (ext.compare(
".BMP") == 0)
584 else if (ext.compare(
".bmp") == 0)
586 else if (ext.compare(
".DIB") == 0)
588 else if (ext.compare(
".dib") == 0)
590 else if (ext.compare(
".PBM") == 0)
592 else if (ext.compare(
".pbm") == 0)
594 else if (ext.compare(
".SR") == 0)
596 else if (ext.compare(
".sr") == 0)
598 else if (ext.compare(
".RAS") == 0)
599 return FORMAT_RASTER;
600 else if (ext.compare(
".ras") == 0)
601 return FORMAT_RASTER;
602 else if (ext.compare(
".JP2") == 0)
603 return FORMAT_JPEG2000;
604 else if (ext.compare(
".jp2") == 0)
605 return FORMAT_JPEG2000;
606 else if (ext.compare(
".AVI") == 0)
608 else if (ext.compare(
".avi") == 0)
610 else if (ext.compare(
".MPEG") == 0)
612 else if (ext.compare(
".mpeg") == 0)
614 else if (ext.compare(
".MPG") == 0)
616 else if (ext.compare(
".mpg") == 0)
618 else if (ext.compare(
".MPEG4") == 0)
620 else if (ext.compare(
".mpeg4") == 0)
622 else if (ext.compare(
".MP4") == 0)
624 else if (ext.compare(
".mp4") == 0)
626 else if (ext.compare(
".MOV") == 0)
628 else if (ext.compare(
".mov") == 0)
630 else if (ext.compare(
".OGV") == 0)
632 else if (ext.compare(
".ogv") == 0)
634 else if (ext.compare(
".WMV") == 0)
636 else if (ext.compare(
".wmv") == 0)
638 else if (ext.compare(
".FLV") == 0)
640 else if (ext.compare(
".flv") == 0)
642 else if (ext.compare(
".MKV") == 0)
644 else if (ext.compare(
".mkv") == 0)
646 else if (ext.compare(
".MTS") == 0)
648 else if (ext.compare(
".mts") == 0)
651 return FORMAT_UNKNOWN;
655 std::string vpVideoReader::getExtension(
const std::string &filename)
658 size_t dot = filename.find_last_of(
".");
659 std::string ext = filename.substr(dot, filename.size() - 1);
666 void vpVideoReader::findLastFrameIndex()
673 if (m_imSequence != NULL) {
674 if (!m_lastFrameIndexIsSet) {
682 for (
size_t i = 0; i < files.size(); i++) {
686 if ((imageIndex != -1) && (imageIndex > m_lastFrame)) {
687 m_lastFrame = imageIndex;
693 #if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
694 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
695 else if (!m_lastFrameIndexIsSet) {
696 m_lastFrame = (long)m_capture.get(cv::CAP_PROP_FRAME_COUNT);
697 if (m_lastFrame <= 2) {
701 m_lastframe_unknown =
true;
702 m_lastFrame = 100000;
706 else if (!m_lastFrameIndexIsSet) {
707 m_lastFrame = (long)m_capture.get(CV_CAP_PROP_FRAME_COUNT);
708 if (m_lastFrame <= 2) {
712 m_lastframe_unknown =
true;
713 m_lastFrame = 100000;
723 void vpVideoReader::findFirstFrameIndex()
725 if (m_imSequence != NULL) {
726 if (!m_firstFrameIndexIsSet) {
734 for (
size_t i = 0; i < files.size(); i++) {
739 if ((imageIndex != -1) && (imageIndex < m_firstFrame || m_firstFrame == -1)) {
740 m_firstFrame = imageIndex;
746 #if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
748 else if (!m_firstFrameIndexIsSet) {
757 bool vpVideoReader::isImageExtensionSupported()
const
759 return (m_formatType == FORMAT_PGM || m_formatType == FORMAT_PPM || m_formatType == FORMAT_JPEG ||
760 m_formatType == FORMAT_PNG || m_formatType == FORMAT_TIFF || m_formatType == FORMAT_BMP ||
761 m_formatType == FORMAT_DIB || m_formatType == FORMAT_PBM || m_formatType == FORMAT_RASTER ||
762 m_formatType == FORMAT_JPEG2000);
768 bool vpVideoReader::isVideoExtensionSupported()
const
770 return (m_formatType == FORMAT_AVI || m_formatType == FORMAT_MPEG || m_formatType == FORMAT_MPEG4 ||
771 m_formatType == FORMAT_MOV || m_formatType == FORMAT_OGV || m_formatType == FORMAT_WMV ||
772 m_formatType == FORMAT_FLV || m_formatType == FORMAT_MKV || m_formatType == FORMAT_MTS);
837 bool vpVideoReader::checkImageNameFormat(
const std::string &format)
const
839 size_t indexBegin = format.find_last_of(
'%');
840 size_t indexEnd = format.find_first_of(
'd', indexBegin);
841 if (indexBegin == std::string::npos || indexEnd == std::string::npos) {
844 for (
size_t i = indexBegin + 1; i < indexEnd; i++) {
845 if (!std::isdigit(format[i])) {
862 switch (m_formatType) {
Class to grab (ie. read) images from the disk.
void setGenericName(const std::string &genericName)
std::string getImageName() const
void setImageNumber(long number)
long getImageNumber() const
void acquire(vpImage< unsigned char > &I)
error that can be emitted by ViSP classes.
@ badValue
Used to indicate that a value is not in the allowed range.
@ notInitialized
Used to indicate that a parameter is not initialized.
Base class for all video devices. It is designed to provide a front end to video sources.
unsigned int height
Number of rows in the image.
unsigned int width
Number of columns in the image.
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Error that can be emitted by the vpImage class and its derivatives.
@ noFileNameError
Image file name error.
unsigned int getWidth() const
unsigned int getHeight() const
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
bool isVideoFormat() const
void acquire(vpImage< vpRGBa > &I)
void setLastFrameIndex(const long last_frame)
void open(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
bool getFrame(vpImage< vpRGBa > &I, long frame)
vpVideoReader & operator>>(vpImage< unsigned char > &I)