由于openCV2.2.0源文件很庞大,这里我只分析openCV2.2.0文件组织结构的各个模块的include文件(重点分析各个模块下引用的算法和实现的功能),而不是src文件。这里分析各个模块有助于更好的从整体把握和理解openCV2.2.0。这里只是自己做草稿部分,便于以后修改查找。有理解不对的,希望大家指出。--疯子_007
首先分析的是highgui目录下的highgui_c.h和highgui.cpp文件:
highgui_c.h分三部分:基本的GUI函数库,视频接口,和过时的函数库。基本的GUI函数库包括:创建窗体,设置窗体属性,获取窗体属性,在窗体内显示图片,改变窗体大小,移动窗体,销毁窗体(包括销毁所有窗体);获取给定句柄的窗体名,创建和显示滚动条,恢复和设置滚动条位置;鼠标回调事件,设置鼠标事件回调,枚举鼠标类型;载入图片,保存图片,改变图片结构;按键等待。视频接口包括:定义摄像头结构,通过视频文件获取帧,从摄像头获取视频文件,帧处理的特殊函数(cvRetrieveFrame),抓取帧,释放视频文件;恢复或者设置摄像头属性,返回摄像头类型;定义写视频指针结构体,四色编码,打开编码属性对话框,初始化写视频指针,将帧流写入写视频指针,释放写视频指针。过时的函数库。将一些功能相同的函数,重新命名,保持旧代码的连接性。
highgui.cpp在CV命名空间枚举相应的一些函数和定义了VideoCapture,VideoWriter类,采用面向对象的思想进行编程,更容易理解和整体把握。
详细分析如下:
.
/highgui/include/opencv2/highgui/highgui_c.h
基本GUI函数:
支持QT模块一些函数(省略)
创建窗体:CVAPI(int) cvNamedWindow( const char* name, int flags CV_DEFAULT(CV_WINDOW_AUTOSIZE) );
设置窗体属性:CVAPI(void) cvSetWindowProperty(const char* name, int prop_id, double prop_value);
获取窗体属性:cvGetWindowProperty(const char* name, int prop_id);
在窗体内显示图片:CVAPI(void) cvShowImage( const char* name, const CvArr* image );
改变窗体大小:CVAPI(void) cvResizeWindow( const char* name, int width, int height );
移动窗体大小:CVAPI(void) cvMoveWindow( const char* name, int x, int y );
销毁窗体(包括连接窗体的滚动条):CVAPI(void) cvDestroyWindow( const char* name );
销毁所有窗体:CVAPI(void) cvDestroyAllWindows(void);
获取该窗体句柄(命令行形式获取HWWD,图形窗口形式获取widget):CVAPI(void*) cvGetWindowHandle( const char* name );
获取给定句柄的窗体名:CVAPI(const char*) cvGetWindowName( void* window_handle );
位置回调定义1:
typedef void (CV_CDECL *CvTrackbarCallback)(int pos);
在给定的窗体创建和显示滚动条1:
CVAPI(int) cvCreateTrackbar( const char* trackbar_name, const char* window_name, int* value, int count, CvTrackbarCallback on_change CV_DEFAULT(NULL));
位置回调定义2:
typedef void (CV_CDECL *CvTrackbarCallback2)(int pos, void* userdata);
在给定的窗体创建和显示滚动条2:
CVAPI(int) cvCreateTrackbar2( const char* trackbar_name, const char* window_name,int* value, int count, CvTrackbarCallback2 on_change, void* userdata CV_DEFAULT(0));
恢复或者设置滚动条位置:CVAPI(int) cvGetTrackbarPos( const char* trackbar_name, const char* window_name );
CVAPI(void) cvSetTrackbarPos( const char* trackbar_name, const char* window_name, int pos );
枚举鼠标事件
enum
{
CV_EVENT_MOUSEMOVE =0,// 鼠标移动
CV_EVENT_LBUTTONDOWN =1,// 鼠标左击
CV_EVENT_RBUTTONDOWN =2,// 鼠标右击
CV_EVENT_MBUTTONDOWN =3,// 鼠标中键单价
CV_EVENT_LBUTTONUP =4,//
CV_EVENT_RBUTTONUP =5,//
CV_EVENT_MBUTTONUP =6,//
CV_EVENT_LBUTTONDBLCLK =7,// 鼠标左键双击
CV_EVENT_RBUTTONDBLCLK =8,// 鼠标右键双击
CV_EVENT_MBUTTONDBLCLK =9 // 鼠标中键双击
};
enum
{
CV_EVENT_FLAG_LBUTTON =1,
CV_EVENT_FLAG_RBUTTON =2,
CV_EVENT_FLAG_MBUTTON =4,
CV_EVENT_FLAG_CTRLKEY =8,
CV_EVENT_FLAG_SHIFTKEY =16,
CV_EVENT_FLAG_ALTKEY =32
};
鼠标回调事件定义
typedef void (CV_CDECL *CvMouseCallback )(int event, int x, int y, int flags, void* param);
设置鼠标事件回调:CVAPI(void) cvSetMouseCallback( const char* window_name, CvMouseCallback on_mouse, void* param CV_DEFAULT(NULL));
enum
{
CV_LOAD_IMAGE_UNCHANGED =-1,
CV_LOAD_IMAGE_GRAYSCALE =0,
CV_LOAD_IMAGE_COLOR =1,
CV_LOAD_IMAGE_ANYDEPTH =2,
CV_LOAD_IMAGE_ANYCOLOR =4
};
从文件中调入图片(iscolor 默认参数是CV_LOAD_IMAGE_UNCHANGED):
CVAPI(IplImage*) cvLoadImage( const char* filename, int iscolor CV_DEFAULT(CV_LOAD_IMAGE_COLOR));
CVAPI(CvMat*) cvLoadImageM( const char* filename, int iscolor CV_DEFAULT(CV_LOAD_IMAGE_COLOR));
enum
{
CV_IMWRITE_JPEG_QUALITY =1,
CV_IMWRITE_PNG_COMPRESSION =16,
CV_IMWRITE_PXM_BINARY =32
};
保存图片:
CVAPI(int) cvSaveImage( const char* filename, const CvArr* image,const int* params CV_DEFAULT(0) );
对存储在缓冲中的图进行解码:CVAPI(IplImage*) cvDecodeImage( const CvMat* buf, int iscolor CV_DEFAULT(CV_LOAD_IMAGE_COLOR));
CVAPI(CvMat*) cvDecodeImageM( const CvMat* buf, int iscolor CV_DEFAULT(CV_LOAD_IMAGE_COLOR));
对图像进行编码,并将结果保存到单通道的8UC1矩阵中:CVAPI(CvMat*) cvEncodeImage( const char* ext, const CvArr* image, const int* params CV_DEFAULT(0) );
enum
{
CV_CVTIMG_FLIP =1,
CV_CVTIMG_SWAP_RB =2
};
改变图像结构:CVAPI(void) cvConvertImage( const CvArr* src, CvArr* dst, int flags CV_DEFAULT(0));
按键等待:CVAPI(int) cvWaitKey(int delay CV_DEFAULT(0));
视频文件和摄像头接口
定义摄像头结构体:typedef struct CvCapture CvCapture;
通过视频文件获取帧:CVAPI(CvCapture*) cvCreateFileCapture( const char* filename );
enum
{
CV_CAP_ANY =0, // autodetect
CV_CAP_MIL =100, // MIL proprietary drivers
CV_CAP_VFW =200, // platform native
CV_CAP_V4L =200,
CV_CAP_V4L2 =200,
CV_CAP_FIREWARE =300, // IEEE 1394 drivers
CV_CAP_FIREWIRE =300,
CV_CAP_IEEE1394 =300,
CV_CAP_DC1394 =300,
CV_CAP_CMU1394 =300,
CV_CAP_STEREO =400, // TYZX proprietary drivers
CV_CAP_TYZX =400,
CV_TYZX_LEFT =400,
CV_TYZX_RIGHT =401,
CV_TYZX_COLOR =402,
CV_TYZX_Z =403,
CV_CAP_QT =500, // QuickTime
CV_CAP_UNICAP =600, // Unicap drivers
CV_CAP_DSHOW =700, // DirectShow (via videoInput)
CV_CAP_PVAPI =800 // PvAPI, Prosilica GigE SDK
};
从摄像头获取视频文件:(index为camera_index + domain_offset(CV_CAP_*))
CVAPI(CvCapture*) cvCreateCameraCapture( int index );
抓取帧,成功返回1,否则为0:CVAPI(int) cvGrabFrame( CvCapture* capture );
得到cvGrabFrame获取的帧,此函数应用在一些帧处理,比如帧减压,旋转:(千万不要释放或者修改返回帧)CVAPI(IplImage*) cvRetrieveFrame( CvCapture* capture, int streamIdx CV_DEFAULT(0) );
连接cvGrabFrame和cvRetrieveFrame函数:(千万不要释放或者修改返回帧) CVAPI(IplImage*) cvQueryFrame( CvCapture* capture );
释放获取或者读取的视频文件,释放资源:CVAPI(void) cvReleaseCapture( CvCapture** capture );
enum
{
CV_CAP_PROP_POS_MSEC =0,
CV_CAP_PROP_POS_FRAMES =1,
CV_CAP_PROP_POS_AVI_RATIO =2,
CV_CAP_PROP_FRAME_WIDTH =3,
CV_CAP_PROP_FRAME_HEIGHT =4,
CV_CAP_PROP_FPS =5,
CV_CAP_PROP_FOURCC =6,
CV_CAP_PROP_FRAME_COUNT =7,
CV_CAP_PROP_FORMAT =8,
CV_CAP_PROP_MODE =9,
CV_CAP_PROP_BRIGHTNESS =10,
CV_CAP_PROP_CONTRAST =11,
CV_CAP_PROP_SATURATION =12,
CV_CAP_PROP_HUE =13,
CV_CAP_PROP_GAIN =14,
CV_CAP_PROP_EXPOSURE =15,
CV_CAP_PROP_CONVERT_RGB =16,
CV_CAP_PROP_WHITE_BALANCE =17,
CV_CAP_PROP_RECTIFICATION =18,
CV_CAP_PROP_MONOCROME =19
};
恢复或者设置摄像头属性:CVAPI(double) cvGetCaptureProperty( CvCapture* capture, int property_id );
CVAPI(int) cvSetCaptureProperty( CvCapture* capture, int property_id, double value );
返回摄像头类型:CVAPI(int) cvGetCaptureDomain( CvCapture* capture);
定义写视频指针结构体:typedef struct CvVideoWriter CvVideoWriter;
对四色进行编码:CV_INLINE int CV_FOURCC(char c1, char c2, char c3, char c4)
{
return (c1 & 255) + ((c2 & 255) << 8) + ((c3 &255) << 16) + ((c4 & 255) << 24);
}
打开编码选项对话框(windows下)
#define CV_FOURCC_PROMPT -1
默认编码方式(linux下)
#define CV_FOURCC_DEFAULT CV_FOURCC('I', 'Y', 'U', 'V')
初始化写视频指针:CVAPI(CvVideoWriter*) cvCreateVideoWriter( const char* filename, int fourcc,double fps, CvSize frame_size,
int is_color CV_DEFAULT(1));
将帧写到视频指针中:CVAPI(int) cvWriteFrame( CvVideoWriter* writer, const IplImage* image );
释放写视频指针:CVAPI(void) cvReleaseVideoWriter( CvVideoWriter** writer );
过时的函数或同意不同名的函数
#define cvCaptureFromFile cvCreateFileCapture //从视频文件读取视频
#define cvCaptureFromCAM cvCreateCameraCapture//从摄像头读取视频
#define cvCaptureFromAVI cvCaptureFromFile//
#define cvCreateAVIWriter cvCreateVideoWriter//创建写视频指针
#define cvWriteToAVI cvWriteFrame//写入写视频指针
#define cvAddSearchPath(path)//增加路径
#define cvvInitSystem cvInitSystem//
#define cvvNamedWindow cvNamedWindow//创建窗体
#define cvvShowImage cvShowImage//在窗体内显示图片
#define cvvResizeWindow cvResizeWindow//设置窗体大小
#define cvvDestroyWindow cvDestroyWindow//销毁窗体
#define cvvCreateTrackbar cvCreateTrackbar//创建滚动条
#define cvvLoadImage(name) cvLoadImage((name),1)//载入图片
#define cvvSaveImage cvSaveImage//保存图片
#define cvvAddSearchPath cvAddSearchPath//增加路径
#define cvvWaitKey(name) cvWaitKey(0)//按键等待
#define cvvWaitKeyEx(name,delay) cvWaitKey(delay)//按键等待
#define cvvConvertImage cvConvertImage//图片结构改变
#define HG_AUTOSIZE CV_WINDOW_AUTOSIZE//
#define set_preprocess_func cvSetPreprocessFuncWin32//
#define set_postprocess_func cvSetPostprocessFuncWin32//
/highgui/include/opencv2/highgui/highgui.hpp
此C++头文件,引入:
struct CvCapture;
struct CvVideoWriter;
在CV命名空间枚举相应的一些函数和定义了VideoCapture,VideoWriter类,采用面向对象的思想进行编程,更容易理解和整体把握。详细的代码如下:
namespace cv
{
enum { WINDOW_AUTOSIZE=1 };
CV_EXPORTS_W void namedWindow( const string& winname, int flags CV_DEFAULT(WINDOW_AUTOSIZE) );
CV_EXPORTS_W void destroyWindow( const string& winname );
CV_EXPORTS_W int startWindowThread();
CV_EXPORTS_W void setWindowProperty(const string& winname, int prop_id, double prop_value);//YV
CV_EXPORTS_W double getWindowProperty(const string& winname, int prop_id);//YV
//Only for Qt
//------------------------
CV_EXPORTS CvFont fontQt(const string& nameFont, int pointSize CV_DEFAULT(-1), Scalar color CV_DEFAULT(Scalar::all(0)), int weight CV_DEFAULT(CV_FONT_NORMAL), int style CV_DEFAULT(CV_STYLE_NORMAL), int spacing CV_DEFAULT(0));
CV_EXPORTS void addText( const Mat& img, const string& text, Point org, CvFont font);
CV_EXPORTS void displayOverlay(const string& winname, const string& text, int delayms);
CV_EXPORTS void displayStatusBar(const string& winname, const string& text, int delayms);
typedef void (CV_CDECL *OpenGLCallback)(void* userdata);
CV_EXPORTS void createOpenGLCallback(const string& winname, CvOpenGLCallback callbackOpenGL, void* userdata CV_DEFAULT(0));
CV_EXPORTS void saveWindowParameters(const string& windowName);
CV_EXPORTS void loadWindowParameters(const string& windowName);
CV_EXPORTS int startLoop(int (*pt2Func)(int argc, char *argv[]), int argc, char* argv[]);
CV_EXPORTS void stopLoop();
typedef void (CV_CDECL *ButtonCallback)(int state, void* userdata);
CV_EXPORTS int createButton( const string& bar_name, ButtonCallback on_change , void* userdata CV_DEFAULT(NULL), int type CV_DEFAULT(CV_PUSH_BUTTON), bool initial_button_state CV_DEFAULT(0));
//-------------------------
CV_EXPORTS_W void imshow( const string& winname, const Mat& mat );
typedef void (CV_CDECL *TrackbarCallback)(int pos, void* userdata);
CV_EXPORTS int createTrackbar( const string& trackbarname, const string& winname,
int* value, int count,
TrackbarCallback onChange CV_DEFAULT(0),
void* userdata CV_DEFAULT(0));
CV_EXPORTS_W int getTrackbarPos( const string& trackbarname, const string& winname );
CV_EXPORTS_W void setTrackbarPos( const string& trackbarname, const string& winname, int pos );
typedef void (*MouseCallback )(int event, int x, int y, int flags, void* param);
//! assigns callback for mouse events
CV_EXPORTS void setMouseCallback( const string& windowName, MouseCallback onMouse, void* param=0);
CV_EXPORTS_W Mat imread( const string& filename, int flags=1 );
CV_EXPORTS_W bool imwrite( const string& filename, const Mat& img,
const vector
CV_EXPORTS_W Mat imdecode( const Mat& buf, int flags );
CV_EXPORTS_W bool imencode( const string& ext, const Mat& img,
CV_OUT vector
const vector
CV_EXPORTS_W int waitKey(int delay=0);
#ifndef CV_NO_VIDEO_CAPTURE_CPP_API
template<> void CV_EXPORTS Ptr
template<> void CV_EXPORTS Ptr
class CV_EXPORTS_W VideoCapture
{
public:
CV_WRAP VideoCapture();
CV_WRAP VideoCapture(const string& filename);
CV_WRAP VideoCapture(int device);
virtual ~VideoCapture();
CV_WRAP virtual bool open(const string& filename);
CV_WRAP virtual bool open(int device);
CV_WRAP virtual bool isOpened() const;
CV_WRAP virtual void release();
CV_WRAP virtual bool grab();
CV_WRAP virtual bool retrieve(CV_OUT Mat& image, int channel=0);
virtual VideoCapture& operator >> (CV_OUT Mat& image);
CV_WRAP virtual bool read(CV_OUT Mat& image);
CV_WRAP virtual bool set(int propId, double value);
CV_WRAP virtual double get(int propId);
protected:
Ptr
};
class CV_EXPORTS_W VideoWriter
{
public:
CV_WRAP VideoWriter();
CV_WRAP VideoWriter(const string& filename, int fourcc, double fps,
Size frameSize, bool isColor=true);
virtual ~VideoWriter();
CV_WRAP virtual bool open(const string& filename, int fourcc, double fps,
Size frameSize, bool isColor=true);
CV_WRAP virtual bool isOpened() const;
virtual VideoWriter& operator << (const Mat& image);
CV_WRAP virtual void write(const Mat& image);
protected:
Ptr
};