如何使用OpenCV的旋转和平移向量与OpenGL ES的Andr​​oid中?向量、如何使用、OpenCV、OpenGL

2023-09-12 08:48:46 作者:匿名情书

我在我的工作就在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.

这产生了我pretty的良好的旋转和平移矩阵。我还计算与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函数。然后我采用的旋转矩阵和平移向量pretty的多,我能想到的任何可能的方式。可悲的是没有这些方法产生的结果是无论如何接近我所希望的。所以我的问题是谁能告诉我如何正确地使用它们呢?

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. So my question is 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.

我希望有人能帮忙,谢谢了!

I hope someone can help, thanks already!

推荐答案

好后一些更多的测试,我终于设法得到它的工作。虽然我不明白它...它的工作。对于任何人谁需要做到这一点,将来这里是我的解决方案。

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.

 
精彩推荐
图片推荐