44 #include <visp3/core/vpDebug.h>
45 #include <visp3/io/vpVideoReader.h>
56 #ifdef VISP_HAVE_FFMPEG
58 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
61 formatType(FORMAT_UNKNOWN), initFileName(false), isOpen(false), frameCount(0),
62 firstFrame(0), lastFrame(0), firstFrameIndexIsSet(false), lastFrameIndexIsSet(false)
71 if (imSequence != NULL)
75 #ifdef VISP_HAVE_FFMPEG
95 if (!filename || *filename ==
'\0')
101 if (strlen( filename ) >= FILENAME_MAX) {
103 "Not enough memory to intialize the file name"));
106 strcpy(this->fileName,filename);
108 formatType = getFormat(fileName);
110 if (formatType == FORMAT_UNKNOWN) {
146 if (isImageExtensionSupported())
150 if (firstFrameIndexIsSet)
153 else if (isVideoExtensionSupported())
155 #ifdef VISP_HAVE_FFMPEG
160 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
161 capture.open(fileName);
163 if(!capture.isOpened())
172 else if (formatType == FORMAT_UNKNOWN)
178 findFirstFrameIndex();
179 frameCount = firstFrame;
190 findLastFrameIndex();
191 frameCount = firstFrame;
210 if (isImageExtensionSupported())
214 if (firstFrameIndexIsSet)
217 else if (isVideoExtensionSupported())
219 #ifdef VISP_HAVE_FFMPEG
224 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
225 capture.open(fileName);
227 if(!capture.isOpened())
236 else if (formatType == FORMAT_UNKNOWN)
242 findFirstFrameIndex();
243 frameCount = firstFrame;
254 findLastFrameIndex();
255 frameCount = firstFrame;
275 if (imSequence != NULL)
280 #ifdef VISP_HAVE_FFMPEG
281 else if (ffmpeg !=NULL)
286 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
290 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
291 frameCount = (long) capture.get(cv::CAP_PROP_POS_FRAMES);
293 frameCount = (long) capture.get(CV_CAP_PROP_POS_FRAMES);
318 if (imSequence != NULL)
323 #ifdef VISP_HAVE_FFMPEG
324 else if (ffmpeg != NULL)
329 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
333 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
334 frameCount = (long) capture.get(cv::CAP_PROP_POS_FRAMES);
336 frameCount = (long) capture.get(CV_CAP_PROP_POS_FRAMES);
362 if (imSequence != NULL)
366 imSequence->
acquire(I, frame_index);
367 frameCount = frame_index + 1;
371 vpERROR_TRACE(
"Couldn't find the %u th frame", frame_index) ;
377 #ifdef VISP_HAVE_FFMPEG
378 if(!ffmpeg->
getFrame(I, (
unsigned int)frame_index))
380 vpERROR_TRACE(
"Couldn't find the %ld th frame", frame_index) ;
383 frameCount = frame_index + 1;
384 #elif defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x030000)
385 if(!capture.set(cv::CAP_PROP_POS_FRAMES, frame_index))
387 vpERROR_TRACE(
"Couldn't find the %ld th frame", frame_index) ;
392 frameCount = (long) capture.get(cv::CAP_PROP_POS_FRAMES);
406 #elif defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100)
407 if(!capture.set(CV_CAP_PROP_POS_FRAMES, frame_index))
409 vpERROR_TRACE(
"Couldn't find the %ld th frame", frame_index) ;
414 frameCount = (long) capture.get(CV_CAP_PROP_POS_FRAMES);
440 if (imSequence != NULL)
444 imSequence->
acquire(I, frame_index);
445 frameCount = frame_index + 1;
449 vpERROR_TRACE(
"Couldn't find the %u th frame", frame_index) ;
455 #ifdef VISP_HAVE_FFMPEG
456 if(!ffmpeg->
getFrame(I, (
unsigned int)frame_index))
458 vpERROR_TRACE(
"Couldn't find the %ld th frame", frame_index) ;
461 frameCount = frame_index + 1;
462 #elif VISP_HAVE_OPENCV_VERSION >= 0x030000
463 if(!capture.set(cv::CAP_PROP_POS_FRAMES, frame_index))
465 vpERROR_TRACE(
"Couldn't find the %ld th frame", frame_index) ;
469 frameCount = (long) capture.get(cv::CAP_PROP_POS_FRAMES);
483 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
484 if(!capture.set(CV_CAP_PROP_POS_FRAMES, frame_index))
486 vpERROR_TRACE(
"Couldn't find the %ld th frame", frame_index);
490 frameCount = (long) capture.get(CV_CAP_PROP_POS_FRAMES);
507 vpVideoReader::vpVideoFormatType
508 vpVideoReader::getFormat(
const char *filename)
510 std::string sfilename(filename);
512 std::string ext = vpVideoReader::getExtension(sfilename);
514 if (ext.compare(
".PGM") == 0)
516 else if (ext.compare(
".pgm") == 0)
518 else if (ext.compare(
".PPM") == 0)
520 else if (ext.compare(
".ppm") == 0)
522 else if (ext.compare(
".JPG") == 0)
524 else if (ext.compare(
".jpg") == 0)
526 else if (ext.compare(
".JPEG") == 0)
528 else if (ext.compare(
".jpeg") == 0)
530 else if (ext.compare(
".PNG") == 0)
532 else if (ext.compare(
".png") == 0)
534 else if (ext.compare(
".TIFF") == 0)
536 else if (ext.compare(
".tiff") == 0)
538 else if (ext.compare(
".BMP") == 0)
540 else if (ext.compare(
".bmp") == 0)
542 else if (ext.compare(
".DIB") == 0)
544 else if (ext.compare(
".dib") == 0)
546 else if (ext.compare(
".PBM") == 0)
548 else if (ext.compare(
".pbm") == 0)
550 else if (ext.compare(
".SR") == 0)
552 else if (ext.compare(
".sr") == 0)
554 else if (ext.compare(
".RAS") == 0)
555 return FORMAT_RASTER;
556 else if (ext.compare(
".ras") == 0)
557 return FORMAT_RASTER;
558 else if (ext.compare(
".JP2") == 0)
559 return FORMAT_JPEG2000;
560 else if (ext.compare(
".jp2") == 0)
561 return FORMAT_JPEG2000;
562 else if (ext.compare(
".AVI") == 0)
564 else if (ext.compare(
".avi") == 0)
566 else if (ext.compare(
".MPEG") == 0)
568 else if (ext.compare(
".mpeg") == 0)
570 else if (ext.compare(
".MPG") == 0)
572 else if (ext.compare(
".mpg") == 0)
574 else if (ext.compare(
".MPEG4") == 0)
576 else if (ext.compare(
".mpeg4") == 0)
578 else if (ext.compare(
".MP4") == 0)
580 else if (ext.compare(
".mp4") == 0)
582 else if (ext.compare(
".MOV") == 0)
584 else if (ext.compare(
".mov") == 0)
586 else if (ext.compare(
".OGV") == 0)
588 else if (ext.compare(
".ogv") == 0)
590 else if (ext.compare(
".WMV") == 0)
592 else if (ext.compare(
".wmv") == 0)
594 else if (ext.compare(
".FLV") == 0)
596 else if (ext.compare(
".flv") == 0)
598 else if (ext.compare(
".MKV") == 0)
600 else if (ext.compare(
".mkv") == 0)
603 return FORMAT_UNKNOWN;
607 std::string vpVideoReader::getExtension(
const std::string &filename)
610 size_t dot = filename.find_last_of(
".");
611 std::string ext = filename.substr(dot, filename.size()-1);
619 void vpVideoReader::findLastFrameIndex()
626 if (imSequence != NULL) {
627 if (! lastFrameIndexIsSet) {
628 char name[FILENAME_MAX];
629 long image_number = firstFrame;
633 sprintf(name,fileName,image_number) ;
634 file.open(name, std::ios::in);
635 failed = file.fail();
642 lastFrame = image_number -1;
646 #ifdef VISP_HAVE_FFMPEG
647 else if (ffmpeg != NULL) {
648 if (! lastFrameIndexIsSet) {
652 #elif VISP_HAVE_OPENCV_VERSION >= 0x030000
653 else if (! lastFrameIndexIsSet)
655 lastFrame = (long) capture.get(cv::CAP_PROP_FRAME_COUNT);
663 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
664 else if (! lastFrameIndexIsSet)
666 lastFrame = (long) capture.get(CV_CAP_PROP_FRAME_COUNT);
680 vpVideoReader::findFirstFrameIndex()
682 if (imSequence != NULL)
684 if (! firstFrameIndexIsSet) {
685 char name[FILENAME_MAX];
686 int image_number = 0;
690 sprintf(name, fileName, image_number) ;
691 file.open(name, std::ios::in);
692 failed = file.fail();
693 if (!failed) file.close();
697 firstFrame = image_number - 1;
701 #ifdef VISP_HAVE_FFMPEG
702 else if (ffmpeg != NULL) {
703 if (! firstFrameIndexIsSet) {
704 firstFrame = (long)(0);
707 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
708 else if (! firstFrameIndexIsSet)
710 firstFrame = (long) (0);
722 double framerate = -1.;
724 #ifdef VISP_HAVE_FFMPEG
729 #elif VISP_HAVE_OPENCV_VERSION >= 0x030000
730 framerate = capture.get(cv::CAP_PROP_FPS);
732 if(std::fabs(framerate) <= std::numeric_limits<double>::epsilon())
736 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
737 framerate = capture.get(CV_CAP_PROP_FPS);
739 if(std::fabs(framerate) <= std::numeric_limits<double>::epsilon())
750 bool vpVideoReader::isImageExtensionSupported()
752 return (formatType == FORMAT_PGM ||
753 formatType == FORMAT_PPM ||
754 formatType == FORMAT_JPEG ||
755 formatType == FORMAT_PNG ||
756 formatType == FORMAT_TIFF ||
757 formatType == FORMAT_BMP ||
758 formatType == FORMAT_DIB ||
759 formatType == FORMAT_PBM ||
760 formatType == FORMAT_RASTER ||
761 formatType == FORMAT_JPEG2000);
767 bool vpVideoReader::isVideoExtensionSupported()
769 return (formatType == FORMAT_AVI ||
770 formatType == FORMAT_MPEG ||
771 formatType == FORMAT_MPEG4 ||
772 formatType == FORMAT_MOV ||
773 formatType == FORMAT_OGV ||
774 formatType == FORMAT_WMV ||
775 formatType == FORMAT_FLV ||
776 formatType == FORMAT_MKV);
double getFramerate() const
unsigned int getWidth() const
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
bool getFrame(vpImage< vpRGBa > &I, unsigned int frameNumber)
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.
unsigned long getFrameNumber() const
void open(vpImage< vpRGBa > &I)
bool getFrame(vpImage< vpRGBa > &I, long frame)
unsigned int height
Number of rows in the image.
bool openStream(const char *filename, vpFFMPEGColorType color_type)
void acquire(vpImage< vpRGBa > &I)
This class interfaces the FFmpeg library to enable video stream reading or writing.
void setImageNumber(long number)
void setFileName(const char *filename)
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)
bool acquire(vpImage< vpRGBa > &I)
unsigned int getHeight() const
void acquire(vpImage< unsigned char > &I)
unsigned int width
Number of columns in the image.
void setGenericName(const char *genericName)