如何在 Android 中将 OpenCV 旋转和平移向量与 OpenGL ES 结合使用?
我正在 Android 上开发一个基本的增强现实应用程序。到目前为止我所做的是用 opencv 检测一个正方形,然后使用 cvFindExtrinsicCameraParams2() 计算旋转和平移向量。为此,我使用了 4 个物点,它们只是围绕 (0,0,0) 的正方形的角以及图像中正方形的 4 个角。
这给我带来了一个非常好的旋转和平移矩阵。我还使用 cvRodrigues2() 计算了旋转矩阵,因为使用它比旋转向量更容易。只要我使用它们在图像中绘制一些点,一切都会正常。然而,我的下一步是将这些向量和矩阵传递回 java,然后将它们与 OpenGL 一起使用,在 OpenGLView 中绘制一个正方形。该正方形应该正好位于 OpenGLView 后面显示的图像中的正方形周围。
我的问题是我无法找到在 OpenGL 中使用旋转矩阵和平移向量的正确方法。我从与 openCV 函数使用的完全相同的对象点开始。然后我以我能想到的几乎任何可能的方式应用了旋转矩阵和平移向量。遗憾的是,这些方法都没有产生接近我希望的结果。谁能告诉我如何正确使用它们?
到目前为止,我得到的“最接近”的结果是随机将整个矩阵乘以-1。但大多数时候,正方形看起来仍然是镜像倒转或旋转 180 度。所以我想这只是一个幸运的打击,但不是正确的方法。
I'm am working on a basic augmented reality application on Android. What I did so far is detect a square with opencv and then using cvFindExtrinsicCameraParams2() I calculated a rotation and translation vector. For this I used 4 object points, which are just the corners of a square around (0,0,0) and the 4 corners of the square in the image.
This yields me a pretty good rotation and translation matrix. I also calculated the rotation matrix with cvRodrigues2() since using this is easier than the rotation vector. As long as I use these to draw some points in the image everything works fine. My next step is however to pass these vectors and the matrix back to java and then use them with OpenGL to draw a square in an OpenGLView. The square should be exactly around the square in the image which is displayed behind the OpenGLView.
My problem is that I cannot find the correct way of using the rotation matrix and translation vector in OpenGL. I started of with exactly the same object points as used for the openCV functions. Then I applied the rotation matrix and translation vector in pretty much any possible way I could think of. Sadly none of these approaches produce a result which is anyway near what I hoped for. Can anyone tell me how to use them correctly?
So far the "closest" results I have gotten, was when randomly multiplying the whole matrix with -1. But most of the time the squares still look mirror inverted or rotated for 180 degrees. So I guess it was just a lucky hit, but not the right approach.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,经过一些更多的测试,我终于设法让它工作了。虽然我不明白……它确实“有效”。对于将来需要这样做的任何人,这是我的解决方案。
正如 genpfault 在他的评论中所说,自从 OpenGL 以来,一切都需要转置,因为 OpenGL 需要列优先顺序。 (感谢您的评论,我之前已经看过该页面。)此外,y 和 z 旋转角度以及 y 和 z 平移需要乘以 -1。这是我觉得有点奇怪的一点。为什么只有这些而不是 x 值?
我猜这应该有效。但拐角并不完全匹配。我猜这是由一些错误的 openGLView 配置引起的。因此,尽管我对我的解决方案仍然不是 100% 满意,但我想这就是我问题的答案。
Okay after some more testing I finally managed to get it to work. While I don't understand it... it does 'work'. For anyone who will need to do this in the future here is my solution.
As genpfault said in his comment everything needs to be transposed since OpenGL since OpenGL needs a column-major order. (Thanks for the comment, I saw that page earlier already.) Furthermore the y and z rotation angle as well as the y and z translation need to be multiplied by -1. This is what I find a bit weird. Why only those and not the x values too?
This works as it should I guess. But corners the don't match exactly. I guess this is caused by some wrong openGLView configurations. So even though I am still not a 100% happy with my solution I guess it is the answer to my question.
潘多罗的方法果然有效!如果有人想知道“如何将旋转向量转换为旋转矩阵”,我就是这样做的。顺便说一下,我在 OpenGL 2 中使用过这些,而不是在 ES 中。
祝你好运!
Pandoro's method really works! In case someone wondering "how to convert the rotation vector into a rotation matrix" here's how I did it. By the way, I've used these in OpenGL 2, not ES.
Good luck!