搜档网
当前位置:搜档网 › OpenCV经典代码

OpenCV经典代码

///////////////////////////
// 1 //
////////////////////////////
#define CV_MAJOR_VERSION1 2
#define CV_MINOR_VERSION1 0
#define CV_SUBMINOR_VERSION1 0

#define CVAUX_STR_EXP1(__A) #__A
#define CVAUX_STR1(__A) CVAUX_STR_EXP1(__A)
#define CV_VERSION1 CVAUX_STR1(CV_MAJOR_VERSION1) "." CVAUX_STR1(CV_MINOR_VERSION1) "." CVAUX_STR1(CV_SUBMINOR_VERSION1)
////////////////////////////
// 2 //
////////////////////////////
template static inline _Tp* alignPtr(_Tp* ptr, int n=(int)sizeof(_Tp))
{
//当n=2,4,8,16,32,64,...时才能真正的对齐
return (_Tp*)(((size_t)ptr + n-1) & -n);
}
////////////////////////////
// 3 //
////////////////////////////

void error( const Exception& exc )
{
if (customErrorCallback != 0)
customErrorCallback(exc.code, exc.func.c_str(), exc.err.c_str(),
exc.file.c_str(), exc.line, customErrorCallbackData);
else
{
const char* errorStr = cvErrorStr(exc.code);
char buf[1 << 16];

sprintf( buf, "OpenCV Error: %s (%s) in %s, file %s, line %d",
errorStr, exc.err.c_str(), exc.func.size() > 0 ?
exc.func.c_str() : "unknown function", exc.file.c_str(), exc.line );
fprintf( stderr, "%s\n", buf );
fflush( stderr );
}
if(breakOnError)
{
static volatile int* p = 0;//不懂为什么要用volatile变量 也不懂这里究竟做了什么工作
*p = 0;
}
throw exc;
}
////////////////////////////
// 4 //
////////////////////////////
class AException
{
public:
AException() { code = 0; line = 0; }
AException(int _code, const string& _err, const string& _func, const string& _file, int _line)
: code(_code), err(_err), func(_func), file(_file), line(_line) {}
AException(const AException& exc)
: code(exc.code), err(exc.err), func(exc.func), file(exc.file), line(exc.line) {}
AException& operator = (const AException& exc)
{
if( this != &exc )
{
code = exc.code; err = exc.err; func = exc.func; file = exc.file; line = exc.line;
}
return *this;
}

int code;
string err;
string func;
string file;
int line;
};

int _tmain(int argc, _TCHAR* argv[])
{
string x = "x123567";
AException* an = new AException(3,"异常","_tmain",__FILE__,__LINE__);
try
{
throw *an;
}
catch (AException e)//catch中的类型与其中的异常必需一致才能截获异常,*an 与 e的类型是一样的
{
x="e";
}


const char* c = x.c_str();
printf("%s",c);


int b;
cin >>b;
return 0;
}

相关主题