由于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 flagsCV_DEFAULT(CV_WINDOW_AUTOSIZE) );
设置窗体属性:CVAPI(void) cvSetWindowProperty(const char* name, intprop_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)(intpos);
在给定的窗体创建和显示滚动条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* paramCV_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 iscolorCV_DEFAULT(CV_LOAD_IMAGE_COLOR)); CVAPI(CvMat*) cvLoadImageM( constchar* 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 iscolorCV_DEFAULT(CV_LOAD_IMAGE_COLOR)); CVAPI(CvMat*) cvDecodeImageM(const CvMat* buf, int iscolor CV_DEFAULT(CV_LOAD_IMAGE_COLOR));
对图像进行编码,并将结果保存到单通道的8UC1矩阵中:CVAPI(CvMat*) cvEncodeImage( constchar* 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 proprietarydrivers
CV_CAP_VFW =200, // platformnative
CV_CAP_V4L =200,
CV_CAP_V4L2 =200,
CV_CAP_FIREWARE =300, // IEEE 1394drivers
CV_CAP_FIREWIRE =300,
CV_CAP_IEEE1394 =300,
CV_CAP_DC1394 =300,
CV_CAP_CMU1394 =300,
CV_CAP_STEREO =400, // TYZXproprietary 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, // Unicapdrivers
CV_CAP_DSHOW =700, // DirectShow (viavideoInput)
CV_CAP_PVAPI =800 // PvAPI, ProsilicaGigE 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, charc4) { 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_colorCV_DEFAULT(1));
将帧写到视频指针中:CVAPI(int) cvWriteFrame( CvVideoWriter* writer, constIplImage* 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++头文件,引入: structCvCapture; 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 voiddestroyWindow( const string& winname );CV_EXPORTS_W int startWindowThread(); CV_EXPORTS_W voidsetWindowProperty(const string& winname, intprop_id, double prop_value);//YV CV_EXPORTS_W doublegetWindowProperty(const string& winname, intprop_id);//YV //Only for Qt
//------------------------ CV_EXPORTS
CvFont fontQt(const string& nameFont, intpointSize CV_DEFAULT(-1), Scalar color CV_DEFAULT(Scalar::all(0)),int weight CV_DEFAULT(CV_FONT_NORMAL), int styleCV_DEFAULT(CV_STYLE_NORMAL), int spacing CV_DEFAULT(0));
CV_EXPORTS void addText( const Mat& img, conststring& 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(conststring& winname, CvOpenGLCallback callbackOpenGL,void* userdata CV_DEFAULT(0));
CV_EXPORTS void saveWindowParameters(conststring& windowName);
CV_EXPORTS void loadWindowParameters(conststring& 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* userdataCV_DEFAULT(NULL), int type CV_DEFAULT(CV_PUSH_BUTTON), boolinitial_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* userdataCV_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, intflags, void* param); //! assigns callback for mouse eventsCV_EXPORTS void setMouseCallback( const string&windowName, MouseCallback onMouse, void* param=0); CV_EXPORTS_W Matimread( const string& filename, int flags=1 );CV_EXPORTS_W bool imwrite( const string& filename,const Mat& img, const vector¶ms=vector());
CV_EXPORTS_W Mat imdecode( const Mat& buf, intflags );
CV_EXPORTS_W bool imencode( conststring& ext, const Mat& img, CV_OUTvector& buf, const vector¶ms=vector());
CV_EXPORTS_W int waitKey(int delay=0);
#ifndef CV_NO_VIDEO_CAPTURE_CPP_API
template<> void CV_EXPORTSPtr::delete_obj();
template<> void CV_EXPORTSPtr::delete_obj();
class CV_EXPORTS_W VideoCapture
{
public:
CV_WRAP VideoCapture();
CV_WRAP VideoCapture(conststring& filename);
CV_WRAP VideoCapture(int device);
virtual ~VideoCapture();
CV_WRAP virtual bool open(conststring& filename);
CV_WRAP virtualbool 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_OUTMat& image, int channel=0);
virtualVideoCapture& operator>> (CV_OUT Mat&image);
CV_WRAP virtual bool read(CV_OUTMat& image);
CV_WRAP virtual bool set(int propId,double value);
CV_WRAP virtual double get(intpropId);
protected:
Ptr cap;
};
class CV_EXPORTS_W VideoWriter
{
public:
CV_WRAP VideoWriter();
CV_WRAP VideoWriter(conststring& filename, int fourcc, double fps, SizeframeSize, bool isColor=true);
virtual ~VideoWriter(); C
V_WRAP virtual bool open(conststring& filename, int fourcc, double fps, SizeframeSize, bool isColor=true);
CV_WRAP virtual bool isOpened()
const; virtualVideoWriter& operator<< (const Mat&image);
CV_WRAP virtual void write(constMat& image);
protected:
Ptr writer;
};
modulescoreincludeopencv2coretypes_c.h
前面的是一些宏定义,是为了兼容C,C++。对于一些函数的调用方式,建议补充一点补充知识:http://blog.csdn.net/yongdun_007/archive/2010/12/21/6090281.aspx,这个头文件是很重要的一个文件,建议大家都看看,这里定义了opencv用到的所有的数据类型和结构。详细如下:
types_c.h包括:常用的宏指令和内联函数,随机数的生成,图像数据类型,矩阵数据类型,多维稠密矩阵,多维稀疏矩阵,稀疏矩阵迭代,直方图,其他数据类型定义(包括矩形,终止准则,点和变量,size变量和Box变量,片和部分,尺度或者角度),动态数据结构(包括内存存储,序列,节点或者集,图结构,链和轮廓),序列类型,序列的读/写操作,对序列的操作(包括图的微操作),保持数据结构的稳定性,系统数据类型。
常用的宏指令和内联函数:
#define CV_PI 3.1415926535897932384626433832795//定义PI大小
#define CV_LOG20.69314718055994530941723212145818//定义log2
#define CV_SWAP(a,b,t) ((t) = (a), (a) = (b), (b) =(t))//交换a,b
#ifndef MIN
#define MIN(a,b) ((a) >(b) ? (b) : (a))//求a,b最小值
#endif
#ifndef MAX
#define MAX(a,b) ((a) <(b) ? (b) : (a))//求a,b最大值
#endif
#define CV_IMIN(a,b) ((a) ^ (((a)^(b)) &(((a) < (b)) - 1)))//
#define CV_IMAX(a,b) ((a) ^ (((a)^(b)) &(((a) > (b)) - 1)))//
#ifndef __cplusplus
#define CV_IABS(a) (((a)^ ((a) < 0 ? -1 : 0)) - ((a) < 0 ? -1: 0))
#else
#define CV_IABS(a) abs(a)
#endif
#define CV_CMP(a,b) (((a) > (b)) - ((a) < (b)))
#define CV_SIGN(a) CV_CMP((a),0)
// 返回和参数最接近的整数值
CV_INLINE int cvRound( double value )
//返回不大于参数的最大整数值
CV_INLINE int cvFloor( double value )
//返回不小于参数的最小整数值
CV_INLINE int cvCeil( double value )
//对参数开平方并进行求倒
#define cvInvSqrt(value) ((float)(1./sqrt(value)))
//对参数开平方
#define cvSqrt(value) ((float)sqrt(value))
//判定是否为合法数
CV_INLINE int cvIsNaN( double value )
//判定是否为无穷
CV_INLINE int cvIsInf( double value )
随机数的生成
typedef uint64 CvRNG;
//初始化随机数生成器状态
CV_INLINE CvRNG cvRNG( int64 seed CV_DEFAULT(-1))
//返回32位无符号整型并更新RNG
CV_INLINE unsigned cvRandInt( CvRNG* rng )
//返回浮点型随机数并更新RNG
CV_INLINE double cvRandReal( CvRNG* rng )
图像数据类型
#ifndef HAVE_IPL
//定义深度位数
#define IPL_DEPTH_SIGN 0x80000000
#defineIPL_DEPTH_1U 1
#defineIPL_DEPTH_8U 8
#defineIPL_DEPTH_16U 16
#defineIPL_DEPTH_32F 32
//定义有符号深度位数
#define IPL_DEPTH_8S (IPL_DEPTH_SIGN|8)
#define IPL_DEPTH_16S (IPL_DEPTH_SIGN|16)
#define IPL_DEPTH_32S (IPL_DEPTH_SIGN|32)
//定义数据顺序
#define IPL_DATA_ORDER_PIXEL 0
#define IPL_DATA_ORDER_PLANE 1
//定义图像原点位置
#define IPL_ORIGIN_TL 0 //左上
#define IPL_ORIGIN_BL 1 //左下
//定义扫描线位数
#define IPL_ALIGN_4BYTES 4
#defineIPL_ALIGN_8BYTES 8
#define IPL_ALIGN_16BYTES 16
#define IPL_ALIGN_32BYTES 32
//定义扫描线对齐
#defineIPL_ALIGN_DWORD IPL_ALIGN_4BYTES
#defineIPL_ALIGN_QWORD IPL_ALIGN_8BYTES
//定义扫描线宽度状态
#defineIPL_BORDER_CONSTANT 0 //扫描线连续
#define IPL_BORDER_REPLICATE 1//双扫描线
#defineIPL_BORDER_REFLECT 2 //带阴影扫描线
#defineIPL_BORDER_WRAP 3 //波浪扫描线
//定义图像结构体
typedef struct _IplImage
{
int nSize;
int ID;
int nChannels;
int alphaChannel;
int depth;
charcolorModel[4];
charchannelSeq[4];
int dataOrder;
int origin;
int align;
int width;
int height;
struct _IplROI*roi;
struct _IplImage*maskROI;
void *imageId;
struct _IplTileInfo *tileInfo;
int imageSize;
char*imageData;
int widthStep;
int BorderMode[4];
int BorderConst[4];
char *imageDataOrigin;
}
IplImage;
//定义图像分块信息结构体
typedef struct _IplTileInfo IplTileInfo;
//定义图像感兴趣区域
typedef struct _IplROI
{
int coi; //感兴趣通道
int xOffset; //X坐标值
int yOffset; //y坐标值
int width; //感兴趣区域宽度
int height; //感兴趣区域高度
}
IplROI;
//定义卷积核结构体
typedef struct _IplConvKernel
{
int nCols;
int nRows;
int anchorX;
int anchorY;
int *values;
int nShiftR;
}
IplConvKernel;
//定义快速卷积核结构体
typedef struct _IplConvKernelFP
{
int nCols;
int nRows;
int anchorX;
int anchorY;
float *values;
}
IplConvKernelFP;
#define IPL_IMAGE_HEADER 1
#defineIPL_IMAGE_DATA 2
#defineIPL_IMAGE_ROI 4
#endif
//定义边界模式
#defineIPL_BORDER_REFLECT_101 4 //带有阴影
#defineIPL_BORDER_TRANSPARENT 5 //透明
#define IPL_IMAGE_MAGIC_VAL ((int)sizeof(IplImage))
#define CV_TYPE_NAME_IMAGE "opencv-image"
#define CV_IS_IMAGE_HDR(img)
((img) != NULL && ((constIplImage*)(img))->nSize == sizeof(IplImage))
#define CV_IS_IMAGE(img)
(CV_IS_IMAGE_HDR(img) &&((IplImage*)img)->imageData != NULL)
//定义存储在图像的双精度数据
#define IPL_DEPTH_64F 64
//定义从给定图像,给定数据类型,给定坐标(col,row)获取图像像素值
#define CV_IMAGE_ELEM( image, elemtype, row, col)
(((elemtype*)((image)->imageData +(image)->widthStep*(row)))[(col)])
矩阵数据类型
//
#defineCV_CN_MAX 512 //定义矩阵数据最大值
#defineCV_CN_SHIFT 3 //
#define CV_DEPTH_MAX (1<< CV_CN_SHIFT) //
//定义矩阵数据类型
#define CV_8U 0
#define CV_8S 1
#define CV_16U 2
#define CV_16S 3
#define CV_32S 4
#define CV_32F 5
#define CV_64F 6
#define CV_USRTYPE1 7
//
#defineCV_MAT_DEPTH_MASK (CV_DEPTH_MAX - 1)
#defineCV_MAT_DEPTH(flags) ((flags) & CV_MAT_DEPTH_MASK)
//
#define CV_MAKETYPE(depth,cn) (CV_MAT_DEPTH(depth) + (((cn)-1)<< CV_CN_SHIFT))
#define CV_MAKE_TYPE CV_MAKETYPE
//
#define CV_8UC1 CV_MAKETYPE(CV_8U,1)
#define CV_8UC2 CV_MAKETYPE(CV_8U,2)
#define CV_8UC3 CV_MAKETYPE(CV_8U,3)
#define CV_8UC4 CV_MAKETYPE(CV_8U,4)
#define CV_8UC(n) CV_MAKETYPE(CV_8U,(n))
//
#define CV_8SC1 CV_MAKETYPE(CV_8S,1)
#define CV_8SC2 CV_MAKETYPE(CV_8S,2)
#define CV_8SC3 CV_MAKETYPE(CV_8S,3)
#define CV_8SC4 CV_MAKETYPE(CV_8S,4)
#define CV_8SC(n) CV_MAKETYPE(CV_8S,(n))
//
#define CV_16UC1 CV_MAKETYPE(CV_16U,1)
#define CV_16UC2 CV_MAKETYPE(CV_16U,2)
#define CV_16UC3 CV_MAKETYPE(CV_16U,3)
#define CV_16UC4 CV_MAKETYPE(CV_16U,4)
#define CV_16UC(n) CV_MAKETYPE(CV_16U,(n))
//
#define CV_16SC1 CV_MAKETYPE(CV_16S,1)
#define CV_16SC2 CV_MAKETYPE(CV_16S,2)
#define CV_16SC3 CV_MAKETYPE(CV_16S,3)
#define CV_16SC4 CV_MAKETYPE(CV_16S,4)
#define CV_16SC(n) CV_MAKETYPE(CV_16S,(n))
//
#define CV_32SC1 CV_MAKETYPE(CV_32S,1)
#define CV_32SC2 CV_MAKETYPE(CV_32S,2)
#define CV_32SC3 CV_MAKETYPE(CV_32S,3)
#define CV_32SC4 CV_MAKETYPE(CV_32S,4)
#define CV_32SC(n) CV_MAKETYPE(CV_32S,(n))
//
#define CV_32FC1 CV_MAKETYPE(CV_32F,1)
#define CV_32FC2 CV_MAKETYPE(CV_32F,2)
#define CV_32FC3 CV_MAKETYPE(CV_32F,3)
#define CV_32FC4 CV_MAKETYPE(CV_32F,4)
#define CV_32FC(n) CV_MAKETYPE(CV_32F,(n))
//
#define CV_64FC1 CV_MAKETYPE(CV_64F,1)
#define CV_64FC2 CV_MAKETYPE(CV_64F,2)
#define CV_64FC3 CV_MAKETYPE(CV_64F,3)
#define CV_64FC4 CV_MAKETYPE(CV_64F,4)
#define CV_64FC(n) CV_MAKETYPE(CV_64F,(n))
//
#define CV_AUTO_STEP 0x7fffffff
#define CV_WHOLE_ARR cvSlice( 0,0x3fffffff )
//
#defineCV_MAT_CN_MASK ((CV_CN_MAX - 1) << CV_CN_SHIFT)
#defineCV_MAT_CN(flags) ((((flags) & CV_MAT_CN_MASK)>> CV_CN_SHIFT) + 1)
#defineCV_MAT_TYPE_MASK (CV_DEPTH_MAX*CV_CN_MAX - 1)
#defineCV_MAT_TYPE(flags) ((flags) & CV_MAT_TYPE_MASK)
#define CV_MAT_CONT_FLAG_SHIFT 14
#defineCV_MAT_CONT_FLAG (1 << CV_MAT_CONT_FLAG_SHIFT)
#defineCV_IS_MAT_CONT(flags) ((flags) & CV_MAT_CONT_FLAG)
#defineCV_IS_CONT_MAT CV_IS_MAT_CONT
#defineCV_SUBMAT_FLAG_SHIFT 15
#defineCV_SUBMAT_FLAG (1 << CV_SUBMAT_FLAG_SHIFT)
#defineCV_IS_SUBMAT(flags) ((flags) & CV_MAT_SUBMAT_FLAG)
//
#defineCV_MAGIC_MASK 0xFFFF0000
#defineCV_MAT_MAGIC_VAL 0x42420000
#defineCV_TYPE_NAME_MAT "opencv-matrix"
//定义矩阵结构体
typedef struct CvMat
{
int type;
int step;
int* refcount;
int hdr_refcount;
union
{
uchar* ptr;
short* s;
int* i;
float* fl;
double* db;
} data;
#ifdef __cplusplus
union
{
int rows;
int height;
};
union
{
int cols;
int width;
};
#else
int rows;
int cols;
#endif
}
CvMat;
//判断矩阵头指针
#define CV_IS_MAT_HDR(mat)
((mat) != NULL &&
(((const CvMat*)(mat))->type &CV_MAGIC_MASK) == CV_MAT_MAGIC_VAL&&
((const CvMat*)(mat))->cols > 0&& ((constCvMat*)(mat))->rows > 0)
//
#define CV_IS_MAT_HDR_Z(mat)
((mat) != NULL &&
(((const CvMat*)(mat))->type &CV_MAGIC_MASK) == CV_MAT_MAGIC_VAL&&
((const CvMat*)(mat))->cols >= 0&& ((constCvMat*)(mat))->rows >= 0)
//
#define CV_IS_MAT(mat)
(CV_IS_MAT_HDR(mat) && ((constCvMat*)(mat))->data.ptr != NULL)
//
#define CV_IS_MASK_ARR(mat)
(((mat)->type & (CV_MAT_TYPE_MASK& ~CV_8SC1)) == 0)
#define CV_ARE_TYPES_EQ(mat1, mat2)
((((mat1)->type ^ (mat2)->type)& CV_MAT_TYPE_MASK) == 0)
//
#define CV_ARE_CNS_EQ(mat1, mat2)
((((mat1)->type ^ (mat2)->type)& CV_MAT_CN_MASK) == 0)
//
#define CV_ARE_DEPTHS_EQ(mat1, mat2)
((((mat1)->type ^ (mat2)->type)& CV_MAT_DEPTH_MASK) == 0)
//
#define CV_ARE_SIZES_EQ(mat1, mat2)
((mat1)->rows == (mat2)->rows&& (mat1)->cols ==(mat2)->cols)
#define CV_IS_MAT_CONST(mat)
(((mat)->rows|(mat)->cols) == 1)
//
#define CV_ELEM_SIZE1(type)
((((sizeof(size_t)<<28)|0x8442211)>> CV_MAT_DEPTH(type)*4)& 15)
//
#define CV_ELEM_SIZE(type)
(CV_MAT_CN(type) <<((((sizeof(size_t)/4+1)*16384|0x3a50)>> CV_MAT_DEPTH(type)*2)& 3))
//
#define IPL2CV_DEPTH(depth)
((((CV_8U)+(CV_16U<<4)+(CV_32F<<8)+(CV_64F<<16)+(CV_8S<<20)+
(CV_16S<<24)+(CV_32S<<28))>> ((((depth) & 0xF0)>> 2) +
(((depth) & IPL_DEPTH_SIGN) ? 20 : 0)))& 15)
//初始化矩阵
CV_INLINE CvMat cvMat( int rows, int cols, int type, void* dataCV_DEFAULT(NULL))
//快速将数据添加到矩阵
#define CV_MAT_ELEM_PTR_FAST( mat, row, col, pix_size)
(assert( (unsigned)(row) < (unsigned)(mat).rows&&
(unsigned)(col) < (unsigned)(mat).cols),
(mat).data.ptr + (size_t)(mat).step*(row) + (pix_size)*(col))
//快速将数据添加到矩阵(1)
#define CV_MAT_ELEM_PTR( mat, row, col)
CV_MAT_ELEM_PTR_FAST( mat, row, col, CV_ELEM_SIZE((mat).type) )
//快速将数据添加到矩阵(2)
#define CV_MAT_ELEM( mat, elemtype, row, col)
(*(elemtype*)CV_MAT_ELEM_PTR_FAST( mat, row, col,sizeof(elemtype)))
//从矩阵中获取数据
CV_INLINE double cvmGet( const CvMat* mat, int row,int col )
//将数据添加到矩阵
CV_INLINE void cvmSet( CvMat* mat, int row, int col, double value )
//设置矩阵数据类型
CV_INLINE int cvIplDepth( int type )
多维稠密矩阵
//
#defineCV_MATND_MAGIC_VAL 0x42430000
#defineCV_TYPE_NAME_MATND "opencv-nd-matrix"
//
#defineCV_MAX_DIM 32
#defineCV_MAX_DIM_HEAP (1 << 16)
//定义稠密矩阵结构体
typedef struct CvMatND
{
int type;
int dims;
int* refcount;
int hdr_refcount;
union
{
uchar* ptr;
float* fl;
double* db;
int* i;
short* s;
} data;
struct
{
int size;
int step;
}
dim[CV_MAX_DIM];
}
CvMatND;
//
#define CV_IS_MATND_HDR(mat)
((mat) != NULL && (((constCvMatND*)(mat))->type &CV_MAGIC_MASK) == CV_MATND_MAGIC_VAL)
//
#define CV_IS_MATND(mat)
(CV_IS_MATND_HDR(mat) && ((constCvMatND*)(mat))->data.ptr != NULL)
多维稀疏矩阵
//
#defineCV_SPARSE_MAT_MAGIC_VAL 0x42440000
#defineCV_TYPE_NAME_SPARSE_MAT "opencv-sparse-matrix"
//
struct CvSet;
//定义稀疏矩阵结构体
typedef struct CvSparseMat
{
int type;
int dims;
int* refcount;
int hdr_refcount;
struct CvSet* heap;
void** hashtable;
int hashsize;
int valoffset;
int idxoffset;
int size[CV_MAX_DIM];
}
CvSparseMat;
//
#define CV_IS_SPARSE_MAT_HDR(mat)
((mat) != NULL &&
(((const CvSparseMat*)(mat))->type &CV_MAGIC_MASK) == CV_SPARSE_MAT_MAGIC_VAL)
//
#define CV_IS_SPARSE_MAT(mat)
CV_IS_SPARSE_MAT_HDR(mat)
稀疏矩阵迭代
//定义稀疏矩阵迭代体结构体
typedef struct CvSparseNode
{
unsigned hashval;
struct CvSparseNode* next;
}
CvSparseNode;
//定义稀疏矩阵迭代器
typedef struct CvSparseMatIterator
{
CvSparseMat* mat;
CvSparseNode* node;
int curidx;
}
CvSparseMatIterator;
#defineCV_NODE_VAL(mat,node) ((void*)((uchar*)(node) + (mat)->valoffset))
#defineCV_NODE_IDX(mat,node) ((int*)((uchar*)(node) + (mat)->idxoffset))
直方图
typedef int CvHistType;
//
#defineCV_HIST_MAGIC_VAL 0x42450000
#define CV_HIST_UNIFORM_FLAG (1<< 10)
//维区域是否设置的标签
#defineCV_HIST_RANGES_FLAG (1 << 11)
//定义矩阵状态
#defineCV_HIST_ARRAY 0 //数组直方图
#defineCV_HIST_SPARSE 1 //稀疏直方图
#defineCV_HIST_TREE CV_HIST_SPARSE //树形直方图
#defineCV_HIST_UNIFORM 1
//定义直方图结构体
typedef struct CvHistogram
{
int type;
CvArr* bins;
float thresh[CV_MAX_DIM][2];
float**thresh2;
CvMatNDmat;
}
CvHistogram;
//
#define CV_IS_HIST( hist )
((hist) != NULL &&
(((CvHistogram*)(hist))->type &CV_MAGIC_MASK) == CV_HIST_MAGIC_VAL&&
(hist)->bins != NULL)
//
#define CV_IS_UNIFORM_HIST( hist )
(((hist)->type &CV_HIST_UNIFORM_FLAG) != 0)
//
#define CV_IS_SPARSE_HIST( hist )
CV_IS_SPARSE_MAT((hist)->bins)
//
#define CV_HIST_HAS_RANGES( hist )
(((hist)->type &CV_HIST_RANGES_FLAG) != 0)
//
其他数据类型应用定义------矩形
//定义矩形结构体
typedef struct CvRect
{
int x;
int y;
int width;
int height;
}
CvRect;
//矩形初始化
CV_INLINE CvRect cvRect( int x, int y, int width,int height )
{
CvRect r;
r.x = x;
r.y = y;
r.width = width;
r.height = height;
return r;
}
//在感兴趣通道上设置图的感兴趣区域
CV_INLINE IplROI cvRectToROI( CvRect rect, int coi)
//获取感兴趣区域的矩形大小
CV_INLINE CvRect cvROIToRect( IplROI roi )
其他数据类型应用定义------终止准则
//
#defineCV_TERMCRIT_ITER 1
#define CV_TERMCRIT_NUMBER CV_TERMCRIT_ITER
#defineCV_TERMCRIT_EPS 2
//定义迭代算法的终止准则结构体
typedef struct CvTermCriteria
{
int type;
int max_iter;
double epsilon;
}
CvTermCriteria;
//初始化终止准则
CV_INLINE CvTermCriteria cvTermCriteria( int type,int max_iter, double epsilon )
其他数据类型应用定义------点和变量
//定义整型二维点
typedef struct CvPoint
{
int x;
int y;
}
CvPoint;
//初始化二维点
CV_INLINE CvPoint cvPoint( int x, int y )
{
CvPoint p;
p.x = x;
p.y = y;
return p;
}
//定义浮点型二维点
typedef struct CvPoint2D32f
{
float x;
float y;
}
CvPoint2D32f;
//初始化浮点型二维点
CV_INLINE CvPoint2D32f cvPoint2D32f( double x,double y )
{
CvPoint2D32f p;
p.x = (float)x;
p.y = (float)y;
return p;
}
//
CV_INLINE CvPoint2D32f cvPointTo32f( CvPoint point)
{
return cvPoint2D32f( (float)point.x, (float)point.y );
}
CV_INLINE CvPoint cvPointFrom32f( CvPoint2D32f point)
{
CvPoint ipt;
ipt.x = cvRound(point.x);
ipt.y = cvRound(point.y);
return ipt;
}
//定义浮点型三维点
typedef struct CvPoint3D32f
{
float x;
float y;
float z;
}
CvPoint3D32f;
//初始化浮点型三维点
CV_INLINE CvPoint3D32f cvPoint3D32f( double x,double y, double z )
{
CvPoint3D32f p;
p.x = (float)x;
p.y = (float)y;
p.z = (float)z;
return p;
}
//定义双精度型二维点
typedef struct CvPoint2D64f
{
double x;
double y;
}
CvPoint2D64f;
//初始化双精度型二维点
CV_INLINE CvPoint2D64f cvPoint2D64f( double x,double y )
{
CvPoint2D64f p;
p.x = x;
p.y = y;
return p;
}
//定义双精度型三维点
typedef struct CvPoint3D64f
{
double x;
double y;
double z;
}
CvPoint3D64f;
//初始化双精度型三维点
CV_INLINE CvPoint3D64f cvPoint3D64f( double x,double y, double z )
{
CvPoint3D64f p;
p.x = x;
p.y = y;
p.z = z;
return p;
}
其他数据类型应用定义------size变量和Box箱变量
//定义size
typedef struct
{
int width;
int height;
}
CvSize;
//初始化size
CV_INLINE CvSize cvSize( int width, int height )
{
CvSize s;
s.width = width;
s.height = height;
return s;
}
//定义二维浮点型size
typedef struct CvSize2D32f
{
float width;
float height;
}
CvSize2D32f;
//初始化二维浮点型size
CV_INLINE CvSize2D32f cvSize2D32f( double width,double height )
{
CvSize2D32f s;
s.width = (float)width;
s.height = (float)height;
return s;
}
//定义二维箱
typedef struct CvBox2D
{
CvPoint2D32f center;
CvSize2D32f size;
floatangle;
}
CvBox2D;
//定义线性迭代状态
typedef struct CvLineIterator
{
uchar* ptr;
int err;
int plus_delta;
int minus_delta;
int plus_step;
int minus_step;
}
CvLineIterator;
其他数据类型应用定义------片,部分
//定义片
typedef struct CvSlice
{
int start_index, end_index;
}
CvSlice;
//初始化片
CV_INLINE CvSlice cvSlice( int start, int end )
{
CvSlice slice;
slice.start_index = start;
slice.end_index = end;
return slice;
}
#define CV_WHOLE_SEQ_END_INDEX 0x3fffffff
#define CV_WHOLE_SEQ cvSlice(0,CV_WHOLE_SEQ_END_INDEX)
其他数据类型应用定义------尺度,角度
//定义角度结构体
typedef struct CvScalar
{
double val[4];
}
CvScalar;
//初始化角度
CV_INLINE CvScalar cvScalar( double val0, doubleval1 CV_DEFAULT(0),
double val2 CV_DEFAULT(0), double val3 CV_DEFAULT(0))
{
CvScalar scalar;
scalar.val[0] = val0; scalar.val[1] = val1;
scalar.val[2] = val2; scalar.val[3] = val3;
return scalar;
}
CV_INLINE CvScalar cvRealScalar( double val0 )
{
CvScalar scalar;
scalar.val[0] = val0;
scalar.val[1] = scalar.val[2] = scalar.val[3] = 0;
return scalar;
}
CV_INLINE CvScalar cvScalarAll( double val0123 )
{
CvScalar scalar;
scalar.val[0] = val0123;
scalar.val[1] = val0123;
scalar.val[2] = val0123;
scalar.val[3] = val0123;
return scalar;
}
动态数据结构-内存存储
//定义内存块结构
typedef struct CvMemBlock
{
struct CvMemBlock* prev;
struct CvMemBlock* next;
}
CvMemBlock;
#defineCV_STORAGE_MAGIC_VAL 0x42890000
typedef struct CvMemStorage
{
int signature;
CvMemBlock*bottom;
CvMemBlock*top;
struct CvMemStorage* parent;
intblock_size;
intfree_space;
}
CvMemStorage;
#define CV_IS_STORAGE(storage)
((storage) != NULL&&
(((CvMemStorage*)(storage))->signature& CV_MAGIC_MASK) == CV_STORAGE_MAGIC_VAL)
typedef struct CvMemStoragePos
{
CvMemBlock* top;
int free_space;
}
CvMemStoragePos;
动态数据结构-序列(线性)
//定义序列块结构体
typedef struct CvSeqBlock
{
struct CvSeqBlock* prev;
struct CvSeqBlock* next;
int start_index;
int count;
schar*data;
}
CvSeqBlock;
//定义序列节点
#defineCV_TREE_NODE_FIELDS(node_type)
int flags;
int header_size;
struct node_type* h_prev;
struct node_type* h_next;
struct node_type* v_prev;
struct node_type* v_next
//读和写序列(动态的添加和删除元素) #defineCV_SEQUENCE_FIELDS()
CV_TREE_NODE_FIELDS(CvSeq);
int total;
int elem_size;
schar* block_max;
schar* ptr;
int delta_elems;
CvMemStorage*storage;
CvSeqBlock* free_blocks;
CvSeqBlock*first;
//定义序列结构体
typedef struct CvSeq
{
CV_SEQUENCE_FIELDS()
}
CvSeq;
#defineCV_TYPE_NAME_SEQ "opencv-sequence"
#defineCV_TYPE_NAME_SEQ_TREE "opencv-sequence-tree"
动态数据结构-节点(集)
(节点顺序是不被保护的,他们可看成介于元素间的空隙,但被插入后就保留在该地方,判断有元素,看MSB‘mose-significantor sign bit’的标志位)
//定义序列节点属性
#defineCV_SET_ELEM_FIELDS(elem_type)
int flags;
struct elem_type* next_free;
//设置序列元素
typedef struct CvSetElem
{
CV_SET_ELEM_FIELDS(CvSetElem)
}
CvSetElem;
//
#defineCV_SET_FIELDS()
CV_SEQUENCE_FIELDS()
CvSetElem*free_elems;
int active_count;
//
typedef struct CvSet
{
CV_SET_FIELDS()
}
CvSet;
#define CV_SET_ELEM_IDX_MASK ((1 << 26) -1)
#define CV_SET_ELEM_FREE_FLAG (1<< (sizeof(int)*8-1))
//定义元素指针被设置与否
#define CV_IS_SET_ELEM( ptr ) (((CvSetElem*)(ptr))->flags >= 0)
动态数据结构-图结构
//定义图边属性
#defineCV_GRAPH_EDGE_FIELDS()
intflags;
floatweight;
struct CvGraphEdge*next[2];
struct CvGraphVtx* vtx[2];
//定义图节点属性
#defineCV_GRAPH_VERTEX_FIELDS()
intflags;
struct CvGraphEdge* first;
//定义图边结构体
typedef struct CvGraphEdge
{
CV_GRAPH_EDGE_FIELDS()
}
CvGraphEdge;
//定义图节点结构体
typedef struct CvGraphVtx
{
CV_GRAPH_VERTEX_FIELDS()
}
CvGraphVtx;
//定义二维图
typedef struct CvGraphVtx2D
{
CV_GRAPH_VERTEX_FIELDS()
CvPoint2D32f* ptr;
}
CvGraphVtx2D;
//定义图属性
#define CV_GRAPH_FIELDS()
CV_SET_FIELDS()
CvSet* edges;
//定义图结构
typedef struct CvGraph
{
CV_GRAPH_FIELDS()
}
CvGraph;
#define CV_TYPE_NAME_GRAPH "opencv-graph"
动态数据结构-链和轮廓
//定义链结构
typedef struct CvChain
{
CV_SEQUENCE_FIELDS()
CvPoint origin;
}
CvChain;
//定义轮廓属性
#define CV_CONTOUR_FIELDS()
CV_SEQUENCE_FIELDS()
CvRectrect;
intcolor;
int reserved[3];
//定义轮廓结构体
typedef struct CvContour
{
CV_CONTOUR_FIELDS()
}
CvContour;
typedef CvContour CvPoint2DSeq;
序列类型
//表示稠密序列
#defineCV_SEQ_MAGIC_VAL 0x42990000
#define CV_IS_SEQ(seq)
((seq) != NULL &&(((CvSeq*)(seq))->flags &CV_MAGIC_MASK) == CV_SEQ_MAGIC_VAL)
//表示稀疏序列
#defineCV_SET_MAGIC_VAL 0x42980000
#define CV_IS_SET(set)
((set) != NULL &&(((CvSeq*)(set))->flags &CV_MAGIC_MASK) == CV_SET_MAGIC_VAL)
//表示稠密序列元素位数
#defineCV_SEQ_ELTYPE_BITS 12
#defineCV_SEQ_ELTYPE_MASK ((1 << CV_SEQ_ELTYPE_BITS) - 1)
//
#defineCV_SEQ_ELTYPE_POINT CV_32SC2
#defineCV_SEQ_ELTYPE_CODE CV_8UC1
//
#defineCV_SEQ_ELTYPE_GENERIC 0
#defineCV_SEQ_ELTYPE_PTR CV_USRTYPE1
#defineCV_SEQ_ELTYPE_PPOINT CV_SEQ_ELTYPE_PTR
#defineCV_SEQ_ELTYPE_INDEX CV_32SC1
#defineCV_SEQ_ELTYPE_GRAPH_EDGE 0
#defineCV_SEQ_ELTYPE_GRAPH_VERTEX 0
#defineCV_SEQ_ELTYPE_TRIAN_ATR 0
#define CV_SEQ_ELTYPE_CONNECTED_COMP0
#defineCV_SEQ_ELTYPE_POINT3D CV_32FC3
//表示稠密序列类型位数
#defineCV_SEQ_KIND_BITS 2
#defineCV_SEQ_KIND_MASK (((1 << CV_SEQ_KIND_BITS) -1)<<CV_SEQ_ELTYPE_BITS)
//稠密序列的三种类型
#defineCV_SEQ_KIND_GENERIC (0 << CV_SEQ_ELTYPE_BITS)
#defineCV_SEQ_KIND_CURVE (1 << CV_SEQ_ELTYPE_BITS)
#defineCV_SEQ_KIND_BIN_TREE (2 << CV_SEQ_ELTYPE_BITS)
//稀疏序列的三种类型
#defineCV_SEQ_KIND_GRAPH (1 << CV_SEQ_ELTYPE_BITS)
#defineCV_SEQ_KIND_SUBDIV2D (2 << CV_SEQ_ELTYPE_BITS)
#defineCV_SEQ_FLAG_SHIFT (CV_SEQ_KIND_BITS + CV_SEQ_ELTYPE_BITS)
//曲线序列的类型标志
#defineCV_SEQ_FLAG_CLOSED (1 << CV_SEQ_FLAG_SHIFT)
#defineCV_SEQ_FLAG_SIMPLE (0 << CV_SEQ_FLAG_SHIFT)
#defineCV_SEQ_FLAG_CONVEX (0 << CV_SEQ_FLAG_SHIFT)
#defineCV_SEQ_FLAG_HOLE (2 << CV_SEQ_FLAG_SHIFT)
//图序列的类型标志
#define CV_GRAPH_FLAG_ORIENTED (1<< CV_SEQ_FLAG_SHIFT)
#defineCV_GRAPH CV_SEQ_KIND_GRAPH
#defineCV_ORIENTED_GRAPH (CV_SEQ_KIND_GRAPH|CV_GRAPH_FLAG_ORIENTED)
//定义点集
#defineCV_SEQ_POINT_SET (CV_SEQ_KIND_GENERIC| CV_SEQ_ELTYPE_POINT)
#defineCV_SEQ_POINT3D_SET (CV_SEQ_KIND_GENERIC| CV_SEQ_ELTYPE_POINT3D)
#defineCV_SEQ_POLYLINE (CV_SEQ_KIND_CURVE |CV_SEQ_ELTYPE_POINT)
#defineCV_SEQ_POLYGON (CV_SEQ_FLAG_CLOSED | CV_SEQ_POLYLINE )
#defineCV_SEQ_CONTOUR CV_SEQ_POLYGON
#define CV_SEQ_SIMPLE_POLYGON (CV_SEQ_FLAG_SIMPLE | CV_SEQ_POLYGON )
//定义曲线链表
#defineCV_SEQ_CHAIN (CV_SEQ_KIND_CURVE |CV_SEQ_ELTYPE_CODE)
#defineCV_SEQ_CHAIN_CONTOUR (CV_SEQ_FLAG_CLOSED | CV_SEQ_CHAIN)
//用二叉树表示轮廓集
#defineCV_SEQ_POLYGON_TREE (CV_SEQ_KIND_BIN_TREE |CV_SEQ_ELTYPE_TRIAN_ATR)
//连接部分的序列表示
#define CV_SEQ_CONNECTED_COMP (CV_SEQ_KIND_GENERIC |CV_SEQ_ELTYPE_CONNECTED_COMP)
//整数表示的序列
#defineCV_SEQ_INDEX (CV_SEQ_KIND_GENERIC | CV_SEQ_ELTYPE_INDEX)
#define CV_SEQ_ELTYPE( seq) ((seq)->flags &CV_SEQ_ELTYPE_MASK)
#define CV_SEQ_KIND( seq) ((seq)->flags & CV_SEQ_KIND_MASK)
//标记检测
#define CV_IS_SEQ_INDEX( seq) ((CV_SEQ_ELTYPE(seq) == CV_SEQ_ELTYPE_INDEX)&&
(CV_SEQ_KIND(seq) == CV_SEQ_KIND_GENERIC))
#define CV_IS_SEQ_CURVE( seq) (CV_SEQ_KIND(seq) == CV_SEQ_KIND_CURVE)
#define CV_IS_SEQ_CLOSED( seq) (((seq)->flags & CV_SEQ_FLAG_CLOSED)!= 0)
#define CV_IS_SEQ_CONVEX( seq) 0
#define CV_IS_SEQ_HOLE( seq) (((seq)->flags & CV_SEQ_FLAG_HOLE)!= 0)
#define CV_IS_SEQ_SIMPLE( seq) 1
//类型检测
#define CV_IS_SEQ_POINT_SET( seq )
((CV_SEQ_ELTYPE(seq) == CV_32SC2 || CV_SEQ_ELTYPE(seq) ==CV_32FC2))
#define CV_IS_SEQ_POINT_SUBSET( seq )
(CV_IS_SEQ_INDEX( seq ) || CV_SEQ_ELTYPE(seq) ==CV_SEQ_ELTYPE_PPOINT)
#define CV_IS_SEQ_POLYLINE( seq)
(CV_SEQ_KIND(seq) == CV_SEQ_KIND_CURVE&& CV_IS_SEQ_POINT_SET(seq))
#define CV_IS_SEQ_POLYGON( seq)
(CV_IS_SEQ_POLYLINE(seq) &&CV_IS_SEQ_CLOSED(seq))
#define CV_IS_SEQ_CHAIN( seq)
(CV_SEQ_KIND(seq) == CV_SEQ_KIND_CURVE&& (seq)->elem_size== 1)
#define CV_IS_SEQ_CONTOUR( seq )
(CV_IS_SEQ_CLOSED(seq) &&(CV_IS_SEQ_POLYLINE(seq) || CV_IS_SEQ_CHAIN(seq)))
#define CV_IS_SEQ_CHAIN_CONTOUR( seq )
(CV_IS_SEQ_CHAIN( seq ) &&CV_IS_SEQ_CLOSED( seq ))
#define CV_IS_SEQ_POLYGON_TREE( seq )
(CV_SEQ_ELTYPE (seq) == CV_SEQ_ELTYPE_TRIAN_ATR&&
CV_SEQ_KIND( seq ) == CV_SEQ_KIND_BIN_TREE)
#define CV_IS_GRAPH( seq)
(CV_IS_SET(seq) &&CV_SEQ_KIND((CvSet*)(seq)) == CV_SEQ_KIND_GRAPH)
#define CV_IS_GRAPH_ORIENTED( seq)
(((seq)->flags &CV_GRAPH_FLAG_ORIENTED) != 0)
#define CV_IS_SUBDIV2D( seq )
(CV_IS_SET(seq) &&CV_SEQ_KIND((CvSet*)(seq)) == CV_SEQ_KIND_SUBDIV2D)
序列的读/写操作
//定义序列写的属性
#defineCV_SEQ_WRITER_FIELDS()
int header_size;
CvSeq* seq;
CvSeqBlock* block;
schar* ptr;
schar* block_min;
schar* block_max;
//定义序列写结构体
typedef struct CvSeqWriter
{
CV_SEQ_WRITER_FIELDS()
}
CvSeqWriter;
//定义序列读的属性
#defineCV_SEQ_READER_FIELDS()
int header_size;
CvSeq* seq;
CvSeqBlock* block;
schar* ptr;
schar* block_min;
schar* block_max;
int delta_index;
schar* prev_elem;
//定义序列读的结构体
typedef struct CvSeqReader
{
CV_SEQ_READER_FIELDS()
}
CvSeqReader;
对序列的操作
#define CV_SEQ_ELEM( seq, elem_type,index)
( assert(sizeof((seq)->first[0]) == sizeof(CvSeqBlock)&&
(seq)->elem_size ==sizeof(elem_type)),
(elem_type*)((seq)->first&& (unsigned)index<
(unsigned)((seq)->first->count)?
(seq)->first->data + (index) *sizeof(elem_type):
cvGetSeqElem( (CvSeq*)(seq), (index) )))
#define CV_GET_SEQ_ELEM( elem_type, seq, index ) CV_SEQ_ELEM((seq), elem_type, (index) )
//在序列中增加元素
#define CV_WRITE_SEQ_ELEM_VAR( elem_ptr, writer)
{
if( (writer).ptr >= (writer).block_max)
{
cvCreateSeqBlock(&writer);
}
memcpy((writer).ptr, elem_ptr,(writer).seq->elem_size);
(writer).ptr +=(writer).seq->elem_size;
}
#define CV_WRITE_SEQ_ELEM( elem, writer)
{
assert( (writer).seq->elem_size == sizeof(elem));
if( (writer).ptr >= (writer).block_max)
{
cvCreateSeqBlock(&writer);
}
assert( (writer).ptr <= (writer).block_max -sizeof(elem));
memcpy((writer).ptr, &(elem),sizeof(elem));
(writer).ptr +=sizeof(elem);
}
//向前移动读序列位置
#define CV_NEXT_SEQ_ELEM( elem_size, reader)
{
if( ((reader).ptr += (elem_size)) >=(reader).block_max )
{
cvChangeSeqBlock( &(reader), 1);
}
}
//向前移动读序列位置
#define CV_PREV_SEQ_ELEM( elem_size, reader)
{
if( ((reader).ptr -= (elem_size)) <(reader).block_min )
{
cvChangeSeqBlock( &(reader), -1);
}
}
//读序列中元素并向前移动读位置
#define CV_READ_SEQ_ELEM( elem, reader)
{
assert( (reader).seq->elem_size ==sizeof(elem));
memcpy( &(elem), (reader).ptr,sizeof((elem)));
CV_NEXT_SEQ_ELEM( sizeof(elem), reader)
}
//读序列中元素并向前移动读位置
#define CV_REV_READ_SEQ_ELEM( elem, reader)
{
assert( (reader).seq->elem_size ==sizeof(elem));
memcpy(&(elem), (reader).ptr,sizeof((elem)));
CV_PREV_SEQ_ELEM( sizeof(elem), reader)
}
#define CV_READ_CHAIN_POINT( _pt, reader)
{
(_pt) =(reader).pt;
if( (reader).ptr)
{
CV_READ_SEQ_ELEM( (reader).code,(reader));
assert( ((reader).code & ~7) == 0);
(reader).pt.x +=(reader).deltas[(int)(reader).code][0];
(reader).pt.y +=(reader).deltas[(int)(reader).code][1];
}
}
#define CV_CURRENT_POINT( reader ) (*((CvPoint*)((reader).ptr)))
#define CV_PREV_POINT( reader) (*((CvPoint*)((reader).prev_elem)))
//读边序列
#define CV_READ_EDGE( pt1, pt2, reader)
{
assert( sizeof(pt1) == sizeof(CvPoint)&&
sizeof(pt2) == sizeof(CvPoint)&&
reader.seq->elem_size == sizeof(CvPoint));
(pt1) = CV_PREV_POINT( reader);
(pt2) = CV_CURRENT_POINT( reader);
(reader).prev_elem =(reader).ptr;
CV_NEXT_SEQ_ELEM( sizeof(CvPoint),(reader));
}
对序列的操作-图的微操作
//返回给定顶点的图边缘
#define CV_NEXT_GRAPH_EDGE( edge,vertex )
(assert((edge)->vtx[0] == (vertex) ||(edge)->vtx[1] ==(vertex)),
(edge)->next[(edge)->vtx[1] ==(vertex)])
保持数据结构的连续性的说明
//文件存储的“黑箱”定义
typedef struct CvFileStorage CvFileStorage;
//存储标记
#defineCV_STORAGE_READ 0
#defineCV_STORAGE_WRITE 1
#defineCV_STORAGE_WRITE_TEXT CV_STORAGE_WRITE
#define CV_STORAGE_WRITE_BINARY CV_STORAGE_WRITE
#defineCV_STORAGE_APPEND 2
//属性列表
typedef struct CvAttrList
{
const char**attr;
struct CvAttrList*next;
}
CvAttrList;
CV_INLINE CvAttrList cvAttrList( const char** attrCV_DEFAULT(NULL),
CvAttrList* next CV_DEFAULT(NULL) )
{
CvAttrList l;
l.attr = attr;
l.next = next;
return l;
}
struct CvTypeInfo;
#defineCV_NODE_NONE 0
#defineCV_NODE_INT 1
#defineCV_NODE_INTEGER CV_NODE_INT
#defineCV_NODE_REAL 2
#defineCV_NODE_FLOAT CV_NODE_REAL
#defineCV_NODE_STR 3
#defineCV_NODE_STRING CV_NODE_STR
#defineCV_NODE_REF 4
#defineCV_NODE_SEQ 5
#defineCV_NODE_MAP 6
#defineCV_NODE_TYPE_MASK 7
#define CV_NODE_TYPE(flags) ((flags)& CV_NODE_TYPE_MASK)
#defineCV_NODE_FLOW 8
#defineCV_NODE_USER 16
#defineCV_NODE_EMPTY 32
#defineCV_NODE_NAMED 64
#defineCV_NODE_IS_INT(flags) (CV_NODE_TYPE(flags) == CV_NODE_INT)
#defineCV_NODE_IS_REAL(flags) (CV_NODE_TYPE(flags) == CV_NODE_REAL)
#defineCV_NODE_IS_STRING(flags) (CV_NODE_TYPE(flags) == CV_NODE_STRING)
#defineCV_NODE_IS_SEQ(flags) (CV_NODE_TYPE(flags) == CV_NODE_SEQ)
#defineCV_NODE_IS_MAP(flags) (CV_NODE_TYPE(flags) == CV_NODE_MAP)
#define CV_NODE_IS_COLLECTION(flags) (CV_NODE_TYPE(flags)>= CV_NODE_SEQ)
#defineCV_NODE_IS_FLOW(flags) (((flags) & CV_NODE_FLOW) != 0)
#defineCV_NODE_IS_EMPTY(flags) (((flags) & CV_NODE_EMPTY) != 0)
#defineCV_NODE_IS_USER(flags) (((flags) & CV_NODE_USER) != 0)
#defineCV_NODE_HAS_NAME(flags) (((flags) & CV_NODE_NAMED) != 0)
#define CV_NODE_SEQ_SIMPLE 256
#define CV_NODE_SEQ_IS_SIMPLE(seq) (((seq)->flags& CV_NODE_SEQ_SIMPLE) != 0)
typedef struct CvString
{
int len;
char* ptr;
}
CvString;
//所有的元素名字以哈希表中,增加搜索速度
typedef struct CvStringHashNode{
unsigned hashval;
CvString str;
struct CvStringHashNode* next;
}
CvStringHashNode;
typedef struct CvGenericHash CvFileNodeHash;
typedef struct CvFileNode
{
int tag;
struct CvTypeInfo* info;
union
{
double f;
inti;
CvString str;
CvSeq* seq;
CvFileNodeHash* map;
} data;
}
CvFileNode;
#ifdef __cplusplus
extern "C" {
#endif
typedef int (CV_CDECL *CvIsInstanceFunc)( const void* struct_ptr);
typedef void (CV_CDECL *CvReleaseFunc)( void** struct_dblptr);
typedef void* (CV_CDECL *CvReadFunc)( CvFileStorage* storage,CvFileNode* node );
typedef void (CV_CDECL *CvWriteFunc)( CvFileStorage* storage,const char* name,
const void* struct_ptr, CvAttrList attributes );
typedef void* (CV_CDECL *CvCloneFunc)( const void* struct_ptr);
#ifdef __cplusplus
}
#endif
typedef struct CvTypeInfo
{
int flags;
int header_size;
struct CvTypeInfo* prev;
struct CvTypeInfo* next;
const char* type_name;
CvIsInstanceFunc is_instance;
CvReleaseFunc release;
CvReadFunc read;
CvWriteFunc write;
CvCloneFunc clone;
}
CvTypeInfo;
系统数据类型
typedef struct CvPluginFuncInfo
{
void** func_addr;
void* default_func_addr;
const char* func_names;
int search_modules;
int loaded_from;
}
CvPluginFuncInfo;
typedef struct CvModuleInfo
{
struct CvModuleInfo* next;
const char* name;
const char* version;
CvPluginFuncInfo* func_tab;
}
CvModuleInfo;