48 #include <visp/vpDebug.h>
49 #include <visp/vpVideoReader.h>
60 #ifdef VISP_HAVE_FFMPEG
62 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
65 formatType(FORMAT_UNKNOWN), initFileName(false), isOpen(false), frameCount(0),
66 firstFrame(0), lastFrame(0), firstFrameIndexIsSet(false), lastFrameIndexIsSet(false)
75 if (imSequence != NULL)
79 #ifdef VISP_HAVE_FFMPEG
99 if (!filename || *filename ==
'\0')
105 if (strlen( filename ) >= FILENAME_MAX) {
107 "Not enough memory to intialize the file name"));
110 strcpy(this->fileName,filename);
112 formatType = getFormat(fileName);
114 if (formatType == FORMAT_UNKNOWN) {
150 if (isImageExtensionSupported())
154 if (firstFrameIndexIsSet)
157 else if (isVideoExtensionSupported())
159 #ifdef VISP_HAVE_FFMPEG
164 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
165 capture.open(fileName);
167 if(!capture.isOpened())
176 else if (formatType == FORMAT_UNKNOWN)
182 findFirstFrameIndex();
183 frameCount = firstFrame;
194 findLastFrameIndex();
213 if (isImageExtensionSupported())
217 if (firstFrameIndexIsSet)
220 else if (isVideoExtensionSupported())
222 #ifdef VISP_HAVE_FFMPEG
227 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
228 capture.open(fileName);
230 if(!capture.isOpened())
239 else if (formatType == FORMAT_UNKNOWN)
245 findFirstFrameIndex();
246 frameCount = firstFrame;
257 findLastFrameIndex();
277 if (imSequence != NULL)
282 #ifdef VISP_HAVE_FFMPEG
283 else if (ffmpeg !=NULL)
288 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
292 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
293 frameCount = (long) capture.get(cv::CAP_PROP_POS_FRAMES);
295 frameCount = (long) capture.get(CV_CAP_PROP_POS_FRAMES);
320 if (imSequence != NULL)
325 #ifdef VISP_HAVE_FFMPEG
326 else if (ffmpeg != NULL)
331 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
335 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
336 frameCount = (long) capture.get(cv::CAP_PROP_POS_FRAMES);
338 frameCount = (long) capture.get(CV_CAP_PROP_POS_FRAMES);
364 if (imSequence != NULL)
368 imSequence->
acquire(I, frame_index);
369 frameCount = frame_index + 1;
373 vpERROR_TRACE(
"Couldn't find the %u th frame", frame_index) ;
379 #ifdef VISP_HAVE_FFMPEG
380 if(!ffmpeg->
getFrame(I, (
unsigned int)frame_index))
382 vpERROR_TRACE(
"Couldn't find the %ld th frame", frame_index) ;
385 frameCount = frame_index + 1;
386 #elif defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x030000)
387 if(!capture.set(cv::CAP_PROP_POS_FRAMES, frame_index))
389 vpERROR_TRACE(
"Couldn't find the %ld th frame", frame_index) ;
394 frameCount = (long) capture.get(cv::CAP_PROP_POS_FRAMES);
399 #elif defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100)
400 if(!capture.set(CV_CAP_PROP_POS_FRAMES, frame_index))
402 vpERROR_TRACE(
"Couldn't find the %ld th frame", frame_index) ;
407 frameCount = (long) capture.get(CV_CAP_PROP_POS_FRAMES);
433 if (imSequence != NULL)
437 imSequence->
acquire(I, frame_index);
438 frameCount = frame_index + 1;
442 vpERROR_TRACE(
"Couldn't find the %u th frame", frame_index) ;
448 #ifdef VISP_HAVE_FFMPEG
449 if(!ffmpeg->
getFrame(I, (
unsigned int)frame_index))
451 vpERROR_TRACE(
"Couldn't find the %ld th frame", frame_index) ;
454 frameCount = frame_index + 1;
455 #elif VISP_HAVE_OPENCV_VERSION >= 0x030000
456 if(!capture.set(cv::CAP_PROP_POS_FRAMES, frame_index))
458 vpERROR_TRACE(
"Couldn't find the %ld th frame", frame_index) ;
462 frameCount = (long) capture.get(cv::CAP_PROP_POS_FRAMES);
467 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
468 if(!capture.set(CV_CAP_PROP_POS_FRAMES, frame_index))
470 vpERROR_TRACE(
"Couldn't find the %ld th frame", frame_index);
474 frameCount = (long) capture.get(CV_CAP_PROP_POS_FRAMES);
491 vpVideoReader::vpVideoFormatType
492 vpVideoReader::getFormat(
const char *filename)
494 std::string sfilename(filename);
496 std::string ext = vpVideoReader::getExtension(sfilename);
498 if (ext.compare(
".PGM") == 0)
500 else if (ext.compare(
".pgm") == 0)
502 else if (ext.compare(
".PPM") == 0)
504 else if (ext.compare(
".ppm") == 0)
506 else if (ext.compare(
".JPG") == 0)
508 else if (ext.compare(
".jpg") == 0)
510 else if (ext.compare(
".JPEG") == 0)
512 else if (ext.compare(
".jpeg") == 0)
514 else if (ext.compare(
".PNG") == 0)
516 else if (ext.compare(
".png") == 0)
518 else if (ext.compare(
".TIFF") == 0)
520 else if (ext.compare(
".tiff") == 0)
522 else if (ext.compare(
".BMP") == 0)
524 else if (ext.compare(
".bmp") == 0)
526 else if (ext.compare(
".DIB") == 0)
528 else if (ext.compare(
".dib") == 0)
530 else if (ext.compare(
".PBM") == 0)
532 else if (ext.compare(
".PBM") == 0)
534 else if (ext.compare(
".SR") == 0)
536 else if (ext.compare(
".sr") == 0)
538 else if (ext.compare(
".RAS") == 0)
539 return FORMAT_RASTER;
540 else if (ext.compare(
".ras") == 0)
541 return FORMAT_RASTER;
542 else if (ext.compare(
".JP2") == 0)
543 return FORMAT_JPEG2000;
544 else if (ext.compare(
".jp2") == 0)
545 return FORMAT_JPEG2000;
546 else if (ext.compare(
".AVI") == 0)
548 else if (ext.compare(
".avi") == 0)
550 else if (ext.compare(
".MPEG") == 0)
552 else if (ext.compare(
".mpeg") == 0)
554 else if (ext.compare(
".MPG") == 0)
556 else if (ext.compare(
".mpg") == 0)
558 else if (ext.compare(
".MPEG4") == 0)
560 else if (ext.compare(
".mpeg4") == 0)
562 else if (ext.compare(
".MP4") == 0)
564 else if (ext.compare(
".mp4") == 0)
566 else if (ext.compare(
".MOV") == 0)
568 else if (ext.compare(
".mov") == 0)
570 else if (ext.compare(
".OGV") == 0)
572 else if (ext.compare(
".ogv") == 0)
574 else if (ext.compare(
".WMV") == 0)
576 else if (ext.compare(
".wmv") == 0)
578 else if (ext.compare(
".FLV") == 0)
580 else if (ext.compare(
".flv") == 0)
582 else if (ext.compare(
".MKV") == 0)
584 else if (ext.compare(
".mkv") == 0)
587 return FORMAT_UNKNOWN;
591 std::string vpVideoReader::getExtension(
const std::string &filename)
594 size_t dot = filename.find_last_of(
".");
595 std::string ext = filename.substr(dot, filename.size()-1);
604 vpVideoReader::findLastFrameIndex()
612 if (imSequence != NULL)
614 if (! lastFrameIndexIsSet) {
615 char name[FILENAME_MAX];
616 int image_number = firstFrame;
621 sprintf(name,fileName,image_number) ;
622 file.open(name, std::ios::in);
623 failed = file.fail();
630 lastFrame = image_number;
634 #ifdef VISP_HAVE_FFMPEG
635 else if (ffmpeg != NULL) {
636 if (! lastFrameIndexIsSet) {
640 #elif VISP_HAVE_OPENCV_VERSION >= 0x030000
641 else if (! lastFrameIndexIsSet)
643 lastFrame = (long) capture.get(cv::CAP_PROP_FRAME_COUNT);
651 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
652 else if (! lastFrameIndexIsSet)
654 lastFrame = (long) capture.get(CV_CAP_PROP_FRAME_COUNT);
668 vpVideoReader::findFirstFrameIndex()
670 if (imSequence != NULL)
672 if (! firstFrameIndexIsSet) {
673 char name[FILENAME_MAX];
674 int image_number = 0;
678 sprintf(name, fileName, image_number) ;
679 file.open(name, std::ios::in);
680 failed = file.fail();
681 if (!failed) file.close();
685 firstFrame = image_number - 1;
689 #ifdef VISP_HAVE_FFMPEG
690 else if (ffmpeg != NULL) {
691 if (! firstFrameIndexIsSet) {
692 firstFrame = (long)(0);
695 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
696 else if (! firstFrameIndexIsSet)
698 firstFrame = (long) (0);
710 double framerate = -1.;
712 #ifdef VISP_HAVE_FFMPEG
717 #elif VISP_HAVE_OPENCV_VERSION >= 0x030000
718 framerate = capture.get(cv::CAP_PROP_FPS);
720 if(std::fabs(framerate) <= std::numeric_limits<double>::epsilon())
724 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
725 framerate = capture.get(CV_CAP_PROP_FPS);
727 if(std::fabs(framerate) <= std::numeric_limits<double>::epsilon())
738 bool vpVideoReader::isImageExtensionSupported()
740 return (formatType == FORMAT_PGM ||
741 formatType == FORMAT_PPM ||
742 formatType == FORMAT_JPEG ||
743 formatType == FORMAT_PNG ||
744 formatType == FORMAT_TIFF ||
745 formatType == FORMAT_BMP ||
746 formatType == FORMAT_DIB ||
747 formatType == FORMAT_PBM ||
748 formatType == FORMAT_RASTER ||
749 formatType == FORMAT_JPEG2000);
755 bool vpVideoReader::isVideoExtensionSupported()
757 return (formatType == FORMAT_AVI ||
758 formatType == FORMAT_MPEG ||
759 formatType == FORMAT_MPEG4 ||
760 formatType == FORMAT_MOV ||
761 formatType == FORMAT_OGV ||
762 formatType == FORMAT_WMV ||
763 formatType == FORMAT_FLV ||
764 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)
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)
Class to grab (ie. read) images from the disk.
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)