moningkai12 于 2016.02.15 16:52 提问

opencv cvGEMM总是报错，附代码，求大神纠正

float x, y;
float a1, a2, a3, a4, a5, b1;
float data[10][5];
float dab[10];
CvMat* curve_p = cvCreateMat(5,1,CV_32FC1);
CvMat* A = cvCreateMat(n, 5, CV_32FC1);
CvMat* b = cvCreateMat(n, 1, CV_32FC1);

//获取十个数据
for (int i = 1; i <= 10; i++)
{
int pt = (20 - 1)*i / 10;

x = 2;
y = 6;

a1 = x*x - y*y;
a2 = x*y;
a3 = x;
a4 = y;
a5 = 1;
b1 = -y*y;

data[i - 1][0] = a1;
data[i - 1][1] = a2;
data[i - 1][2] = a3;
data[i - 1][3] = a4;
data[i - 1][4] = a5;

dab[i - 1] = b1;
}

cvSetData(A, data, A->step);
cvSetData(b, dab, b->step);

CvMat *E1, *E2, *E3;

E1 = cvCreateMat(5, 5, CV_32FC1);
E2 = cvCreateMat(5, 5, CV_32FC1);
E3 = cvCreateMat(5, n, CV_32FC1);

cvGEMM(A, A, 1, NULL, 0, E1, CV_GEMM_A_T);
cvInvert(E1, E2, 0);
cvGEMM(E2, A, 1, NULL, 0, E3, CV_GEMM_B_T);
cvGEMM(E3, b, 1, NULL, 0, curve_p);

cvReleaseMat(&A);
cvReleaseMat(&b);
cvReleaseMat(&E1);
cvReleaseMat(&E2);
cvReleaseMat(&E3);

1个回答

wangzhiyu1980   2016.02.15 17:03

#include
#include
#include
#include
using namespace std;

int main()
{
double a[3][3] =
{

{1,0,0},
{0,2,0},
{0,0,3}
};

CvMat va=cvMat(3,3, CV_64FC1,a);

cout<<"目标矩阵1："<<endl;

for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
printf("%f\t",cvmGet(&va,i,j));
cout << endl;
}

double b[3][3] =
{

{1,1,1},
{1,1,1},
{1,1,1}
};

CvMat vb =cvMat(3, 3, CV_64FC1, b);

cout<<"目标矩阵2："<<endl;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
printf("%f\t",cvmGet(&vb,i,j));
cout << endl;
}

double c[3][3] = {0,0,0,0,0,0,0,0,0};

CvMat vc = cvMat(3,3, CV_64FC1, c);

cvGEMM(&va,&vb,1,&vc,1,&vc);

cout << "结果矩阵："<< endl;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
printf("%f\t",cvmGet(&vc,i,j));
cout << endl;
}
getchar();
return 0;
}

moningkai12 谢谢，你的这段代码可以用，我把矩阵指针都改成CvMat就可以进行乘法了。但我记得是CvMat*也可以直接进行矩阵运算啊