现在的位置: 首页 > 综合 > 正文

opencv学习记录1 //cv::gemm()

2018年04月25日 ⁄ 综合 ⁄ 共 1168字 ⁄ 字号 评论关闭

因为学习需要,所以要用到矩阵相乘,看别人的程序,会用到cvGEMM,但这是OpenCV的老函数了,网上都是老式的用法,新版的操作方法几乎没有说明,查了一下函数库,有gemm函数可以代替,描述如下:

Performs generalized matrix multiplication.
C++: void gemm(InputArray src1, InputArray src2, double alpha, InputArray src3, double gamma, OutputArray dst, int flags=0 )

src1 – first multiplied input matrix that should have CV_32FC1, CV_64FC1, CV_32FC2, orCV_64FC2 type.
src2 – second multiplied input matrix of the same type as src1.
alpha – weight of the matrix product.
src3 – third optional delta matrix added to the matrix product; it should have the same type
as src1 and src2.
beta – weight of src3.
dst – output matrix; it has the proper size and the same type as input matrices.
flags – operation flags:
– GEMM_1_T transposes src1.
– GEMM_2_T transposes src2.
– GEMM_3_T transposes src3

于是写了以下的语句:

 Mat   T;
 Mat   A=Mat::eye(1,3,CV_32FC1);
 Mat   C=Mat::eye(3,3,CV_32FC1);
 std::cout<<"A="<<A<<std::endl<<"C="<<C<<std::endl;
 gemm(A,C,1,0,1,T,0);
 std::cout<<"T="<<T;

编译没问题,但运行就悲剧了,不知名原因异常弹出!!后来试了又试,终于调通了:

 Mat   T;
 Mat   A=Mat::eye(1,3,CV_32FC1);
 Mat   C=Mat::eye(3,3,CV_32FC1);
 std::cout<<"A="<<A<<std::endl<<"C="<<C<<std::endl;
 gemm(A,C,1,10,0,T,0);//the src3 can't be 0,so if you 
			//want to ignore the src3,
                        //you should set the b  eater as 0
 std::cout<<"T="<<T;

如果你不想做加操作,也就是如果你想忽略src3时,不可以把src3设为0,而应该把beater设为0,这样src3就不会影响运算。

抱歉!评论已关闭.