gpt4 book ai didi

c++ - opencv错误乘以2 Mat的

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:31:52 24 4
gpt4 key购买 nike

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

下面是第一个 Mat 是如何生成的:

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

这是第二个的生成方式:

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() 函数定义:

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 进行乘法运算的地方:

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。那么为什么我会收到此错误。请帮忙。

最佳答案

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

清楚地说,你只能乘浮点 Mat 的

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

( 代码如下:

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

可能也需要改变,不确定)

关于c++ - opencv错误乘以2 Mat的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15049811/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com