从数组创建 Mat 时,'cv::Mat' 和 'int' 类型不兼容

Types 'cv::Mat' and 'int' are not compatible when create a Mat from array

本文关键字:Mat 类型 不兼容 int 数组 创建 cv      更新时间:2023-10-16

在有关Mat的文档中,它展示了如何使用逗号分隔的初始值设定项创建Mat,如下所示:

// create 3x3 double-precision identity matrix
Mat M = (Mat_<double>(3,3) << 1, 0, 0, 0, 1, 0, 0, 0, 1);

但是当我尝试时,它显示一个错误:

Types 'cv::Mat' and 'int' are not compatible

如何解决异常?

谢谢

你的代码没有问题

cv::Mat M = (cv::Mat_<double>(3,3) << 1, 0, 0, 0, 1, 0, 0, 0, 1);

我可以确认它适用于 gcc 5.4.0 和 OpenCV 3.1.0。应使用 Matx 创建固定大小的小矩阵:

typedef cv::Matx<double, 3, 3> Mat33d;
Mat33d m(1, 0, 0, 0, 1, 0, 0, 0, 1);