基于Opencv的矩阵三角函数封装 cvAtan2Mat cvSinMat cvAtan2Mat功能等于matlab里的atan2 sin cos

Opencv中没有提供对矩阵进行操作的三角函数,我封装了三个,其他三角函数可以仿照此进行封装。

头文件中要包含

#include <cmath>


CvMat* cvAtan2Mat(CvMat *a, CvMat *b)
{
int rows = a->rows;
int cols = a->cols;
CvMat *out = cvCreateMat(rows, cols, a->type);
for(int i=0; i<rows; i++)
{
float* ptra = ( float*)(a->data.ptr+i*a->step);
float* ptrb = ( float*)(b->data.ptr+i*b->step);
float* ptrout = ( float*)(out->data.ptr+i*out->step);
for(int j=0; j<cols; j++)
{
*ptrout = atan2(*ptra,*ptrb);
ptra++;
ptrb++;
ptrout++;
}
}
return out;
}


CvMat* cvSinMat(CvMat *a)
{
int rows = a->rows;
int cols = a->cols;
CvMat *out = cvCreateMat(rows, cols, a->type);
for(int i=0; i<rows; i++)
{
float* ptra = ( float*)(a->data.ptr+i*a->step);
float* ptrout = ( float*)(out->data.ptr+i*out->step);
for(int j=0; j<cols; j++)
{
*ptrout = sin(*ptra);
ptra++;
ptrout++;
}
}
return out;
}


CvMat* cvCosMat(CvMat *a)
{
int rows = a->rows;
int cols = a->cols;
CvMat *out = cvCreateMat(rows, cols, a->type);
for(int i=0; i<rows; i++)
{
float* ptra = ( float*)(a->data.ptr+i*a->step);
float* ptrout = ( float*)(out->data.ptr+i*out->step);
for(int j=0; j<cols; j++)
{
*ptrout = cos(*ptra);
ptra++;
ptrout++;
}
}
return out;
}

你可能感兴趣的:(matlab)