cv :: mat CV_8U产品错误和慢CV_32F产品 [英] cv::mat CV_8U product error and slow CV_32F product

查看:342
本文介绍了cv :: mat CV_8U产品错误和慢CV_32F产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在2772x128矩阵和4000x128矩阵之间制作产品。两者都是SIFT描述符矩阵,使用下一个代码:

I am trying to make a product between a 2772x128 matrix and a 4000x128 matrix. Both are matrices of SIFT descriptors, using next code:

Mat a = Mat(nframes, descrSize, CV_8U, DATAdescr);
Mat b = Mat(vocabulary_size, descrSize, CV_8U, vocabulary);
Mat ab =a * b.t();

问题是,在计算产品时,会抛出一个错误:

The problem is that when calculating the product, it throws an error saying

err_msg = 0x00cdd5e0 "..\..\..\src\opencv\modules\core\src\matmul.cpp:711: error: (-215) type == B.type() && (type == CV_32FC1 || type == CV_64FC1 || type == CV_32FC2 || type == CV_64FC2)"

此问题的解决方案是将数据类型转换为CV_32FC1

The solution to this has been to convert the data type to CV_32FC1

Mat a = Mat(nframes, descrSize, CV_8U, DATAdescr);
Mat b = Mat(vocabulary_size, descrSize, CV_8U, vocabulary);
a.convertTo(a, CV_32FC1);
b.convertTo(b, CV_32FC1);
Mat ab = a * b.t();

它工作得很好,但消耗太多时间,约1.2秒。我想尝试相同的产品,但使用整数,看看我是否可以加快速度。我做错了什么?我看不到任何原因,我不能做CV_8U矩阵之间的矩阵产品。

It works well, but it is consuming too much time, about 1.2 s. I would like to try the same product but using integers, to see if I can speed this up. Am I doing something wrong? I can't see any reason I cannot do matrix product between CV_8U matrices.

编辑:答案与使用其他库或解决其他方式有关。我正在考虑打开一个新线程,建议解决我的问题,但任何人都可以回答我原来的quiestion请求?我可以不乘以CV_8U或CV32S矩阵吗?

The answers are related to using other libraries or solving other way. I was thinking on opening a new thread with advice to solve my problem, but can anybody answer my original quiestion pleas? Can I not multiply CV_8U or CV32S matrices? Really?

推荐答案

其他消息您说下面的代码将需要0.9秒。

In your other message you said that the following code would take 0.9 seconds.

MatrixXd A = MatrixXd::Random(1000, 1000);
MatrixXd B = MatrixXd::Random(1000, 500);
MatrixXd X;

我试过在我的机器上做一点基准测试,intel core i7运行在linux上。我的完整基准代码如下:

I tried a little benchmark on my machine, intel core i7 running on linux. My full benchmark code is the following:

#include <Eigen/Dense>
using namespace Eigen;

int
main(int argc, char *argv[])
{
  MatrixXd A = MatrixXd::Random(2772, 128);
  MatrixXd B = MatrixXd::Random(4000, 128);
  MatrixXd X = A*B.transpose();
}

我只是使用linux的time命令,所以运行时间包括启动和停止可执行文件。

I just use the time command from linux so the running time includes the launching and stopping of the executable.

1 /无优化编译(gcc编译器):

1/ Compiling with no optimisation (gcc compiler):

g++ -I/usr/include/eigen3 matcal.cpp -O0 -o matcal
time ./matcal
real    0m13.177s  -> this is the time you should be looking at
user    0m13.133s
sys     0m0.022s


$ b b

13秒,这很慢。顺便说一下,没有矩阵乘法需要0.048s,更大的矩阵,在你的0.9s的例子。为什么??

13 seconds, that's very slow. By the way, without matrix multiplication it takes 0.048s, with bigger matrices that in your 0.9s example. Why ??

使用编译器优化Eigen是非常重要的。
2 /使用一些优化进行编译:

Using compilers optimisation with Eigen is very important. 2/ Compiling with some optimisation:

g++ -I/usr/include/eigen3 matcal.cpp -O2 -o matcal
time ./matcal
real    0m0.324s
user        0m0.298s
sys     0m0.024s

现在0.324s,更好!

Now 0.324s, that's better!

3 /切换所有的优化标志,我不是这方面的专家)

3/ Switching all the optimization flags (at least all that I know of, I'm not an expert in this field)

g++ -I/usr/include/eigen3 matcal.cpp -O3 -march=corei7 -mtune=corei7 -o matcal 
time ./matcal
real    0m0.317s
user    0m0.291s
sys     0m0.024s

0.317,关闭,但是增加了几ms(一些测试一致)。所以在我看来,你对使用Eigen有一个问题,你不要切换编译器优化,或者你的编译器不会自己做。

0.317, close, but a few ms gained (consistantly for a few tests). So in my opinion you do have a problem with your usage of Eigen, either you dont switch compiler optimization or your compiler does not do it by itself.

我不是一个专家在Eigen我只使用了几次,但我认为文件是相当不错,你可能应该读它,以充分利用它。

I'm not an expert in Eigen I have only used it a few time but I think the documentation is quite good and you probably should read it to get the most of it.

关于性能与MatLab的比较,上次我阅读关于Eigen它不是多线程,而MatLab可能使用多线程库。对于矩阵乘法,您可以使用TBB将您的矩阵拆分成几个块并并行化每个块的乘法。

Concerning performance comparison with MatLab, last time I read about Eigen it was not multithreaded while MatLab probably use multithreaded libraries. For matrix multiplication you could split up your matrix in several chunks and parallelize multiplication of each chunk using TBB

这篇关于cv :: mat CV_8U产品错误和慢CV_32F产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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