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),
63 m_firstFrame(0), m_lastFrame(0), m_firstFrameIndexIsSet(false), m_lastFrameIndexIsSet(false), m_frameStep(1),
73 if (m_imSequence != NULL) {
95 if (filename.empty()) {
99 m_videoName = filename;
100 m_frameName = filename;
102 m_formatType = getFormat(filename);
104 if (m_formatType == FORMAT_UNKNOWN) {
109 if (isImageExtensionSupported()) {
111 if (!checkImageNameFormat(format)) {
116 m_initFileName =
true;
122 void vpVideoReader::getProperties()
128 if (!m_initFileName) {
132 if (isImageExtensionSupported()) {
135 m_imSequence->
setStep(m_frameStep);
136 if (m_firstFrameIndexIsSet) {
140 }
else if (isVideoExtensionSupported()) {
141 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
142 m_capture.open(m_videoName.c_str());
144 if (!m_capture.isOpened()) {
147 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
148 width = (
unsigned int)m_capture.get(cv::CAP_PROP_FRAME_WIDTH);
149 height = (
unsigned int)m_capture.get(cv::CAP_PROP_FRAME_HEIGHT);
150 m_frameRate = (double)m_capture.get(cv::CAP_PROP_FPS);
152 width = (
unsigned int)m_capture.get(CV_CAP_PROP_FRAME_WIDTH);
153 height = (
unsigned int)m_capture.get(CV_CAP_PROP_FRAME_HEIGHT);
154 m_frameRate = m_capture.get(CV_CAP_PROP_FPS);
159 "3rd >= 2.1.0 party libraries."));
161 }
else if (m_formatType == FORMAT_UNKNOWN) {
165 "not correspond to a readable "
166 "format supported by ViSP."));
169 findFirstFrameIndex();
171 findLastFrameIndex();
185 m_frameCount = m_firstFrame;
192 m_frameCount = m_firstFrame;
194 if (isVideoExtensionSupported()) {
195 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
197 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
198 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_firstFrame - 1);
200 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_firstFrame - 1);
218 m_frameCount = m_firstFrame;
225 m_frameCount = m_firstFrame;
227 if (isVideoExtensionSupported()) {
228 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
230 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
231 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_firstFrame - 1);
233 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_firstFrame - 1);
255 if (m_imSequence != NULL) {
256 m_imSequence->
setStep(m_frameStep);
257 bool skip_frame =
false;
265 }
while (skip_frame && m_imSequence->
getImageNumber() < m_lastFrame);
268 if (m_frameCount + m_frameStep > m_lastFrame) {
270 }
else if (m_frameCount + m_frameStep < m_firstFrame) {
274 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
276 m_capture >> m_frame;
277 if (m_frameStep == 1) {
280 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
281 m_frameCount = (long)m_capture.get(cv::CAP_PROP_POS_FRAMES);
282 if (m_frameStep > 0) {
283 if (m_frameCount + m_frameStep <= m_lastFrame) {
284 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
286 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);
292 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_firstFrame - 1);
296 m_frameCount = (long)m_capture.get(CV_CAP_PROP_POS_FRAMES);
297 if (m_frameStep > 0) {
298 if (m_frameCount + m_frameStep <= m_lastFrame) {
299 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);
303 }
else if (m_frameStep < 0) {
304 if (m_frameCount + m_frameStep >= m_firstFrame) {
305 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount + m_frameStep - 1);
307 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_firstFrame - 1);
313 if (m_frame.empty()) {
314 std::cout <<
"Warning: Unable to decode image " << m_frameCount - m_frameStep << std::endl;
315 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;
422 if (m_imSequence != NULL) {
424 m_imSequence->
acquire(I, frame_index);
429 if (m_frameCount + m_frameStep > m_lastFrame) {
431 }
else if (m_frameCount + m_frameStep < m_firstFrame) {
439 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x030000)
440 if (!m_capture.set(cv::CAP_PROP_POS_FRAMES, frame_index)) {
441 vpERROR_TRACE(
"Couldn't find the %ld th frame", frame_index);
445 m_capture >> m_frame;
446 m_frameCount = frame_index + m_frameStep;
447 m_capture.set(cv::CAP_PROP_POS_FRAMES, m_frameCount);
448 if (m_frame.empty()) {
450 m_capture >> m_frame;
451 if (m_frame.empty()) {
459 #elif defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100)
460 if (!m_capture.set(CV_CAP_PROP_POS_FRAMES, frame_index)) {
461 vpERROR_TRACE(
"Couldn't find the %ld th frame", frame_index);
465 m_capture >> m_frame;
466 m_frameCount = frame_index + m_frameStep;
467 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount);
492 if (m_imSequence != NULL) {
494 m_imSequence->
acquire(I, frame_index);
499 if (m_frameCount + m_frameStep > m_lastFrame) {
501 }
else if (m_frameCount + m_frameStep < m_firstFrame) {
509 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
510 if (!m_capture.set(cv::CAP_PROP_POS_FRAMES, frame_index)) {
511 vpERROR_TRACE(
"Couldn't find the %ld th frame", frame_index);
514 m_capture >> m_frame;
515 if (m_frame.empty()) {
517 m_capture >> m_frame;
518 if (m_frame.empty()) {
527 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
528 if (!m_capture.set(CV_CAP_PROP_POS_FRAMES, frame_index)) {
533 m_capture >> m_frame;
534 m_frameCount = (long)m_capture.get(CV_CAP_PROP_POS_FRAMES);
535 if (m_frameStep > 1) {
536 m_frameCount += m_frameStep - 1;
537 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount);
538 }
else if (m_frameStep < -1) {
539 m_frameCount += m_frameStep - 1;
540 m_capture.set(CV_CAP_PROP_POS_FRAMES, m_frameCount);
556 vpVideoReader::vpVideoFormatType vpVideoReader::getFormat(
const std::string &filename)
const
558 std::string ext = vpVideoReader::getExtension(filename);
560 if (ext.compare(
".PGM") == 0)
562 else if (ext.compare(
".pgm") == 0)
564 else if (ext.compare(
".PPM") == 0)
566 else if (ext.compare(
".ppm") == 0)
568 else if (ext.compare(
".JPG") == 0)
570 else if (ext.compare(
".jpg") == 0)
572 else if (ext.compare(
".JPEG") == 0)
574 else if (ext.compare(
".jpeg") == 0)
576 else if (ext.compare(
".PNG") == 0)
578 else if (ext.compare(
".png") == 0)
580 else if (ext.compare(
".TIFF") == 0)
582 else if (ext.compare(
".tiff") == 0)
584 else if (ext.compare(
".BMP") == 0)
586 else if (ext.compare(
".bmp") == 0)
588 else if (ext.compare(
".DIB") == 0)
590 else if (ext.compare(
".dib") == 0)
592 else if (ext.compare(
".PBM") == 0)
594 else if (ext.compare(
".pbm") == 0)
596 else if (ext.compare(
".SR") == 0)
598 else if (ext.compare(
".sr") == 0)
600 else if (ext.compare(
".RAS") == 0)
601 return FORMAT_RASTER;
602 else if (ext.compare(
".ras") == 0)
603 return FORMAT_RASTER;
604 else if (ext.compare(
".JP2") == 0)
605 return FORMAT_JPEG2000;
606 else if (ext.compare(
".jp2") == 0)
607 return FORMAT_JPEG2000;
608 else if (ext.compare(
".AVI") == 0)
610 else if (ext.compare(
".avi") == 0)
612 else if (ext.compare(
".MPEG") == 0)
614 else if (ext.compare(
".mpeg") == 0)
616 else if (ext.compare(
".MPG") == 0)
618 else if (ext.compare(
".mpg") == 0)
620 else if (ext.compare(
".MPEG4") == 0)
622 else if (ext.compare(
".mpeg4") == 0)
624 else if (ext.compare(
".MP4") == 0)
626 else if (ext.compare(
".mp4") == 0)
628 else if (ext.compare(
".MOV") == 0)
630 else if (ext.compare(
".mov") == 0)
632 else if (ext.compare(
".OGV") == 0)
634 else if (ext.compare(
".ogv") == 0)
636 else if (ext.compare(
".WMV") == 0)
638 else if (ext.compare(
".wmv") == 0)
640 else if (ext.compare(
".FLV") == 0)
642 else if (ext.compare(
".flv") == 0)
644 else if (ext.compare(
".MKV") == 0)
646 else if (ext.compare(
".mkv") == 0)
648 else if (ext.compare(
".MTS") == 0)
650 else if (ext.compare(
".mts") == 0)
653 return FORMAT_UNKNOWN;
657 std::string vpVideoReader::getExtension(
const std::string &filename)
660 size_t dot = filename.find_last_of(
".");
661 std::string ext = filename.substr(dot, filename.size() - 1);
668 void vpVideoReader::findLastFrameIndex()
675 if (m_imSequence != NULL) {
676 if (!m_lastFrameIndexIsSet) {
684 for (
size_t i = 0; i < files.size(); i++) {
688 if ((imageIndex != -1) && (imageIndex > m_lastFrame)) {
689 m_lastFrame = imageIndex;
695 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
696 else if (!m_lastFrameIndexIsSet) {
697 m_lastFrame = (long)m_capture.get(cv::CAP_PROP_FRAME_COUNT);
698 if (m_lastFrame <= 2) {
702 m_lastframe_unknown =
true;
703 m_lastFrame = 100000;
706 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
707 else if (!m_lastFrameIndexIsSet) {
708 m_lastFrame = (long)m_capture.get(CV_CAP_PROP_FRAME_COUNT);
709 if (m_lastFrame <= 2) {
713 m_lastframe_unknown =
true;
714 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 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) {
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 emited 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 emited by the vpImage class and its derivates.
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)