39 #include <visp3/core/vpIoTools.h>
40 #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),
66 if (m_imSequence !=
nullptr) {
88 if (filename.empty()) {
92 m_videoName = filename;
93 m_frameName = filename;
95 m_formatType = getFormat(filename);
97 if (m_formatType == FORMAT_UNKNOWN) {
102 if (isImageExtensionSupported()) {
104 if (!checkImageNameFormat(format)) {
109 m_initFileName =
true;
115 void vpVideoReader::getProperties()
121 if (!m_initFileName) {
125 if (isImageExtensionSupported()) {
128 m_imSequence->
setStep(m_frameStep);
129 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."));
156 else if (m_formatType == FORMAT_UNKNOWN) {
158 "not correspond to a readable "
159 "format supported by ViSP."));
162 findFirstFrameIndex();
164 findLastFrameIndex();
178 m_frameCount = m_firstFrame;
185 m_frameCount = m_firstFrame;
187 if (isVideoExtensionSupported()) {
188 #if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
190 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
191 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_firstFrame - 1);
193 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_firstFrame - 1);
211 m_frameCount = m_firstFrame;
218 m_frameCount = m_firstFrame;
220 if (isVideoExtensionSupported()) {
221 #if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
223 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
224 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_firstFrame - 1);
226 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_firstFrame - 1);
248 if (m_imSequence !=
nullptr) {
249 m_imSequence->
setStep(m_frameStep);
250 bool skip_frame =
false;
259 }
while (skip_frame && m_imSequence->
getImageNumber() < m_lastFrame);
262 if (m_frameCount + m_frameStep > m_lastFrame) {
265 else if (m_frameCount + m_frameStep < m_firstFrame) {
269 #if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
271 m_capture >> m_frame;
272 if (m_frameStep == 1) {
276 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
277 m_frameCount = (long)m_capture.get(cv::CAP_PROP_POS_FRAMES);
278 if (m_frameStep > 0) {
279 if (m_frameCount + m_frameStep <= m_lastFrame) {
280 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
283 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount - 1);
286 else if (m_frameStep < 0) {
287 if (m_frameCount + m_frameStep >= m_firstFrame) {
288 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
291 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_firstFrame - 1);
295 m_frameCount = (long)m_capture.get(CV_CAP_PROP_POS_FRAMES);
296 if (m_frameStep > 0) {
297 if (m_frameCount + m_frameStep <= m_lastFrame) {
298 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
301 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount - 1);
304 else if (m_frameStep < 0) {
305 if (m_frameCount + m_frameStep >= m_firstFrame) {
306 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
309 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_firstFrame - 1);
315 if (m_frame.empty()) {
316 std::cout <<
"Warning: Unable to decode image " << m_frameCount - m_frameStep << std::endl;
317 if (m_lastframe_unknown) {
343 if (m_imSequence !=
nullptr) {
344 m_imSequence->
setStep(m_frameStep);
345 bool skip_frame =
false;
354 }
while (skip_frame && m_imSequence->
getImageNumber() < m_lastFrame);
357 if (m_frameCount + m_frameStep > m_lastFrame) {
360 else if (m_frameCount + m_frameStep < m_firstFrame) {
364 #if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
366 m_capture >> m_frame;
367 if (m_frameStep == 1) {
371 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
372 m_frameCount = (long)m_capture.get(cv::CAP_PROP_POS_FRAMES);
373 if (m_frameStep > 0) {
374 if (m_frameCount + m_frameStep <= m_lastFrame) {
375 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
378 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount - 1);
381 else if (m_frameStep < 0) {
382 if (m_frameCount + m_frameStep >= m_firstFrame) {
383 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);
390 m_frameCount = (long)m_capture.get(CV_CAP_PROP_POS_FRAMES);
391 if (m_frameStep > 0) {
392 if (m_frameCount + m_frameStep <= m_lastFrame) {
393 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
396 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount - 1);
399 else if (m_frameStep < 0) {
400 if (m_frameCount + m_frameStep >= m_firstFrame) {
401 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
404 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_firstFrame - 1);
410 if (m_frame.empty()) {
411 std::cout <<
"Warning: Unable to decode image " << m_frameCount - m_frameStep << std::endl;
435 if (m_imSequence !=
nullptr) {
437 m_imSequence->
acquire(I, frame_index);
442 if (m_frameCount + m_frameStep > m_lastFrame) {
445 else if (m_frameCount + m_frameStep < m_firstFrame) {
455 #if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
456 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
457 if (!m_capture.set(cv::CAP_PROP_POS_FRAMES, frame_index)) {
462 m_capture >> m_frame;
463 m_frameCount = frame_index + m_frameStep;
464 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount);
465 if (m_frame.empty()) {
467 m_capture >> m_frame;
468 if (m_frame.empty()) {
479 if (!m_capture.set(CV_CAP_PROP_POS_FRAMES, frame_index)) {
484 m_capture >> m_frame;
485 m_frameCount = frame_index + m_frameStep;
486 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount);
512 if (m_imSequence !=
nullptr) {
514 m_imSequence->
acquire(I, frame_index);
519 if (m_frameCount + m_frameStep > m_lastFrame) {
522 else if (m_frameCount + m_frameStep < m_firstFrame) {
532 #if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
533 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
534 if (!m_capture.set(cv::CAP_PROP_POS_FRAMES, frame_index)) {
538 m_capture >> m_frame;
539 if (m_frame.empty()) {
541 m_capture >> m_frame;
542 if (m_frame.empty()) {
554 if (!m_capture.set(CV_CAP_PROP_POS_FRAMES, frame_index)) {
558 m_capture >> m_frame;
559 m_frameCount = (long)m_capture.get(CV_CAP_PROP_POS_FRAMES);
560 if (m_frameStep > 1) {
561 m_frameCount += m_frameStep - 1;
562 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount);
564 else if (m_frameStep < -1) {
565 m_frameCount += m_frameStep - 1;
566 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount);
583 vpVideoReader::vpVideoFormatType vpVideoReader::getFormat(
const std::string &filename)
const
585 std::string ext = vpVideoReader::getExtension(filename);
587 if (ext.compare(
".PGM") == 0)
589 else if (ext.compare(
".pgm") == 0)
591 else if (ext.compare(
".PPM") == 0)
593 else if (ext.compare(
".ppm") == 0)
595 else if (ext.compare(
".JPG") == 0)
597 else if (ext.compare(
".jpg") == 0)
599 else if (ext.compare(
".JPEG") == 0)
601 else if (ext.compare(
".jpeg") == 0)
603 else if (ext.compare(
".PNG") == 0)
605 else if (ext.compare(
".png") == 0)
607 else if (ext.compare(
".TIFF") == 0)
609 else if (ext.compare(
".tiff") == 0)
611 else if (ext.compare(
".BMP") == 0)
613 else if (ext.compare(
".bmp") == 0)
615 else if (ext.compare(
".DIB") == 0)
617 else if (ext.compare(
".dib") == 0)
619 else if (ext.compare(
".PBM") == 0)
621 else if (ext.compare(
".pbm") == 0)
623 else if (ext.compare(
".SR") == 0)
625 else if (ext.compare(
".sr") == 0)
627 else if (ext.compare(
".RAS") == 0)
628 return FORMAT_RASTER;
629 else if (ext.compare(
".ras") == 0)
630 return FORMAT_RASTER;
631 else if (ext.compare(
".JP2") == 0)
632 return FORMAT_JPEG2000;
633 else if (ext.compare(
".jp2") == 0)
634 return FORMAT_JPEG2000;
635 else if (ext.compare(
".AVI") == 0)
637 else if (ext.compare(
".avi") == 0)
639 else if (ext.compare(
".MPEG") == 0)
641 else if (ext.compare(
".mpeg") == 0)
643 else if (ext.compare(
".MPG") == 0)
645 else if (ext.compare(
".mpg") == 0)
647 else if (ext.compare(
".MPEG4") == 0)
649 else if (ext.compare(
".mpeg4") == 0)
651 else if (ext.compare(
".MP4") == 0)
653 else if (ext.compare(
".mp4") == 0)
655 else if (ext.compare(
".MOV") == 0)
657 else if (ext.compare(
".mov") == 0)
659 else if (ext.compare(
".OGV") == 0)
661 else if (ext.compare(
".ogv") == 0)
663 else if (ext.compare(
".WMV") == 0)
665 else if (ext.compare(
".wmv") == 0)
667 else if (ext.compare(
".FLV") == 0)
669 else if (ext.compare(
".flv") == 0)
671 else if (ext.compare(
".MKV") == 0)
673 else if (ext.compare(
".mkv") == 0)
675 else if (ext.compare(
".MTS") == 0)
677 else if (ext.compare(
".mts") == 0)
680 return FORMAT_UNKNOWN;
684 std::string vpVideoReader::getExtension(
const std::string &filename)
687 size_t dot = filename.find_last_of(
".");
688 std::string ext = filename.substr(dot, filename.size() - 1);
695 void vpVideoReader::findLastFrameIndex()
701 if (m_imSequence !=
nullptr) {
702 if (!m_lastFrameIndexIsSet) {
710 for (
size_t i = 0; i < files.size(); ++i) {
714 if ((imageIndex != -1) && (imageIndex > m_lastFrame)) {
715 m_lastFrame = imageIndex;
721 #if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
722 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
723 else if (!m_lastFrameIndexIsSet) {
724 m_lastFrame = (long)m_capture.get(cv::CAP_PROP_FRAME_COUNT);
725 if (m_lastFrame <= 2) {
729 m_lastframe_unknown =
true;
730 m_lastFrame = 100000;
734 else if (!m_lastFrameIndexIsSet) {
735 m_lastFrame = (long)m_capture.get(CV_CAP_PROP_FRAME_COUNT);
736 if (m_lastFrame <= 2) {
740 m_lastframe_unknown =
true;
741 m_lastFrame = 100000;
751 void vpVideoReader::findFirstFrameIndex()
753 if (m_imSequence !=
nullptr) {
754 if (!m_firstFrameIndexIsSet) {
762 for (
size_t i = 0; i < files.size(); ++i) {
767 if ((imageIndex != -1) && (imageIndex < m_firstFrame || m_firstFrame == -1)) {
768 m_firstFrame = imageIndex;
774 #if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
776 else if (!m_firstFrameIndexIsSet) {
785 bool vpVideoReader::isImageExtensionSupported()
const
787 return (m_formatType == FORMAT_PGM || m_formatType == FORMAT_PPM || m_formatType == FORMAT_JPEG ||
788 m_formatType == FORMAT_PNG || m_formatType == FORMAT_TIFF || m_formatType == FORMAT_BMP ||
789 m_formatType == FORMAT_DIB || m_formatType == FORMAT_PBM || m_formatType == FORMAT_RASTER ||
790 m_formatType == FORMAT_JPEG2000);
796 bool vpVideoReader::isVideoExtensionSupported()
const
798 return (m_formatType == FORMAT_AVI || m_formatType == FORMAT_MPEG || m_formatType == FORMAT_MPEG4 ||
799 m_formatType == FORMAT_MOV || m_formatType == FORMAT_OGV || m_formatType == FORMAT_WMV ||
800 m_formatType == FORMAT_FLV || m_formatType == FORMAT_MKV || m_formatType == FORMAT_MTS);
873 bool vpVideoReader::checkImageNameFormat(
const std::string &format)
const
875 size_t indexBegin = format.find_last_of(
'%');
876 size_t indexEnd = format.find_first_of(
'd', indexBegin);
877 if (indexBegin == std::string::npos || indexEnd == std::string::npos) {
880 for (
size_t i = indexBegin + 1; i < indexEnd; ++i) {
881 if (!std::isdigit(format[i])) {
898 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)