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_videoName(), m_frameName(), 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_videoName = filename;
99 m_frameName = filename;
101 m_formatType = getFormat(filename);
103 if (m_formatType == FORMAT_UNKNOWN) {
108 if (isImageExtensionSupported()) {
110 if (!checkImageNameFormat(format)) {
115 m_initFileName =
true;
121 void vpVideoReader::getProperties()
127 if (!m_initFileName) {
131 if (isImageExtensionSupported()) {
134 m_imSequence->
setStep(m_frameStep);
135 if (m_firstFrameIndexIsSet) {
139 }
else if (isVideoExtensionSupported()) {
140 #if VISP_HAVE_OPENCV_VERSION >= 0x020100 141 m_capture.open(m_videoName.c_str());
143 if (!m_capture.isOpened()) {
146 #if VISP_HAVE_OPENCV_VERSION >= 0x030000 147 width = (
unsigned int)m_capture.get(cv::CAP_PROP_FRAME_WIDTH);
148 height = (
unsigned int)m_capture.get(cv::CAP_PROP_FRAME_HEIGHT);
149 m_frameRate = (double)m_capture.get(cv::CAP_PROP_FPS);
151 width = (
unsigned int)m_capture.get(CV_CAP_PROP_FRAME_WIDTH);
152 height = (
unsigned int)m_capture.get(CV_CAP_PROP_FRAME_HEIGHT);
153 m_frameRate = m_capture.get(CV_CAP_PROP_FPS);
158 "3rd >= 2.1.0 party libraries."));
160 }
else if (m_formatType == FORMAT_UNKNOWN) {
164 "not correspond to a readable " 165 "format supported by ViSP."));
168 findFirstFrameIndex();
170 findLastFrameIndex();
184 m_frameCount = m_firstFrame;
191 m_frameCount = m_firstFrame;
193 if (isVideoExtensionSupported()) {
194 #if VISP_HAVE_OPENCV_VERSION >= 0x020100 196 #if VISP_HAVE_OPENCV_VERSION >= 0x030000 197 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_firstFrame - 1);
199 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_firstFrame - 1);
217 m_frameCount = m_firstFrame;
224 m_frameCount = m_firstFrame;
226 if (isVideoExtensionSupported()) {
227 #if VISP_HAVE_OPENCV_VERSION >= 0x020100 229 #if VISP_HAVE_OPENCV_VERSION >= 0x030000 230 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_firstFrame - 1);
232 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_firstFrame - 1);
254 if (m_imSequence != NULL) {
255 m_imSequence->
setStep(m_frameStep);
256 bool skip_frame =
false;
264 }
while (skip_frame && m_imSequence->
getImageNumber() < m_lastFrame);
267 if (m_frameCount + m_frameStep > m_lastFrame) {
269 }
else if (m_frameCount + m_frameStep < m_firstFrame) {
273 #if VISP_HAVE_OPENCV_VERSION >= 0x020100 275 m_capture >> m_frame;
276 if (m_frameStep == 1) {
279 #if VISP_HAVE_OPENCV_VERSION >= 0x030000 280 m_frameCount = (long)m_capture.get(cv::CAP_PROP_POS_FRAMES);
281 if (m_frameStep > 0) {
282 if (m_frameCount + m_frameStep <= m_lastFrame) {
283 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);
287 }
else if (m_frameStep < 0) {
288 if (m_frameCount + m_frameStep >= m_firstFrame) {
289 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);
300 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount - 1);
302 }
else if (m_frameStep < 0) {
303 if (m_frameCount + m_frameStep >= m_firstFrame) {
304 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
306 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_firstFrame - 1);
312 if (m_frame.empty()) {
313 std::cout <<
"Warning: Unable to decode image " << m_frameCount - m_frameStep << std::endl;
314 if (m_lastframe_unknown) {
340 if (m_imSequence != NULL) {
341 m_imSequence->
setStep(m_frameStep);
342 bool skip_frame =
false;
350 }
while (skip_frame && m_imSequence->
getImageNumber() < m_lastFrame);
353 if (m_frameCount + m_frameStep > m_lastFrame) {
355 }
else if (m_frameCount + m_frameStep < m_firstFrame) {
359 #if VISP_HAVE_OPENCV_VERSION >= 0x020100 361 m_capture >> m_frame;
362 if (m_frameStep == 1) {
365 #if VISP_HAVE_OPENCV_VERSION >= 0x030000 366 m_frameCount = (long)m_capture.get(cv::CAP_PROP_POS_FRAMES);
367 if (m_frameStep > 0) {
368 if (m_frameCount + m_frameStep <= m_lastFrame) {
369 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
371 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount - 1);
373 }
else if (m_frameStep < 0) {
374 if (m_frameCount + m_frameStep >= m_firstFrame) {
375 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
377 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_firstFrame - 1);
381 m_frameCount = (long)m_capture.get(CV_CAP_PROP_POS_FRAMES);
382 if (m_frameStep > 0) {
383 if (m_frameCount + m_frameStep <= m_lastFrame) {
384 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
386 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount - 1);
388 }
else if (m_frameStep < 0) {
389 if (m_frameCount + m_frameStep >= m_firstFrame) {
390 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
392 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_firstFrame - 1);
398 if (m_frame.empty()) {
399 std::cout <<
"Warning: Unable to decode image " << m_frameCount - m_frameStep << std::endl;
423 if (m_imSequence != NULL) {
425 m_imSequence->
acquire(I, frame_index);
430 if (m_frameCount + m_frameStep > m_lastFrame) {
432 }
else if (m_frameCount + m_frameStep < m_firstFrame) {
440 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x030000) 441 if (!m_capture.set(cv::CAP_PROP_POS_FRAMES, frame_index)) {
442 vpERROR_TRACE(
"Couldn't find the %ld th frame", frame_index);
446 m_capture >> m_frame;
447 m_frameCount = frame_index + m_frameStep;
448 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount);
449 if (m_frame.empty()) {
451 m_capture >> m_frame;
452 if (m_frame.empty()) {
460 #elif defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100) 461 if (!m_capture.set(CV_CAP_PROP_POS_FRAMES, frame_index)) {
462 vpERROR_TRACE(
"Couldn't find the %ld th frame", frame_index);
466 m_capture >> m_frame;
467 m_frameCount = frame_index + m_frameStep;
468 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount);
493 if (m_imSequence != NULL) {
495 m_imSequence->
acquire(I, frame_index);
500 if (m_frameCount + m_frameStep > m_lastFrame) {
502 }
else if (m_frameCount + m_frameStep < m_firstFrame) {
510 #if VISP_HAVE_OPENCV_VERSION >= 0x030000 511 if (!m_capture.set(cv::CAP_PROP_POS_FRAMES, frame_index)) {
512 vpERROR_TRACE(
"Couldn't find the %ld th frame", frame_index);
515 m_capture >> m_frame;
516 if (m_frame.empty()) {
518 m_capture >> m_frame;
519 if (m_frame.empty()) {
528 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100 529 if (!m_capture.set(CV_CAP_PROP_POS_FRAMES, frame_index)) {
534 m_capture >> m_frame;
535 m_frameCount = (long)m_capture.get(CV_CAP_PROP_POS_FRAMES);
536 if (m_frameStep > 1) {
537 m_frameCount += m_frameStep - 1;
538 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount);
539 }
else if (m_frameStep < -1) {
540 m_frameCount += m_frameStep - 1;
541 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount);
557 vpVideoReader::vpVideoFormatType vpVideoReader::getFormat(
const std::string &filename)
const 559 std::string ext = vpVideoReader::getExtension(filename);
561 if (ext.compare(
".PGM") == 0)
563 else if (ext.compare(
".pgm") == 0)
565 else if (ext.compare(
".PPM") == 0)
567 else if (ext.compare(
".ppm") == 0)
569 else if (ext.compare(
".JPG") == 0)
571 else if (ext.compare(
".jpg") == 0)
573 else if (ext.compare(
".JPEG") == 0)
575 else if (ext.compare(
".jpeg") == 0)
577 else if (ext.compare(
".PNG") == 0)
579 else if (ext.compare(
".png") == 0)
581 else if (ext.compare(
".TIFF") == 0)
583 else if (ext.compare(
".tiff") == 0)
585 else if (ext.compare(
".BMP") == 0)
587 else if (ext.compare(
".bmp") == 0)
589 else if (ext.compare(
".DIB") == 0)
591 else if (ext.compare(
".dib") == 0)
593 else if (ext.compare(
".PBM") == 0)
595 else if (ext.compare(
".pbm") == 0)
597 else if (ext.compare(
".SR") == 0)
599 else if (ext.compare(
".sr") == 0)
601 else if (ext.compare(
".RAS") == 0)
602 return FORMAT_RASTER;
603 else if (ext.compare(
".ras") == 0)
604 return FORMAT_RASTER;
605 else if (ext.compare(
".JP2") == 0)
606 return FORMAT_JPEG2000;
607 else if (ext.compare(
".jp2") == 0)
608 return FORMAT_JPEG2000;
609 else if (ext.compare(
".AVI") == 0)
611 else if (ext.compare(
".avi") == 0)
613 else if (ext.compare(
".MPEG") == 0)
615 else if (ext.compare(
".mpeg") == 0)
617 else if (ext.compare(
".MPG") == 0)
619 else if (ext.compare(
".mpg") == 0)
621 else if (ext.compare(
".MPEG4") == 0)
623 else if (ext.compare(
".mpeg4") == 0)
625 else if (ext.compare(
".MP4") == 0)
627 else if (ext.compare(
".mp4") == 0)
629 else if (ext.compare(
".MOV") == 0)
631 else if (ext.compare(
".mov") == 0)
633 else if (ext.compare(
".OGV") == 0)
635 else if (ext.compare(
".ogv") == 0)
637 else if (ext.compare(
".WMV") == 0)
639 else if (ext.compare(
".wmv") == 0)
641 else if (ext.compare(
".FLV") == 0)
643 else if (ext.compare(
".flv") == 0)
645 else if (ext.compare(
".MKV") == 0)
647 else if (ext.compare(
".mkv") == 0)
649 else if (ext.compare(
".MTS") == 0)
651 else if (ext.compare(
".mts") == 0)
654 return FORMAT_UNKNOWN;
658 std::string vpVideoReader::getExtension(
const std::string &filename)
661 size_t dot = filename.find_last_of(
".");
662 std::string ext = filename.substr(dot, filename.size() - 1);
669 void vpVideoReader::findLastFrameIndex()
676 if (m_imSequence != NULL) {
677 if (!m_lastFrameIndexIsSet) {
685 for (
size_t i = 0; i < files.size(); i++) {
689 if ((imageIndex != -1) && (imageIndex > m_lastFrame)) {
690 m_lastFrame = imageIndex;
696 #if VISP_HAVE_OPENCV_VERSION >= 0x030000 697 else if (!m_lastFrameIndexIsSet) {
698 m_lastFrame = (long)m_capture.get(cv::CAP_PROP_FRAME_COUNT);
699 if (m_lastFrame <= 2) {
703 m_lastframe_unknown =
true;
704 m_lastFrame = 100000;
707 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100 708 else if (!m_lastFrameIndexIsSet) {
709 m_lastFrame = (long)m_capture.get(CV_CAP_PROP_FRAME_COUNT);
710 if (m_lastFrame <= 2) {
714 m_lastframe_unknown =
true;
715 m_lastFrame = 100000;
724 void vpVideoReader::findFirstFrameIndex()
726 if (m_imSequence != NULL) {
727 if (!m_firstFrameIndexIsSet) {
735 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 VISP_HAVE_OPENCV_VERSION >= 0x020100 747 else if (!m_firstFrameIndexIsSet) {
756 bool vpVideoReader::isImageExtensionSupported()
const 758 return (m_formatType == FORMAT_PGM || m_formatType == FORMAT_PPM || m_formatType == FORMAT_JPEG ||
759 m_formatType == FORMAT_PNG || m_formatType == FORMAT_TIFF || m_formatType == FORMAT_BMP ||
760 m_formatType == FORMAT_DIB || m_formatType == FORMAT_PBM || m_formatType == FORMAT_RASTER ||
761 m_formatType == FORMAT_JPEG2000);
767 bool vpVideoReader::isVideoExtensionSupported()
const 769 return (m_formatType == FORMAT_AVI || m_formatType == FORMAT_MPEG || m_formatType == FORMAT_MPEG4 ||
770 m_formatType == FORMAT_MOV || m_formatType == FORMAT_OGV || m_formatType == FORMAT_WMV ||
771 m_formatType == FORMAT_FLV || m_formatType == FORMAT_MKV || m_formatType == FORMAT_MTS);
836 bool vpVideoReader::checkImageNameFormat(
const std::string &format)
const 838 size_t indexBegin = format.find_last_of(
'%');
839 size_t indexEnd = format.find_first_of(
'd', indexBegin);
840 if (indexBegin == std::string::npos || indexEnd == std::string::npos) {
843 for (
size_t i = indexBegin + 1; i < indexEnd; i++) {
844 if (!std::isdigit(format[i])) {
861 switch (m_formatType) {
long getImageNumber() const
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)
bool isVideoFormat() const
unsigned int height
Number of rows in the image.
std::string getImageName() const
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.