Opencv STL向量转Mat进行矩阵乘法

发布于 2024-12-27 15:02:25 字数 883 浏览 0 评论 0原文

矩阵乘法是图像处理中非常基本的任务,OpenCV 使用重载的 * 运算符来处理。点的 STL 向量可以通过转换转换为 Mat。

vector<Point2f> p1(2);
p1[0].x=1;p1[0].y=0;
p1[1].x=0;p1[1].y=1;
Mat p1M=Mat(p1);

正如 OpenCV 文档中提到的,这将创建具有单列(有 2 个元素)的矩阵,其行数等于向量数量:

[1 0;0 1]------>p1M.rows=2;p1M.cols=1

当您想要矩阵乘法 (p1M*p1M)...[2x1]*[ 时,这会产生问题2x1]???...本质上我相信所有到 Matrix 的转换向量所做的就是按原样合并向量......

但是,命令 p1M.at(0,1)p1M.at(1,0) 分别返回 0 和 1。这让我认为 p1M*p1M 是可能的,但不幸的是它只编译并生成运行时错误:

OpenCV 错误:gemm 中断言失败 (a_size.width == len),文件 /home/james/OpenCV-2.3.1/modules/core/src/matmul.cpp,第 708 行 抛出“cv::Exception”实例后调用终止 What(): /home/james/OpenCV-2.3.1/modules/core/src/matmul.cpp:708: 错误: (-215) a_size.width == len 函数 gemm

我正在考虑编写一个函数就这么做吧!向量到 Mat,反之亦然......我错过了什么吗?

Matrix multiplication is a very basic task in image processing and OpenCV takes care of with a overloaded * operator. A STL vector of points can be converted into Mat by casting.

vector<Point2f> p1(2);
p1[0].x=1;p1[0].y=0;
p1[1].x=0;p1[1].y=1;
Mat p1M=Mat(p1);

As mentioned in OpenCV documentation, this will create matrix with a single column(with 2 elements) with rows equal to no of vectors:

[1 0;0 1]------>p1M.rows=2;p1M.cols=1

This creates a problem when you want to matrix multiply (p1M*p1M)...[2x1]*[2x1]???...Essentially i believe all the casting vector to Matrix does is to merge the vectors as it is....

However, the command p1M.at<float> (0,1) and p1M.at<float> (1,0) returns 0 and 1 resp. this made me think p1M*p1M is possible but unfortunately it only compiles and generates a run time error:

OpenCV Error: Assertion failed (a_size.width == len) in gemm, file /home/james/OpenCV-2.3.1/modules/core/src/matmul.cpp, line 708
terminate called after throwing an instance of 'cv::Exception'
what(): /home/james/OpenCV-2.3.1/modules/core/src/matmul.cpp:708: error: (-215) a_size.width == len in function gemm

I am thinking of writing a function to just do that! vector to Mat and vice versa...Am i missing something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幸福还没到 2025-01-03 15:02:26

您可能想研究一下 cv::Mat::reshape
http://docs.opencv.org/modules/core/doc /basic_structs.html#mat-reshape

当您从一系列点创建 Mat 时,它会为点的每个组件创建一个通道。因此,如果您使用 Point3f,它会生成具有 3 个通道的单列 Mat。

您可以尝试通过调用将 p1M 转换为您期望的矩阵

p1M.reshape(1);

You might want to look into cv::Mat::reshape
http://docs.opencv.org/modules/core/doc/basic_structures.html#mat-reshape

When you create a Mat from a list of points it creates one channel for each component of point. So if you use a Point3f it makes a single column Mat with 3 channels.

You could try converting p1M to the matrix you are expecting by calling

p1M.reshape(1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文