利用ViSP也可以从输入视频流获取图像。支持格式包括avi,mp4,mov,ogv,flv等常见视频格式。下面创建的这个程序要求安装第三方库OpenCV。
1 #include <visp/vpDisplayGDI.h> 2 #include <visp/vpDisplayOpenCV.h> 3 #include <visp/vpDisplayX.h> 4 #include <visp/vpTime.h>//允许使用其下的计时函数 5 #include <visp/vpVideoReader.h>//允许读取视频流 6 int main(int argc, char** argv) 7 { 8 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100) || defined(VISP_HAVE_FFMPEG) 9 try { 10 std::string videoname = "video.mpg"; 11 for (int i=0; i<argc; i++) { 12 if (std::string(argv[i]) == "--name") 13 videoname = std::string(argv[i+1]); 14 else if (std::string(argv[i]) == "--help") { 15 std::cout << " Usage: " << argv[0] << " [--name <video name>] [--help] " << std::endl; 16 return 0; 17 } 18 } 19 vpImage<unsigned char> I; 20 vpVideoReader g;//创建一个视频读取对象 21 g.setFileName(videoname);//设置视频的文件名 22 g.open(I);//初始化帧读取工具 23 std::cout << "video name: " << videoname << std::endl; 24 std::cout << "video framerate: " << g.getFramerate() << "Hz" << std::endl; 25 std::cout << "video dimension: " << I.getWidth() << " " << I.getHeight() << std::endl; 26 #ifdef VISP_HAVE_X11 27 vpDisplayX d(I); 28 #elif defined(VISP_HAVE_GDI) 29 vpDisplayGDI d(I); 30 #elif defined(VISP_HAVE_OPENCV) 31 vpDisplayOpenCV d(I); 32 #else 33 std::cout << "No image viewer is available..." << std::endl; 34 #endif 35 vpDisplay::setTitle(I, "Video reader"); 36 while (! g.end() ) { 37 double t = vpTime::measureTimeMs(); 38 g.acquire(I); 39 vpDisplay::display(I); 40 vpDisplay::flush(I); 41 if (vpDisplay::getClick(I, false)) break; 42 vpTime::wait(t, 1000. / g.getFramerate()); 43 } 44 } 45 catch(vpException e) { 46 std::cout << e.getMessage() << std::endl; 47 } 48 #else 49 (void)argc; 50 (void)argv; 51 std::cout << "Install OpenCV or ffmpeg and rebuild ViSP to use this example." << std::endl; 52 #endif 53 }