- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有以下代码:
cv::Mat temp0 = R.t();
cv::Mat temp1 = R * temp0;
cv::Mat temp2(960, 960, CV_32FC1);
temp2 = temp1.inv();
cv::Size s = temp1.size();
std::cout<<s.height<<" "<<s.width<<std::endl;
std::cout<<cv::format(temp1, "numpy" ) << std::endl;
std::cout<<cv::format(temp2, "numpy" ) << std::endl;
Transpose 工作正常,矩阵乘法也是如此。因此 Mat temp1
的大小为 960x960。但是,当我执行 temp2 =temp1.inv()
时,我在 temp2
中收到所有零。我的意思是零是所有 960x960 单元格。此外,R
仅属于 CV_32FC1
类型。所以这可能不是数据类型问题。我无法理解这里的问题。我用谷歌搜索了很多。你能帮忙吗?
编辑
我正在复制 Mat::inv()
函数的 gdb 输出。我很难弄清楚,但如果有人更熟悉 OpenCV,也许会有帮助:)
Breakpoint 1, CreateShares::ConstructShares (this=0x80556d0, channel=..., k=2, n=4) at CreateShares.cpp:165
165 temp2 = temp1.inv();
(gdb) step
cv::Mat::operator= (this=0xbffff294, e=...) at /usr/include/opencv2/core/mat.hpp:1373
1373 e.op->assign(e, *this);
(gdb)
1374 return *this;
(gdb) step
1375 }
(gdb) step
cv::MatExpr::~MatExpr (this=0xbfffef64, __in_chrg=<optimized out>) at /usr/include/opencv2/core/mat.hpp:1167
1167 class CV_EXPORTS MatExpr
(gdb) step
cv::Mat::~Mat (this=0xbfffefdc, __in_chrg=<optimized out>) at /usr/include/opencv2/core/mat.hpp:295
295 release();
(gdb) step
cv::Mat::release (this=0xbfffefdc) at /usr/include/opencv2/core/mat.hpp:381
381 if( refcount && CV_XADD(refcount, -1) == 1 )
(gdb) step
383 data = datastart = dataend = datalimit = 0;
(gdb) step
384 size.p[0] = 0;
(gdb) step
385 refcount = 0;
(gdb) step
386 }
(gdb) step
cv::Mat::~Mat (this=0xbfffefdc, __in_chrg=<optimized out>) at /usr/include/opencv2/core/mat.hpp:296
296 if( step.p != step.buf )
(gdb) step
298 }
(gdb) step
cv::Mat::~Mat (this=0xbfffefa4, __in_chrg=<optimized out>) at /usr/include/opencv2/core/mat.hpp:295
295 release();
(gdb) step
cv::Mat::release (this=0xbfffefa4) at /usr/include/opencv2/core/mat.hpp:381
381 if( refcount && CV_XADD(refcount, -1) == 1 )
(gdb) step
383 data = datastart = dataend = datalimit = 0;
(gdb) step
384 size.p[0] = 0;
(gdb) step
385 refcount = 0;
(gdb) step
386 }
(gdb) step
cv::Mat::~Mat (this=0xbfffefa4, __in_chrg=<optimized out>) at /usr/include/opencv2/core/mat.hpp:296
296 if( step.p != step.buf )
(gdb) step
298 }
(gdb) step
cv::Mat::~Mat (this=0xbfffef6c, __in_chrg=<optimized out>) at /usr/include/opencv2/core/mat.hpp:295
295 release();
(gdb) step
cv::Mat::release (this=0xbfffef6c) at /usr/include/opencv2/core/mat.hpp:381
381 if( refcount && CV_XADD(refcount, -1) == 1 )
(gdb) step
383 data = datastart = dataend = datalimit = 0;
(gdb) step
384 size.p[0] = 0;
(gdb) step
385 refcount = 0;
(gdb) step
386 }
(gdb) step
cv::Mat::~Mat (this=0xbfffef6c, __in_chrg=<optimized out>) at /usr/include/opencv2/core/mat.hpp:296
296 if( step.p != step.buf )
(gdb) step
298 }
(gdb) step
CreateShares::ConstructShares (this=0x80556d0, channel=..., k=2, n=4) at CreateShares.cpp:167
167 cv::Size s = temp1.size();
(gdb) step
cv::Mat::MSize::operator() (this=0xbffff284) at /usr/include/opencv2/core/mat.hpp:705
705 return Size(p[1], p[0]);
(gdb) step
cv::Size_<int>::Size_ (this=0xbffff2f8, _width=960, _height=960) at /usr/include/opencv2/core/operations.hpp:1624
1624 : width(_width), height(_height) {}
(gdb) step
cv::Mat::MSize::operator() (this=0xbffff284) at /usr/include/opencv2/core/mat.hpp:706
706 }
(gdb) step
最佳答案
行列式很可能为零。
来自维基百科:
A square matrix that is not invertible is called singular or degenerate. A square matrix is singular if and only if its determinant is 0.
您可以像这样显示行列式...
std::cout<<"determinant(temp1)="<<cv::determinant(temp1)<<"\n";
来自 the documentation for Mat::inv() ,共有三种方法可供选择:
来自 the documentation for invert() , 大概由 Mat::inv() 内部使用:
In the case of DECOMP_LU method, the function returns the src determinant ( src must be square). If it is 0, the matrix is not inverted and dst is filled with zeros.
这与您看到的结果一致。
我不是数学家,但我的印象是求逆矩阵可能是一件很麻烦的事情——如果您的矩阵非常大,则更是如此。事实上,这些逆函数原则上可能确实存在,但实际上不可能精确计算。在对您的代码进行一些实验时,我发现在许多情况下我会得到不完全为零但非常接近于零的行列式——这可能表明数值精度可能是限制因素。我尝试使用 64 位值而不是 32 位值指定矩阵,得到了不同但不一定更好的答案。
根据您计算 temp1
矩阵的方式,认识到它总是对称可能会很有用。 DECOMP_CHOLESKY
方法专门设计用于处理对称 positive definite。矩阵,因此使用它可能会提供一些优势。
在实验上,我发现归一化(如@cedrou 所建议的那样)使逆函数更有可能返回一个非零矩阵(使用 DECOMP_LU
而不是使用 DECOMP_CHOLESKY
).但是,根据我对您可能如何初始化 R
矩阵的猜测,生成的矩阵似乎永远不会满足逆矩阵的定义:A*inverse(A)=Identity
。但您不一定关心这一点——这也许就是 SVD 方法计算伪逆的原因。
最后,为什么反演失败这个更深层次的问题似乎是一个数学问题,而不是一个编程问题。基于此,我在数学网站上进行了一些搜索,结果发现有人已经问过如何做这件事:https://math.stackexchange.com/questions/182662
根据您的调试跟踪,我倾向于认为您感兴趣的部分被编译到一个不可跟踪的库中,并在您运行 step
时跳过。换句话说,第一个 step
之后的神秘空白行代表它实际运行 inv()
函数的部分。之后,它将结果分配给 temp2
并销毁临时对象。因此,您的调试跟踪不会告诉我们有关 inv()
内部发生的事情的任何信息。
165 temp2 = temp1.inv();
(gdb) step
cv::Mat::operator= (this=0xbffff294, e=...) at /usr/include/opencv2/core/mat.hpp:1373
1373 e.op->assign(e, *this);
我自己对此运行了一个调试器,并且能够通过对 invert()
的内部调用进行跟踪,并观察它根据对矩阵的内部分析决定失败(确定它不是可逆)——因此返回一个用零填充的矩阵,与您报告的内容相匹配。
invert()
函数在 cxlapack.cpp 中定义,如果您有兴趣查看源代码。
关于c++ - Mat.inv() 在 opencv 中产生全零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15051669/
我有一个矩阵形状 (4000, 4000),我想取逆矩阵。 (对于如此大的矩阵,我对逆矩阵的直觉会崩溃。) 起始矩阵的值大小为 e-10,具有以下值:打印矩阵 给出输出 [[ 2.19885119e
我的 JavaScript 函数如下: function myClickEvent(params) { var inv = $("#" + params["inv"]); inv.on
我需要知道当玩家从另一个插件运行命令时是否有办法清除玩家的库存。我认为您可以使用 PlayerCommandPreprocessEvent 但我自己无法获得它。我需要一些帮助来解决我的问题。谢谢:)
我是 Python 的新手,我想知道它们之间的区别是什么 inv(A) 和 A.I 两者都返回包含矩阵 A 的逆矩阵的 Numpy 数组。 A.I 在我看来它正在从存储矩阵逆矩阵的矩阵类中访问一个变量
我正在实现 LinearTransformation类,继承自 numpy.matrix并使用 numpy.matrix.I计算变换矩阵的逆矩阵。 有谁知道 numpy 在尝试计算逆矩阵之前是否检查矩
特别是遵循此处的教程:https://kelvinlawrence.net/book/Gremlin-Graph-Guide.html#walk (3.3.2) 我不明白这句话outE().inV()
下面的矩阵是奇异的,据我所知尝试反转它应该会导致 numpy.linalg.linalg.LinAlgError: Singular matrix 但是,我确实得到了一些输出矩阵。请注意,输出矩阵是一
我在 Python 中使用逆矩阵函数 inv()。我正在计算 3x3 矩阵的逆矩阵,但是当我将结果与原始矩阵相乘时,我没有得到单位矩阵,为什么? 示例: AA = [[1,6,5],[2,3,1],[
嗯,我正在用java编写一个模拟主题的伪随机数测试,我需要计算卡方的倒数,所以我有alpha和度as you can see here . 我正在看的书,使用Excel函数Excel ChiSQ.IN
我有一个 MutableDenseMatrix,Q。 theta1 和 theta2 是 SymPy 类型的 symbol。 In[12]: Q Out[12]: [cos(theta1), -si
下面的矩阵是奇异的,据我所知尝试反转它应该会导致 numpy.linalg.linalg.LinAlgError: Singular matrix 但是,我确实得到了一些输出矩阵。请注意,输出矩阵是一
求对角矩阵的逆非常简单,不需要复杂的方法。 scipy.linalg.inv 在应用更复杂的方法之前是否检查矩阵是否为对角线,还是我需要自己检查? 最佳答案 如你所见the Github code o
我正在尝试使用 numpy.linalg.inv() 函数反转二维矩阵。然而,每当我运行它时,它似乎就停止了。由于我使用的是尺寸为 40989x52 的矩阵,我最初认为这是一个问题,因此我尝试等待它处
我正在尝试使用 Matlab 内置函数“inv”来计算方阵的逆。 matlab inv() 是否使用任何重新排序算法(inv 内部且用户不指定)来计算逆? 提前致谢.. 最佳答案 可能会进行行旋转以最
我在编写执行音频处理的 SSE 方法时遇到问题。我在这里根据英特尔的论文实现了一个 SSE 随机函数: http://software.intel.com/en-us/articles/fast-ra
我正在 HPC 上反转非常大的矩阵。显然,这对 RAM 有很高的要求。为了避免内存不足错误,作为临时解决方案,我一直在请求大量内存 (TB)。我如何使用 numpy.linalg.inv 根据输入矩阵
作为疾病风险模型的一部分,我试图从一篇论文(在 Python/numpy 中)实现计算,其中一部分是以下矩阵计算: 在哪里: X an [n.m]。 m 的大小合理 (1..200),但 n 相当大
我正在使用对数深度算法,该算法导致someFunc(clipspace.z)被写入深度缓冲区,并且没有隐式透视划分。 我正在执行RTT/后处理,因此稍后在片段着色器中,我想重新计算eyespace.x
使用 JProfiler 分析在 Oracle IAS 上运行的应用程序,进入 JEE & Probes -> JDBC -> Hot Spots,在“Unknown”树下有一个名为“Hot Spot
我创建了一个奇异矩阵。 mat = np.array([[ 1, 8, 50], [ 8, 64, 400], [ 50, 400, 2500]]) 已经知道 mat
我是一名优秀的程序员,十分优秀!