如何在 Android 中使用带有 OpenGL ES 的 OpenCV 旋转和平移向量? [英] How to use an OpenCV rotation and translation vector with OpenGL ES in Android?

查看:40
本文介绍了如何在 Android 中使用带有 OpenGL ES 的 OpenCV 旋转和平移向量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Android 上开发基本的增强现实应用程序.到目前为止我所做的是用 opencv 检测一个正方形,然后使用 cvFindExtrinsicCameraParams2() 我计算了一个旋转和平移向量.为此,我使用了 4 个对象点,它们只是 (0,0,0) 周围正方形的角和图像中正方形的 4 个角.

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.

这产生了一个很好的旋转和平移矩阵.我还使用 cvRodrigues2() 计算了旋转矩阵,因为使用它比旋转向量更容易.只要我使用这些在图像中绘制一些点,一切正常.然而,我的下一步是将这些向量和矩阵传递回 java,然后将它们与 OpenGL 一起使用以在 OpenGLView 中绘制一个正方形.正方形应该正好围绕在 OpenGLView 后面显示的图像中的正方形周围.

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.

我的问题是我找不到在 OpenGL 中使用旋转矩阵和平移向量的正确方法.我从与用于 openCV 函数的完全相同的对象点开始.然后我以我能想到的几乎任何可能的方式应用旋转矩阵和平移向量.可悲的是,这些方法都没有产生接近我所希望的结果.谁能告诉我如何正确使用它们?

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?

到目前为止,我得到的最接近"的结果是将整个矩阵随机乘以 -1.但大多数情况下,这些方块看起来仍然是镜像倒置或旋转了 180 度.所以我想这只是一个幸运的打击,但不是正确的方法.

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.

推荐答案

好的,经过一些更多的测试,我终于设法让它工作了.虽然我不明白它......它确实有效".对于将来需要这样做的任何人,这是我的解决方案.

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.

float rv[3]; // the rotation vector
float rotMat[9]; // rotation matrix
float tv[3]; // translation vector.


rv[1]=-1.0f * rv[1]; rv[2]=-1.0f * rv[2];
//Convert the rotation vector into a matrix here.

//Complete matrix ready to use for OpenGL
float RTMat[] = {rotMat[0], rotMat[3], rotMat[6], 0.0f,
                 rotMat[1], rotMat[4], rotMat[7], 0.0f,
                 rotMat[2], rotMat[5], rotMat[8], 0.0f,
                 tv[0], -tv[1], -tv[2], 1.0f};

正如 genpfault 在他的评论中所说,自从 OpenGL 以来,一切都需要转置,因为 OpenGL 需要列优先顺序.(感谢您的评论,我之前已经看过该页面.)此外,y 和 z 旋转角度以及 y 和 z 平移需要乘以 -1.这是我觉得有点奇怪的地方.为什么只有那些而不是 x 值?

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?

这就像我猜的那样.但角落不完全匹配.我猜这是由一些错误的 openGLView 配置引起的.因此,即使我对我的解决方案仍然不是 100% 满意,我想这就是我问题的答案.

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.

这篇关于如何在 Android 中使用带有 OpenGL ES 的 OpenCV 旋转和平移向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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