opencv错误乘以2个垫子 [英] opencv error multiplying 2 Mat's

查看:57
本文介绍了opencv错误乘以2个垫子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 opencv 中乘以 2 个矩阵(Mat 对象).

I am multiplying 2 matrices (Mat objects) in opencv.

这是第一个 Mat 是如何生成的:

Here is how first Mat was generated :

cv::Mat R(m, k, CV_8UC1);
rm.generateRandomMatrix(m, k, 255, R);

这里是第二个是如何生成的:

Here is how the second one was generated:

for (int i=0; i<=n; i++)
{
    randomMatrix_Xi rm;
    cv::Mat Xi(k, 1, CV_8UC1);
    rm.generateRandomMatrix(k, 1, 255, Xi);
    random_Xi_Vectors.push_back(Xi);
    sleep(2);
}

这是我在两个地方都使用过的 generateRandomMatrix() 函数定义:

Here is the generateRandomMatrix() function definition which I have used in both places:

int randomMatrix_Xi::generateRandomMatrix(int m, int k, int range, cv::Mat R)
{
    typedef boost::mt19937 RNGType;
    RNGType rng(std::time(0));
    boost::uniform_int<> one_to_range( 1, range);
    boost::variate_generator< RNGType, boost::uniform_int<> > number_generator(rng, one_to_range);
    for (int j=0; j<k; j++)
    {
        for ( int i = 0; i < m; i++ ) 
        {
           int n = number_generator();
           R.at<uchar>(i,j) = n;
           //std::cout<<"Putting "<<n<<" at"<<i<<", "<<j<<std::endl;
        }
    }
}

最后,这里是我做两个 Mat 相乘的地方:

Finally, here is the place where I am doing the multiplication of the two Mat's:

for (int i = 0; i < n; i++)
    {
        std::cout<<" Sizes of matrices to be multiplied: "<<std::endl;

        cv::Size Xi_size = random_Xi_Vectors[i].size();
        cv::Size A_size = R.size();
        std::cout<<"R : "<<A_size.height<<" "<<A_size.width<<std::endl;
        std::cout<<"Xi : "<<Xi_size.height<<" "<<Xi_size.width<<std::endl;

            cv::Mat temp(960,1, CV_8UC1); 
        cv::Mat temp = random_Xi_Vectors[i] * R;
        shares.push_back(temp);
    }

这是我得到的错误:

r@r-HP-Mini-110:~/l33t/Secret Sharing$ ./mainProgram  Sizes of matrices to be multiplied: 
R : 960 2
Xi : 2 1
OpenCV Error: Assertion failed (type == B.type() && (type == CV_32FC1 || type == CV_64FC1 || type == CV_32FC2 || type == CV_64FC2)) in gemm, file /build/buildd/opencv-2.3.1/modules/core/src/matmul.cpp, line 701
terminate called after throwing an instance of 'cv::Exception'
  what():  /build/buildd/opencv-2.3.1/modules/core/src/matmul.cpp:701: error: (-215) type == B.type() && (type == CV_32FC1 || type == CV_64FC1 || type == CV_32FC2 || type == CV_64FC2) in function gemm

Aborted (core dumped)
r@r-HP-Mini-110:~/l33t/Secret Sharing$ 

如您所见,第一个 Mat 中的列数与第二个 Mat 中的行数相匹配.此外,两个 Mat 的类型相同,即 CV_8UC1.那么为什么我会收到此错误.请帮忙.

As you can see, the number of columns in the first Mat matches the number of rows in the second Mat. Also, the type of both the Mat is the same i.e CV_8UC1. So then why am I getting this error. Please help.

推荐答案

(type == CV_32FC1 || type == CV_64FC1 || type == CV_32FC2 || type == CV_64FC2)

清楚地说,你只能乘以浮点垫

that clearly says, you can only multiply floating point Mat's

所以使用 CV_32FC1 (float) 或 CV_64FC1 (double) Mats 而不是 CV_8UC1

so use CV_32FC1 (float) or CV_64FC1 (double) Mats instead of the CV_8UC1

(代码如下:

boost::variate_generator<RNGType,boost::uniform_int<>> number_generator(rng,one_to_range);

那时可能也需要更改,不确定)

might need to change then, too, not sure )

这篇关于opencv错误乘以2个垫子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆