OpenCV源码阅读(3)---base.hpp

本文主要探讨OpenCV核心头文件base.hpp,涉及内容包括错误类型定义、矩阵操作、类型检查、防止数据溢出的机制、内联函数实现的基本运算及前向声明。base.hpp确保了程序的健壮性,通过枚举类型增强可读性,并利用宏控制编译过程。
摘要由CSDN通过智能技术生成

base.h处于core模块中,是OpenCV的核心类。其作用是定义了OpenCV的基本错误类型,在程序运行出现错误是抛出错误,防止数据溢出。总而言之,其功能主要是考虑程序的健壮性。

头文件

#ifndef __OPENCV_CORE_BASE_HPP__
#define __OPENCV_CORE_BASE_HPP__

#ifndef __cplusplus
#  error base.hpp header must be compiled as C++
#endif

#include <climits>

#include "opencv2/core/cvdef.h"
#include "opencv2/core/cvstd.hpp"

namespace cv
{
   

和其他程序一样,base.h中包含头文件cvdef.h,和cvstd.hpp。从名字中就可以读出,base.hpp,是一个.hpp类型的文件,它既包含了声明,又包含了定义(h+cpp)。

namespace Error {
enum {
    StsOk=                       0,  /* everithing is ok                */
    StsBackTrace=               -1,  /* pseudo error for back trace     */
    StsError=                   -2,  /* unknown /unspecified error      */
    StsInternal=                -3,  /* internal error (bad state)      */
    StsNoMem=                   -4,  /* insufficient memory             */
    StsBadArg=                  -5,  /* function arg/param is bad       */
    StsBadFunc=                 -6,  /* unsupported function            */
    StsNoConv=                  -7,  /* iter. didn't converge           */
    StsAutoTrace=               -8,  /* tracing                         */
    HeaderIsNull=               -9,  /* image header is NULL            */
    BadImageSize=              -10,  /* image size is invalid           */
    BadOffset=                 -11,  /* offset is invalid               */
    BadDataPtr=                -12,  /**/
    BadStep=                   -13,  /**/
    BadModelOrChSeq=           -14,  /**/
    BadNumChannels=            -15,  /**/
    BadNumChannel1U=           -16,  /**/
    BadDepth=                  -17,  /**/
    BadAlphaChannel=           -18,  /**/
    BadOrder=                  -19,  /**/
    BadOrigin=                 -20,  /**/
    BadAlign=                  -21,  /**/
    BadCallBack=               -22,  /**/
    BadTileSize=               -23,  /**/
    BadCOI=                    -24,  /**/
    BadROISize=                -25,  /**/
    MaskIsTiled=               -26,  /**/
    StsNullPtr=                -27,  /* null pointer */
    StsVecLengthErr=           -28,  /* incorrect vector length */
    StsFilterStructContentErr= -29,  /* incorr. filter structure content */
    StsKernelStructContentErr= -30,  /* incorr. transform kernel content */
    StsFilterOffsetErr=        -31,  /* incorrect filter ofset value */
    StsBadSize=                -201, /* the input/output structure size is incorrect  */
    StsDivByZero=              -202, /* division by zero */
    StsInplaceNotSupported=    -203, /* in-place operation is not supported */
    StsObjectNotFound=         -204, /* request can't be completed */
    StsUnmatchedFormats=       -205, /* formats of input/output arrays differ */
    StsBadFlag=                -206, /* flag is wrong or not supported */
    StsBadPoint=               -207, /* bad CvPoint */
    StsBadMask=                -208, /* bad format of mask (neither 8uC1 nor 8sC1)*/
    StsUnmatchedSizes=         -209, /* sizes of input/output structures do not match */
    StsUnsupportedFormat=      -210, /* the data format/type is not supported by the function*/
    StsOutOfRange=             -211, /* some of parameters are out of range */
    StsParseError=             -212, /* invalid syntax/structure of the parsed file */
    StsNotImplemented=         -213, /* the requested function/feature is not implemented */
    StsBadMemBlock=            -214, /* an allocated block has been corrupted */
    StsAssert=                 -215, /* assertion failed */
    GpuNotSupported=           -216,
    GpuApiCallError=           -217,
    OpenGlNotSupported=        -218,
    OpenGlApiCallError=        -219,
    OpenCLApiCallError=        -220,
    OpenCLDoubleNotSupported=  -221,
    OpenCLInitError=           -222,
    OpenCLNoAMDBlasFft=        -223
};
} //Error

首先为error专门开了一个namespace,这里用枚举类型的方式定义了错误的类型,这样就把ID和名称联系在了一起,之后只要使用错误的名称就可以调用错误的ID了,增强程序的可读性。

enum { DECOMP_LU       = 0,
       DECOMP_SVD      = 1,
       DECOMP_EIG      = 2,
       DECOMP_CHOLESKY = 3,
       DECOMP_QR       = 4,
       DECOMP_NORMAL   = 16
     };

矩阵的分解方式

enum { NORM_INF       = 1,
       NORM_L1        = 2,
       NORM_L2        = 4,
       NORM_L2SQR     = 5,
       NORM_HAMMING   = 6,
       NORM_HAMMING2  = 7,
       NORM_TYPE_MASK = 7,
       NORM_RELATIVE  = 8,
       NORM_MINMAX    = 32
     };

正规化的类型

enum { CMP_EQ = 0,
       CMP_GT = 1,
       CMP_GE = 2,
       CMP_LT = 3,
       CMP_LE = 4,
       CMP_NE = 5
     };

比较的类型

enum { GEMM_1_T = 1,
       GEMM_2_T = 2,
       GEMM_3_T = 4
     };

enum { DFT_INVERSE        = 1,
       DFT_SCALE          = 2,
       DFT_ROWS           = 4,
       DFT_COMPLEX_OUTPUT = 16,
       DFT_REAL_OUTPUT    = 32,
       DCT_INVERSE        = DFT_INVERSE,
       DCT_ROWS           = DFT_ROWS
     };

//! Various border types, image boundaries are denoted with '|'
enum {
       BORDER_CONSTANT    = 0, // iiiiii|abcdefgh|iiiiiii  with some specified 'i'
       BORDER_REPLICATE   = 1, // aaaaaa|abcdefgh|hhhhhhh
       BORDER_REFLECT     = 2, // fedcba|abcdefgh|hgfedcb
       BORDER_WRAP        = 3, // cdefgh|abcdefgh|abcdefg
       BORDER_REFLECT_101 = 4, // gfedcb|abcdefgh|gfedcba
       BORDER_TRANSPARENT = 5, // uvwxyz|absdefgh|ijklmno

       BORDER_REFLECT101  = BORDER_REFLECT_101,
       BORDER_DEFAULT     = BORDER_REFLECT_101,
       BORDER_ISOLATED    = 16 // do not look outside of ROI
     };

这些都大同小异,通过枚举类的方式实现变量名和ID的替换。

//////////////// static assert /////////////////

#define CVAUX_CONCAT_EXP(a, b) a##b
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值