简体   繁体   English

OpenGL ES-从摄影机位置获取2D像素坐标

[英]OpenGL ES - get 2D pixel coordinate from camera position

I'm working with OpenGL ES (IOS) and OpenCV. 我正在使用OpenGL ES(IOS)和OpenCV。 I would like to know if there is a way to get 2D pixel coordinate from a GLKMatrix4 camera position ? 我想知道是否可以从GLKMatrix4相机位置获取2D像素坐标吗?

Indeed, I have a specific 3D object displayed with OpenGL which has only its size and its camera pose and I would like to convert it into the OpenCV reference Coordinate ie 2D pixel. 确实,我在OpenGL中显示了一个特定的3D对象,该对象只有它的大小和它的相机姿势,我想将其转换为OpenCV参考坐标,即2D像素。

More precisely, I have an IOS app and I stream the camera color. 更准确地说,我有一个IOS应用程序,并且可以传输相机颜色。 This stream is displayed with OpenGL on my Ipad. 此流与OpenGL一起显示在我的Ipad上。 On this display, I have a specific overlay which is a 3D object. 在此显示器上,我有一个特定的叠加层,即3D对象。 At the same time, I do some some detection with OpenCV. 同时,我对OpenCV进行了一些检测。 Now, I would like to link the result of my detection thank to OpenCV in my frame (It's a rectangle draw) with the 3D object but for this one, I have only its camera position and its size. 现在,我要通过3D对象将帧中的OpenCV(这是一个矩形绘制)与OpenCV关联起来,但是对于这个对象,我只有其摄像机位置和大小。

I know the content of a 4x4 camera pose but I did not find any information about how I could convert it (if it is possible) to a 2D coordinate. 我知道4x4照相机姿势的内容,但是没有找到有关如何将其转换为2D坐标的任何信息。

So briefly : Input : A 2D rectangle (get only 2D coordinate) and an 3D object (get only its camera position) 简而言之:输入:一个2D矩形(仅获取2D坐标)和一个3D对象(仅获取其相机位置)

Is there a way ? 有办法吗? Could you please help me ? 请你帮助我好吗 ?

Thank 谢谢

To get the onscreen projection of the vector you only need to multiply it by the matrix you are using in the openGL. 要获得向量在屏幕上的投影,只需要将其乘以openGL中使用的矩阵即可。 The 3rd coordinate is simply discarded and is used only for the depth buffer (also interpolations but it really doesn't matter here). 第三个坐标被简单地丢弃,仅用于深度缓冲区(还有插值,但这在这里实际上并不重要)。

In openGL the coordinate of the drawn pixel you will get by multiplying it with a matrix will be normalized to a coordinate system in range [-1,1] in all axises. 在openGL中,您将通过将其与矩阵相乘得到的绘制像素的坐标将被标准化为所有轴上[-1,1]范围内的坐标系。 That means a pixel that is on most top-left of the screen/view will have a coordinate (-1,1) , top-right (1,1) , center (0,0) ... Now corresponding to the buffer pixels this value must be denormalized: 这意味着位于屏幕/视图最左上角的像素将具有坐标(-1,1) ,右上角(1,1) ,中心(0,0) ...现在对应于缓冲区像素,此值必须反规范化:

You need to have the buffer width and height so then the pixel position is bufferX = ((x+1)/2.0) * width and bufferY = ((y+1)/2.0) * height . 您需要具有缓冲区的widthheight以便像素位置为bufferX = ((x+1)/2.0) * widthbufferY = ((y+1)/2.0) * height The results are floating values so you need to either round them to integers or typecast them. 结果是浮动值,因此您需要将它们四舍五入为整数或进行类型转换。 I have no idea which is done by the openGL though. 我不知道这是由openGL完成的。

Since you're already using GLKit types and math, try the function GLKMathProject to convert from 3D model space to viewport (window/pixel 2D) space, and GLKMathUnproject if you also need the other way around. 既然你已经使用GLKit类型和数学,试着功能GLKMathProject从3D模型空间转换到视口(窗口/像素2D)空间, GLKMathUnproject如果你还需要周围的其他方法。 You'll need the (same) modelview and projection matrices that you're (presumably) passing to a shader for drawing your scene. 您将需要(大概)传递给着色器的(相同)模型视图和投影矩阵,以绘制场景。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM