OpenCV 矩阵乘法初学OpenCV现有如下程序,#include //通过三个函数分别实现矩阵乘法#include"cv.h" #include"highgui.h" void PrintMat(CvMat *A) //显示矩阵 {\x05int i,j;\x05for(i=0;irows;i++)\x05{\x05\x05printf("\n");\x05\x05swit

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/12 15:19:20
OpenCV 矩阵乘法初学OpenCV现有如下程序,#include //通过三个函数分别实现矩阵乘法#include

OpenCV 矩阵乘法初学OpenCV现有如下程序,#include //通过三个函数分别实现矩阵乘法#include"cv.h" #include"highgui.h" void PrintMat(CvMat *A) //显示矩阵 {\x05int i,j;\x05for(i=0;irows;i++)\x05{\x05\x05printf("\n");\x05\x05swit
OpenCV 矩阵乘法
初学OpenCV现有如下程序,
#include //通过三个函数分别实现矩阵乘法
#include"cv.h"
#include"highgui.h"
void PrintMat(CvMat *A) //显示矩阵
{
\x05int i,j;
\x05for(i=0;irows;i++)
\x05{
\x05\x05printf("\n");
\x05\x05switch(CV_MAT_DEPTH(A->type))
\x05\x05{
\x05\x05case CV_32F:
\x05\x05case CV_64F:
\x05\x05\x05for(j=0;jcols;j++)
\x05\x05\x05\x05printf("%9.3f",(float)cvGetReal2D(A,i,j));
\x05\x05\x05break;
\x05\x05case CV_8U:
\x05\x05case CV_16U:
\x05\x05\x05for(j=0;jcols;j++)
\x05\x05\x05\x05printf("%6d",(int)cvGetReal2D(A,i,j));
\x05\x05\x05break;
\x05\x05default:
\x05\x05\x05break;
\x05\x05}
\x05}
\x05printf("\n");
}
int main() // 测试矩阵乘法
{
double a[] = { 1 ,2 ,3 ,4 ,
5 ,6 ,7 ,8 ,
9 ,10 ,11 ,12 };
double b[] = { 1 ,5 ,9 ,
2 ,6 ,10 ,
3 ,7 ,11 ,
4 ,8 ,12 };
double c[9],d[9],e[9];
\x05
CvMat Ma,Mb,Mc,Md,Me;
cvInitMatHeader( &Ma,3,4,CV_64FC1,a,CV_AUTOSTEP );
cvInitMatHeader( &Mb,4,3,CV_64FC1,b,CV_AUTOSTEP );
cvInitMatHeader( &Mc,3,3,CV_64FC1,c,CV_AUTOSTEP );
\x05cvInitMatHeader( &Md,3,3,CV_64FC1,d,CV_AUTOSTEP );
\x05cvInitMatHeader( &Me,3,3,CV_64FC1,e,CV_AUTOSTEP );
\x05printf("矩阵A=");
PrintMat(&Ma);
\x05printf("\n");
\x05
\x05printf("矩阵B=");
PrintMat(&Mb);
\x05printf("\n");
\x05cvMatMulAdd( &Ma,&Mb,0,&Mc );
\x05printf("通过cvMatMulAdd()函数实现矩阵乘法C=A*B");
PrintMat(&Mc);
\x05printf("\n");
\x05
\x05cvMatMul(&Ma,&Mb,&Md);
\x05printf("通过cvMatMul()函数实现矩阵乘法D=A*B");
\x05PrintMat(&Md);
\x05printf("\n");
\x05
\x05cvGEMM(&Ma,&Mb,1,&Me,0,&Me,0);
\x05printf("通过cvGEMM()函数实现矩阵乘法E=A*B");
\x05PrintMat(&Me);
\x05printf("\n");
return 0;
}
主函数中的数组类型似乎只能是double,我尝试着把矩阵元素设成float或者unsigned之类的几种类型,结果要么运行出错要么结果显示乱码.请问为什么?是不是像这种处理矩阵的情况一般都要设置成double类型?

OpenCV 矩阵乘法初学OpenCV现有如下程序,#include //通过三个函数分别实现矩阵乘法#include"cv.h" #include"highgui.h" void PrintMat(CvMat *A) //显示矩阵 {\x05int i,j;\x05for(i=0;irows;i++)\x05{\x05\x05printf("\n");\x05\x05swit
只有double类型的才不会溢出

没问题啊?
你该数组type后,改没改initMatHeader里对应的第4个参数?