OpenCV的HighGUI提供了视频和摄像头的直接打开。那么它是如何实现的了?这里进行初步分析。
/**
* Videoreader dispatching method: it tries to find the first
* API that can access a given filename.
*/
CV_IMPL CvCapture * cvCreateFileCaptureWithPreference (const char * filename, int apiPreference)
{
CvCapture * result = 0;
switch(apiPreference) {
default:
// user specified an API we do not know
// bail out to let the user know that it is not available
if (apiPreference) break;
#ifdef HAVE_FFMPEG
case CV_CAP_FFMPEG:
TRY_OPEN(result, cvCreateFileCapture_FFMPEG_proxy (filename))
if (apiPreference) break;
#endif
case CV_CAP_VFW:
#ifdef HAVE_VFW
TRY_OPEN(result, cvCreateFileCapture_VFW (filename))
#endif
#if defined HAVE_LIBV4L || defined HAVE_CAMV4L || defined HAVE_CAMV4L2 || defined HAVE_VIDEOIO
TRY_OPEN(result, cvCreateCameraCapture_V4L(filename))
#endif
if (apiPreference) break;
case CV_CAP_MSMF:
#ifdef HAVE_MSMF
TRY_OPEN(result, cvCreateFileCapture_MSMF (filename))
#endif
#ifdef HAVE_XINE
TRY_OPEN(result, cvCreateFileCapture_XINE (filename))
#endif
if (apiPreference) break;
#ifdef HAVE_GSTREAMER
case CV_CAP_GSTREAMER:
TRY_OPEN(result, cvCreateCapture_GStreamer (CV_CAP_GSTREAMER_FILE, filename))
if (apiPreference) break;
#endif
#if defined(HAVE_QUICKTIME) || defined(HAVE_QTKIT)
case CV_CAP_QT:
TRY_OPEN(result, cvCreateFileCapture_QT (filename))
if (apiPreference) break;
#endif
#ifdef HAVE_AVFOUNDATION
case CV_CAP_AVFOUNDATION:
TRY_OPEN(result, cvCreateFileCapture_AVFoundation (filename))
if (apiPreference) break;
#endif
#ifdef HAVE_OPENNI
case CV_CAP_OPENNI:
TRY_OPEN(result, cvCreateFileCapture_OpenNI (filename))
if (apiPreference) break;
#endif
#ifdef HAVE_OPENNI2
case CV_CAP_OPENNI2:
TRY_OPEN(result, cvCreateFileCapture_OpenNI2 (filename))
if (apiPreference) break;
#endif
case CV_CAP_IMAGES:
TRY_OPEN(result, cvCreateFileCapture_Images (filename))
}
return result;
}
* Videoreader dispatching method: it tries to find the first
* API that can access a given filename.
*/
CV_IMPL CvCapture * cvCreateFileCaptureWithPreference (const char * filename, int apiPreference)
{
CvCapture * result = 0;
switch(apiPreference) {
default:
// user specified an API we do not know
// bail out to let the user know that it is not available
if (apiPreference) break;
#ifdef HAVE_FFMPEG
case CV_CAP_FFMPEG:
TRY_OPEN(result, cvCreateFileCapture_FFMPEG_proxy (filename))
if (apiPreference) break;
#endif
case CV_CAP_VFW:
#ifdef HAVE_VFW
TRY_OPEN(result, cvCreateFileCapture_VFW (filename))
#endif
#if defined HAVE_LIBV4L || defined HAVE_CAMV4L || defined HAVE_CAMV4L2 || defined HAVE_VIDEOIO
TRY_OPEN(result, cvCreateCameraCapture_V4L(filename))
#endif
if (apiPreference) break;
case CV_CAP_MSMF:
#ifdef HAVE_MSMF
TRY_OPEN(result, cvCreateFileCapture_MSMF (filename))
#endif
#ifdef HAVE_XINE
TRY_OPEN(result, cvCreateFileCapture_XINE (filename))
#endif
if (apiPreference) break;
#ifdef HAVE_GSTREAMER
case CV_CAP_GSTREAMER:
TRY_OPEN(result, cvCreateCapture_GStreamer (CV_CAP_GSTREAMER_FILE, filename))
if (apiPreference) break;
#endif
#if defined(HAVE_QUICKTIME) || defined(HAVE_QTKIT)
case CV_CAP_QT:
TRY_OPEN(result, cvCreateFileCapture_QT (filename))
if (apiPreference) break;
#endif
#ifdef HAVE_AVFOUNDATION
case CV_CAP_AVFOUNDATION:
TRY_OPEN(result, cvCreateFileCapture_AVFoundation (filename))
if (apiPreference) break;
#endif
#ifdef HAVE_OPENNI
case CV_CAP_OPENNI:
TRY_OPEN(result, cvCreateFileCapture_OpenNI (filename))
if (apiPreference) break;
#endif
#ifdef HAVE_OPENNI2
case CV_CAP_OPENNI2:
TRY_OPEN(result, cvCreateFileCapture_OpenNI2 (filename))
if (apiPreference) break;
#endif
case CV_CAP_IMAGES:
TRY_OPEN(result, cvCreateFileCapture_Images (filename))
}
return result;
}
具体跟入源代码中的cvCreateFileCaptureWithPreference 函数。可以看到,它提供了这些选项(qt那个除外)
HAVE_FFMPEG
CV_CAP_VFW
HAVE_MSMF
HAVE_XINE
HAVE_GSTREAMER
HAVE_AVFOUNDATION
HAVE_OPENNI
CV_CAP_IMAGES
那么他们分别都是些什么东西了?
FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。采用LGPL或GPL许可证。它提供了录制、转换以及流化音视频的完整解决方案。它包含了非常先进的音频/视频编解码库libavcodec,为了保证高可移植性和编解码质量,libavcodec里很多code都是从头开发的。
VFW(Video for Windows)是Microsoft推出的关于数字视频的一个软件开发包,VFW的核心是AVI文件标准。AVI(Audio Video Interleave)文件中的音、视频数据帧交错存放。
It also decodes multimedia files like AVI, MOV, WMV, and MP3 from local disk
drives, and displays multimedia streamed over the Internet.
It interprets many of the most common multimedia formats available – and some
of the most uncommon formats, too.
AVFoundation是一个OC媒体数据的高级框架。AVFoundation的构建考虑到了目前的硬件环境和应用程序,其设计过程高度依赖多线程机制。充分利用了多核硬件的优势并大量使用block和GCD机制,将复杂的计算机进程放到了后台线程运行。会自动提供硬件加速操作,确保在大部分设备上应用程序能以最佳性能运行。该框架就是针对64位处理器设计的,可以发挥64位处理器的所有优势。
可以看到,OpenCV对于多平台支持,提供了不同的基础库。我认为,在windows下,应该选择的就是
VFW了。那么一般意义上认为VFW是比较老旧的技术了,在实际开发的时候,能感到一些性能的不足。
感谢阅读至此,希望有所收获。