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
62 formatType(FORMAT_UNKNOWN), initFileName(false), isOpen(false), frameCount(0), firstFrame(0), lastFrame(0),
63 firstFrameIndexIsSet(false), lastFrameIndexIsSet(false), frameStep(1), frameRate(0.)
72 if (imSequence != NULL) {
94 if ((!filename) || (*filename ==
'\0')) {
99 if (strlen(filename) >= FILENAME_MAX) {
103 strcpy(this->fileName, filename);
105 formatType = getFormat(fileName);
107 if (formatType == FORMAT_UNKNOWN) {
112 if (isImageExtensionSupported()) {
114 if (!checkImageNameFormat(format)) {
142 void vpVideoReader::getProperties()
148 if (isImageExtensionSupported()) {
151 imSequence->
setStep(frameStep);
152 if (firstFrameIndexIsSet) {
156 }
else if (isVideoExtensionSupported()) {
157 #if VISP_HAVE_OPENCV_VERSION >= 0x020100 158 capture.open(fileName);
160 if (!capture.isOpened()) {
163 #if VISP_HAVE_OPENCV_VERSION >= 0x030000 164 width = (
unsigned int)capture.get(cv::CAP_PROP_FRAME_WIDTH);
165 height = (
unsigned int)capture.get(cv::CAP_PROP_FRAME_HEIGHT);
166 frameRate = (double)capture.get(cv::CAP_PROP_FPS);
168 width = (
unsigned int)capture.get(CV_CAP_PROP_FRAME_WIDTH);
169 height = (
unsigned int)capture.get(CV_CAP_PROP_FRAME_HEIGHT);
170 frameRate = capture.get(CV_CAP_PROP_FPS);
175 "3rd >= 2.1.0 party libraries."));
177 }
else if (formatType == FORMAT_UNKNOWN) {
181 "not correspond to a readable " 182 "format supported by ViSP."));
185 findFirstFrameIndex();
187 findLastFrameIndex();
201 frameCount = firstFrame;
208 frameCount = firstFrame;
210 if (isVideoExtensionSupported()) {
211 #if VISP_HAVE_OPENCV_VERSION >= 0x020100 213 #if VISP_HAVE_OPENCV_VERSION >= 0x030000 214 capture.set(cv::CAP_PROP_POS_FRAMES, firstFrame - 1);
216 capture.set(CV_CAP_PROP_POS_FRAMES, firstFrame - 1);
234 frameCount = firstFrame;
241 frameCount = firstFrame;
243 if (isVideoExtensionSupported()) {
244 #if VISP_HAVE_OPENCV_VERSION >= 0x020100 246 #if VISP_HAVE_OPENCV_VERSION >= 0x030000 247 capture.set(cv::CAP_PROP_POS_FRAMES, firstFrame - 1);
249 capture.set(CV_CAP_PROP_POS_FRAMES, firstFrame - 1);
273 if (imSequence != NULL) {
274 imSequence->
setStep(frameStep);
277 if (frameCount + frameStep > lastFrame) {
279 }
else if (frameCount + frameStep < firstFrame) {
283 #if VISP_HAVE_OPENCV_VERSION >= 0x020100 286 if (frameStep == 1) {
289 #if VISP_HAVE_OPENCV_VERSION >= 0x030000 290 frameCount = (long)capture.get(cv::CAP_PROP_POS_FRAMES);
292 if (frameCount + frameStep <= lastFrame) {
293 capture.set(cv::CAP_PROP_POS_FRAMES, frameCount + frameStep - 1);
295 capture.set(cv::CAP_PROP_POS_FRAMES, frameCount - 1);
297 }
else if (frameStep < 0) {
298 if (frameCount + frameStep >= firstFrame) {
299 capture.set(cv::CAP_PROP_POS_FRAMES, frameCount + frameStep - 1);
301 capture.set(cv::CAP_PROP_POS_FRAMES, firstFrame - 1);
305 frameCount = (long)capture.get(CV_CAP_PROP_POS_FRAMES);
307 if (frameCount + frameStep <= lastFrame) {
308 capture.set(CV_CAP_PROP_POS_FRAMES, frameCount + frameStep - 1);
310 capture.set(CV_CAP_PROP_POS_FRAMES, frameCount - 1);
312 }
else if (frameStep < 0) {
313 if (frameCount + frameStep >= firstFrame) {
314 capture.set(CV_CAP_PROP_POS_FRAMES, frameCount + frameStep - 1);
316 capture.set(CV_CAP_PROP_POS_FRAMES, firstFrame - 1);
344 if (imSequence != NULL) {
345 imSequence->
setStep(frameStep);
348 if (frameCount + frameStep > lastFrame) {
350 }
else if (frameCount + frameStep < firstFrame) {
354 #if VISP_HAVE_OPENCV_VERSION >= 0x020100 357 if (frameStep == 1) {
360 #if VISP_HAVE_OPENCV_VERSION >= 0x030000 361 frameCount = (long)capture.get(cv::CAP_PROP_POS_FRAMES);
363 if (frameCount + frameStep <= lastFrame) {
364 capture.set(cv::CAP_PROP_POS_FRAMES, frameCount + frameStep - 1);
366 capture.set(cv::CAP_PROP_POS_FRAMES, frameCount - 1);
368 }
else if (frameStep < 0) {
369 if (frameCount + frameStep >= firstFrame) {
370 capture.set(cv::CAP_PROP_POS_FRAMES, frameCount + frameStep - 1);
372 capture.set(cv::CAP_PROP_POS_FRAMES, firstFrame - 1);
376 frameCount = (long)capture.get(CV_CAP_PROP_POS_FRAMES);
378 if (frameCount + frameStep <= lastFrame) {
379 capture.set(CV_CAP_PROP_POS_FRAMES, frameCount + frameStep - 1);
381 capture.set(CV_CAP_PROP_POS_FRAMES, frameCount - 1);
383 }
else if (frameStep < 0) {
384 if (frameCount + frameStep >= firstFrame) {
385 capture.set(CV_CAP_PROP_POS_FRAMES, frameCount + frameStep - 1);
387 capture.set(CV_CAP_PROP_POS_FRAMES, firstFrame - 1);
416 if (imSequence != NULL) {
418 imSequence->
acquire(I, frame_index);
423 if (frameCount + frameStep > lastFrame) {
425 }
else if (frameCount + frameStep < firstFrame) {
433 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x030000) 434 if (!capture.set(cv::CAP_PROP_POS_FRAMES, frame_index)) {
435 vpERROR_TRACE(
"Couldn't find the %ld th frame", frame_index);
440 frameCount = frame_index + frameStep;
441 capture.set(cv::CAP_PROP_POS_FRAMES, frameCount);
453 #elif defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100) 454 if (!capture.set(CV_CAP_PROP_POS_FRAMES, frame_index)) {
455 vpERROR_TRACE(
"Couldn't find the %ld th frame", frame_index);
460 frameCount = frame_index + frameStep;
461 capture.set(CV_CAP_PROP_POS_FRAMES, frameCount);
486 if (imSequence != NULL) {
488 imSequence->
acquire(I, frame_index);
493 if (frameCount + frameStep > lastFrame) {
495 }
else if (frameCount + frameStep < firstFrame) {
503 #if VISP_HAVE_OPENCV_VERSION >= 0x030000 504 if (!capture.set(cv::CAP_PROP_POS_FRAMES, frame_index)) {
505 vpERROR_TRACE(
"Couldn't find the %ld th frame", frame_index);
521 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100 522 if (!capture.set(CV_CAP_PROP_POS_FRAMES, frame_index)) {
528 frameCount = (long)capture.get(CV_CAP_PROP_POS_FRAMES);
530 frameCount += frameStep - 1;
531 capture.set(CV_CAP_PROP_POS_FRAMES, frameCount);
532 }
else if (frameStep < -1) {
533 frameCount += frameStep - 1;
534 capture.set(CV_CAP_PROP_POS_FRAMES, frameCount);
550 vpVideoReader::vpVideoFormatType vpVideoReader::getFormat(
const char *filename)
552 std::string sfilename(filename);
554 std::string ext = vpVideoReader::getExtension(sfilename);
556 if (ext.compare(
".PGM") == 0)
558 else if (ext.compare(
".pgm") == 0)
560 else if (ext.compare(
".PPM") == 0)
562 else if (ext.compare(
".ppm") == 0)
564 else if (ext.compare(
".JPG") == 0)
566 else if (ext.compare(
".jpg") == 0)
568 else if (ext.compare(
".JPEG") == 0)
570 else if (ext.compare(
".jpeg") == 0)
572 else if (ext.compare(
".PNG") == 0)
574 else if (ext.compare(
".png") == 0)
576 else if (ext.compare(
".TIFF") == 0)
578 else if (ext.compare(
".tiff") == 0)
580 else if (ext.compare(
".BMP") == 0)
582 else if (ext.compare(
".bmp") == 0)
584 else if (ext.compare(
".DIB") == 0)
586 else if (ext.compare(
".dib") == 0)
588 else if (ext.compare(
".PBM") == 0)
590 else if (ext.compare(
".pbm") == 0)
592 else if (ext.compare(
".SR") == 0)
594 else if (ext.compare(
".sr") == 0)
596 else if (ext.compare(
".RAS") == 0)
597 return FORMAT_RASTER;
598 else if (ext.compare(
".ras") == 0)
599 return FORMAT_RASTER;
600 else if (ext.compare(
".JP2") == 0)
601 return FORMAT_JPEG2000;
602 else if (ext.compare(
".jp2") == 0)
603 return FORMAT_JPEG2000;
604 else if (ext.compare(
".AVI") == 0)
606 else if (ext.compare(
".avi") == 0)
608 else if (ext.compare(
".MPEG") == 0)
610 else if (ext.compare(
".mpeg") == 0)
612 else if (ext.compare(
".MPG") == 0)
614 else if (ext.compare(
".mpg") == 0)
616 else if (ext.compare(
".MPEG4") == 0)
618 else if (ext.compare(
".mpeg4") == 0)
620 else if (ext.compare(
".MP4") == 0)
622 else if (ext.compare(
".mp4") == 0)
624 else if (ext.compare(
".MOV") == 0)
626 else if (ext.compare(
".mov") == 0)
628 else if (ext.compare(
".OGV") == 0)
630 else if (ext.compare(
".ogv") == 0)
632 else if (ext.compare(
".WMV") == 0)
634 else if (ext.compare(
".wmv") == 0)
636 else if (ext.compare(
".FLV") == 0)
638 else if (ext.compare(
".flv") == 0)
640 else if (ext.compare(
".MKV") == 0)
642 else if (ext.compare(
".mkv") == 0)
645 return FORMAT_UNKNOWN;
649 std::string vpVideoReader::getExtension(
const std::string &filename)
652 size_t dot = filename.find_last_of(
".");
653 std::string ext = filename.substr(dot, filename.size() - 1);
660 void vpVideoReader::findLastFrameIndex()
667 if (imSequence != NULL) {
668 if (!lastFrameIndexIsSet) {
676 for (
size_t i = 0; i < files.size(); i++) {
679 long imageIndex = extractImageIndex(files[i], imageNameFormat);
680 if ((imageIndex != -1) && (imageIndex > lastFrame)) {
681 lastFrame = imageIndex;
687 #if VISP_HAVE_OPENCV_VERSION >= 0x030000 688 else if (!lastFrameIndexIsSet) {
689 lastFrame = (long)capture.get(cv::CAP_PROP_FRAME_COUNT);
698 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100 699 else if (!lastFrameIndexIsSet) {
700 lastFrame = (long)capture.get(CV_CAP_PROP_FRAME_COUNT);
715 void vpVideoReader::findFirstFrameIndex()
717 if (imSequence != NULL) {
718 if (!firstFrameIndexIsSet) {
726 for (
size_t i = 0; i < files.size(); i++) {
729 long imageIndex = extractImageIndex(files[i], imageNameFormat);
730 if ((imageIndex != -1) && (imageIndex < firstFrame || firstFrame == -1)) {
731 firstFrame = imageIndex;
737 #if VISP_HAVE_OPENCV_VERSION >= 0x020100 738 else if (!firstFrameIndexIsSet) {
747 bool vpVideoReader::isImageExtensionSupported()
749 return (formatType == FORMAT_PGM || formatType == FORMAT_PPM || formatType == FORMAT_JPEG ||
750 formatType == FORMAT_PNG || formatType == FORMAT_TIFF || formatType == FORMAT_BMP ||
751 formatType == FORMAT_DIB || formatType == FORMAT_PBM || formatType == FORMAT_RASTER ||
752 formatType == FORMAT_JPEG2000);
758 bool vpVideoReader::isVideoExtensionSupported()
760 return (formatType == FORMAT_AVI || formatType == FORMAT_MPEG || formatType == FORMAT_MPEG4 ||
761 formatType == FORMAT_MOV || formatType == FORMAT_OGV || formatType == FORMAT_WMV ||
762 formatType == FORMAT_FLV || formatType == FORMAT_MKV);
836 long vpVideoReader::extractImageIndex(
const std::string &imageName,
const std::string &format)
838 size_t indexBegin = format.find_last_of(
'%');
839 size_t indexEnd = format.find_first_of(
'd', indexBegin);
840 size_t suffixLength = format.length() - indexEnd - 1;
843 if (imageName.length() <= suffixLength + indexBegin) {
846 size_t indexLength = imageName.length() - suffixLength - indexBegin;
847 std::string indexSubstr = imageName.substr(indexBegin, indexLength);
848 std::istringstream ss(indexSubstr);
851 if (ss.fail() || index < 0 || !ss.eof()) {
856 char nameByFormat[FILENAME_MAX];
857 sprintf(nameByFormat, format.c_str(), index);
858 if (std::string(nameByFormat) != imageName) {
868 bool vpVideoReader::checkImageNameFormat(
const std::string &format)
870 size_t indexBegin = format.find_last_of(
'%');
871 size_t indexEnd = format.find_first_of(
'd', indexBegin);
872 if (indexBegin == std::string::npos || indexEnd == std::string::npos) {
875 for (
size_t i = indexBegin + 1; i < indexEnd; i++) {
876 if (!std::isdigit(format[i])) {
Used to indicate that a value is not in the allowed range.
void setGenericName(const std::string &genericName)
unsigned int getWidth() const
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)
unsigned int height
Number of rows in the image.
void acquire(vpImage< vpRGBa > &I)
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)
Used to indicate that a parameter is not initialized.
unsigned int getHeight() const
void acquire(vpImage< unsigned char > &I)
unsigned int width
Number of columns in the image.