首页 > 解决方案 > OpenCV 简单的二维矩阵乘法失败

问题描述

我只是想将两个简单的二维矩阵相乘。

#include <iostream>
#include "opencv2/core.hpp"

using namespace std;
using namespace cv;

int main(const int argc,const char* argv[])
{

    Mat A = (Mat_<char>(2,2) << 1,2,3,4);
    Mat B = (Mat_<char>(2,2) << 2,2,2,2);
    Mat C = A*B;

    cout << C << endl;

    return 0;
}

为什么我会得到核心转储(gemm)?

抛出'cv :: Exception'实例后调用终止what():OpenCV(4.1.0)/OpenCV/opencv-4.1.0/modules/core/src/matmul.dispatch.cpp:337:错误:(- 215: 断言失败) (type == (((5) & ((1 << 3) - 1)) + (((1)-1) << 3)) || type == (((6) & ((1 << 3) - 1)) + (((1)-1) << 3)) || 类型 == (((5) & ((1 << 3) - 1)) + ( ((2)-1) << 3)) || 类型 == (((6) & ((1 << 3) - 1)) + (((2)-1) << 3))) 在功能“宝石”

中止(核心转储)

标签: c++opencvmatrix

解决方案


https://github.com/opencv/opencv/blob/master/modules/core/src/matmul.dispatch.cpp#L337

根据消息来源,似乎代码需要浮动,

CV_Assert_N( type == B.type(), (type == CV_32FC1 || type == CV_64FC1 || type == CV_32FC2 || type == CV_64FC2) );

尝试Mat_<float>(为我工作)


推荐阅读