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),
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) {
160 "not correspond to a readable "
161 "format supported by ViSP."));
164 findFirstFrameIndex();
166 findLastFrameIndex();
180 m_frameCount = m_firstFrame;
187 m_frameCount = m_firstFrame;
189 if (isVideoExtensionSupported()) {
190 #if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
192 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
193 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_firstFrame - 1);
195 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_firstFrame - 1);
213 m_frameCount = m_firstFrame;
220 m_frameCount = m_firstFrame;
222 if (isVideoExtensionSupported()) {
223 #if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
225 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
226 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_firstFrame - 1);
228 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_firstFrame - 1);
250 if (m_imSequence !=
nullptr) {
251 m_imSequence->
setStep(m_frameStep);
252 bool skip_frame =
false;
261 }
while (skip_frame && m_imSequence->
getImageNumber() < m_lastFrame);
264 if (m_frameCount + m_frameStep > m_lastFrame) {
267 else if (m_frameCount + m_frameStep < m_firstFrame) {
271 #if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
273 m_capture >> m_frame;
274 if (m_frameStep == 1) {
278 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
279 m_frameCount = (long)m_capture.get(cv::CAP_PROP_POS_FRAMES);
280 if (m_frameStep > 0) {
281 if (m_frameCount + m_frameStep <= m_lastFrame) {
282 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
285 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount - 1);
288 else if (m_frameStep < 0) {
289 if (m_frameCount + m_frameStep >= m_firstFrame) {
290 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);
297 m_frameCount = (long)m_capture.get(CV_CAP_PROP_POS_FRAMES);
298 if (m_frameStep > 0) {
299 if (m_frameCount + m_frameStep <= m_lastFrame) {
300 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
303 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount - 1);
306 else if (m_frameStep < 0) {
307 if (m_frameCount + m_frameStep >= m_firstFrame) {
308 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
311 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_firstFrame - 1);
317 if (m_frame.empty()) {
318 std::cout <<
"Warning: Unable to decode image " << m_frameCount - m_frameStep << std::endl;
319 if (m_lastframe_unknown) {
345 if (m_imSequence !=
nullptr) {
346 m_imSequence->
setStep(m_frameStep);
347 bool skip_frame =
false;
356 }
while (skip_frame && m_imSequence->
getImageNumber() < m_lastFrame);
359 if (m_frameCount + m_frameStep > m_lastFrame) {
362 else if (m_frameCount + m_frameStep < m_firstFrame) {
366 #if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
368 m_capture >> m_frame;
369 if (m_frameStep == 1) {
373 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
374 m_frameCount = (long)m_capture.get(cv::CAP_PROP_POS_FRAMES);
375 if (m_frameStep > 0) {
376 if (m_frameCount + m_frameStep <= m_lastFrame) {
377 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);
383 else if (m_frameStep < 0) {
384 if (m_frameCount + m_frameStep >= m_firstFrame) {
385 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
388 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_firstFrame - 1);
392 m_frameCount = (long)m_capture.get(CV_CAP_PROP_POS_FRAMES);
393 if (m_frameStep > 0) {
394 if (m_frameCount + m_frameStep <= m_lastFrame) {
395 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
398 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount - 1);
401 else if (m_frameStep < 0) {
402 if (m_frameCount + m_frameStep >= m_firstFrame) {
403 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
406 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_firstFrame - 1);
412 if (m_frame.empty()) {
413 std::cout <<
"Warning: Unable to decode image " << m_frameCount - m_frameStep << std::endl;
437 if (m_imSequence !=
nullptr) {
439 m_imSequence->
acquire(I, frame_index);
444 if (m_frameCount + m_frameStep > m_lastFrame) {
447 else if (m_frameCount + m_frameStep < m_firstFrame) {
457 #if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
458 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
459 if (!m_capture.set(cv::CAP_PROP_POS_FRAMES, frame_index)) {
460 vpERROR_TRACE(
"Couldn't find the %ld th frame", frame_index);
464 m_capture >> m_frame;
465 m_frameCount = frame_index + m_frameStep;
466 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount);
467 if (m_frame.empty()) {
469 m_capture >> m_frame;
470 if (m_frame.empty()) {
481 if (!m_capture.set(CV_CAP_PROP_POS_FRAMES, frame_index)) {
482 vpERROR_TRACE(
"Couldn't find the %ld th frame", frame_index);
486 m_capture >> m_frame;
487 m_frameCount = frame_index + m_frameStep;
488 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount);
514 if (m_imSequence !=
nullptr) {
516 m_imSequence->
acquire(I, frame_index);
521 if (m_frameCount + m_frameStep > m_lastFrame) {
524 else if (m_frameCount + m_frameStep < m_firstFrame) {
534 #if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
535 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
536 if (!m_capture.set(cv::CAP_PROP_POS_FRAMES, frame_index)) {
537 vpERROR_TRACE(
"Couldn't find the %ld th frame", frame_index);
540 m_capture >> m_frame;
541 if (m_frame.empty()) {
543 m_capture >> m_frame;
544 if (m_frame.empty()) {
556 if (!m_capture.set(CV_CAP_PROP_POS_FRAMES, frame_index)) {
561 m_capture >> m_frame;
562 m_frameCount = (long)m_capture.get(CV_CAP_PROP_POS_FRAMES);
563 if (m_frameStep > 1) {
564 m_frameCount += m_frameStep - 1;
565 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount);
567 else if (m_frameStep < -1) {
568 m_frameCount += m_frameStep - 1;
569 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount);
586 vpVideoReader::vpVideoFormatType vpVideoReader::getFormat(
const std::string &filename)
const
588 std::string ext = vpVideoReader::getExtension(filename);
590 if (ext.compare(
".PGM") == 0)
592 else if (ext.compare(
".pgm") == 0)
594 else if (ext.compare(
".PPM") == 0)
596 else if (ext.compare(
".ppm") == 0)
598 else if (ext.compare(
".JPG") == 0)
600 else if (ext.compare(
".jpg") == 0)
602 else if (ext.compare(
".JPEG") == 0)
604 else if (ext.compare(
".jpeg") == 0)
606 else if (ext.compare(
".PNG") == 0)
608 else if (ext.compare(
".png") == 0)
610 else if (ext.compare(
".TIFF") == 0)
612 else if (ext.compare(
".tiff") == 0)
614 else if (ext.compare(
".BMP") == 0)
616 else if (ext.compare(
".bmp") == 0)
618 else if (ext.compare(
".DIB") == 0)
620 else if (ext.compare(
".dib") == 0)
622 else if (ext.compare(
".PBM") == 0)
624 else if (ext.compare(
".pbm") == 0)
626 else if (ext.compare(
".SR") == 0)
628 else if (ext.compare(
".sr") == 0)
630 else if (ext.compare(
".RAS") == 0)
631 return FORMAT_RASTER;
632 else if (ext.compare(
".ras") == 0)
633 return FORMAT_RASTER;
634 else if (ext.compare(
".JP2") == 0)
635 return FORMAT_JPEG2000;
636 else if (ext.compare(
".jp2") == 0)
637 return FORMAT_JPEG2000;
638 else if (ext.compare(
".AVI") == 0)
640 else if (ext.compare(
".avi") == 0)
642 else if (ext.compare(
".MPEG") == 0)
644 else if (ext.compare(
".mpeg") == 0)
646 else if (ext.compare(
".MPG") == 0)
648 else if (ext.compare(
".mpg") == 0)
650 else if (ext.compare(
".MPEG4") == 0)
652 else if (ext.compare(
".mpeg4") == 0)
654 else if (ext.compare(
".MP4") == 0)
656 else if (ext.compare(
".mp4") == 0)
658 else if (ext.compare(
".MOV") == 0)
660 else if (ext.compare(
".mov") == 0)
662 else if (ext.compare(
".OGV") == 0)
664 else if (ext.compare(
".ogv") == 0)
666 else if (ext.compare(
".WMV") == 0)
668 else if (ext.compare(
".wmv") == 0)
670 else if (ext.compare(
".FLV") == 0)
672 else if (ext.compare(
".flv") == 0)
674 else if (ext.compare(
".MKV") == 0)
676 else if (ext.compare(
".mkv") == 0)
678 else if (ext.compare(
".MTS") == 0)
680 else if (ext.compare(
".mts") == 0)
683 return FORMAT_UNKNOWN;
687 std::string vpVideoReader::getExtension(
const std::string &filename)
690 size_t dot = filename.find_last_of(
".");
691 std::string ext = filename.substr(dot, filename.size() - 1);
698 void vpVideoReader::findLastFrameIndex()
705 if (m_imSequence !=
nullptr) {
706 if (!m_lastFrameIndexIsSet) {
714 for (
size_t i = 0; i < files.size(); i++) {
718 if ((imageIndex != -1) && (imageIndex > m_lastFrame)) {
719 m_lastFrame = imageIndex;
725 #if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
726 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
727 else if (!m_lastFrameIndexIsSet) {
728 m_lastFrame = (long)m_capture.get(cv::CAP_PROP_FRAME_COUNT);
729 if (m_lastFrame <= 2) {
733 m_lastframe_unknown =
true;
734 m_lastFrame = 100000;
738 else if (!m_lastFrameIndexIsSet) {
739 m_lastFrame = (long)m_capture.get(CV_CAP_PROP_FRAME_COUNT);
740 if (m_lastFrame <= 2) {
744 m_lastframe_unknown =
true;
745 m_lastFrame = 100000;
755 void vpVideoReader::findFirstFrameIndex()
757 if (m_imSequence !=
nullptr) {
758 if (!m_firstFrameIndexIsSet) {
766 for (
size_t i = 0; i < files.size(); i++) {
771 if ((imageIndex != -1) && (imageIndex < m_firstFrame || m_firstFrame == -1)) {
772 m_firstFrame = imageIndex;
778 #if defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
780 else if (!m_firstFrameIndexIsSet) {
789 bool vpVideoReader::isImageExtensionSupported()
const
791 return (m_formatType == FORMAT_PGM || m_formatType == FORMAT_PPM || m_formatType == FORMAT_JPEG ||
792 m_formatType == FORMAT_PNG || m_formatType == FORMAT_TIFF || m_formatType == FORMAT_BMP ||
793 m_formatType == FORMAT_DIB || m_formatType == FORMAT_PBM || m_formatType == FORMAT_RASTER ||
794 m_formatType == FORMAT_JPEG2000);
800 bool vpVideoReader::isVideoExtensionSupported()
const
802 return (m_formatType == FORMAT_AVI || m_formatType == FORMAT_MPEG || m_formatType == FORMAT_MPEG4 ||
803 m_formatType == FORMAT_MOV || m_formatType == FORMAT_OGV || m_formatType == FORMAT_WMV ||
804 m_formatType == FORMAT_FLV || m_formatType == FORMAT_MKV || m_formatType == FORMAT_MTS);
869 bool vpVideoReader::checkImageNameFormat(
const std::string &format)
const
871 size_t indexBegin = format.find_last_of(
'%');
872 size_t indexEnd = format.find_first_of(
'd', indexBegin);
873 if (indexBegin == std::string::npos || indexEnd == std::string::npos) {
876 for (
size_t i = indexBegin + 1; i < indexEnd; i++) {
877 if (!std::isdigit(format[i])) {
894 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)