OpenGL中使用OpenCV像素坐标绘制正方形
我有一个由 OpenCV 为方形标记生成的 4x2 二维顶点数组。我想使用 OpenGL 坐标系中的这些坐标在标记的精确位置绘制 2D 四边形。如何做到这一点?
这是我的代码,如下所示,二维四边形绘制得比标记小。
// 'CvMat* dstPoints2D' is generated using cvProjectPoints2()
glPushMatrix();
glBegin(GL_QUADS);
glVertex2f((float)dstPoints2D->data.fl[2], (float)dstPoints2D->data.fl[3]); // right, bottom
glVertex2f((float)dstPoints2D->data.fl[0], (float)dstPoints2D->data.fl[1]); //Left, bottom
glVertex2f((float)dstPoints2D->data.fl[4], (float)dstPoints2D->data.fl[5]); // left, top
glVertex2f((float)dstPoints2D->data.fl[6], (float)dstPoints2D->data.fl[7]); // right, top
glEnd();
glPopMatrix();
I have a 4x2 array of 2D vertices generated by OpenCV for a Square marker. I want to use these coordinates in OpenGL coordinate system to draw a 2D Quad in the exact location of the marker. How to do this?
Here's my code, as you can see below that the 2D quad is drawn smaller than the marker.
// 'CvMat* dstPoints2D' is generated using cvProjectPoints2()
glPushMatrix();
glBegin(GL_QUADS);
glVertex2f((float)dstPoints2D->data.fl[2], (float)dstPoints2D->data.fl[3]); // right, bottom
glVertex2f((float)dstPoints2D->data.fl[0], (float)dstPoints2D->data.fl[1]); //Left, bottom
glVertex2f((float)dstPoints2D->data.fl[4], (float)dstPoints2D->data.fl[5]); // left, top
glVertex2f((float)dstPoints2D->data.fl[6], (float)dstPoints2D->data.fl[7]); // right, top
glEnd();
glPopMatrix();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来你们没有相同的坐标系(CS)。您的 OpenCV 顶点是二维的。您需要首先校准 OpenGL CS,然后应用尺寸因子。您可能只想使用 2 个点(方形边或对角线之一的极值点)来完成此操作。否则,您将需要一个更复杂的算法来使用您拥有的所有顶点信息进行拟合(以防您的正方形变成其他东西,例如菱形)。
最简单的方法是为第一个点+尺寸因子和方向添加平移因子到您的OpenGL方形CS中:)
我猜您的OpenGL场景处于正交(正交)模式,并且您的正方形仍然是正方形而不是菱形。
It seems that you're not having the same coordinate system (CS). Your OpenCV vertices are 2D. You'll need to first calibrate the OpenGL CS and second apply a size factor. You would want to do this using only 2 points (extremum points of one of your square sides or diagnoals). Otherwise you'il need a more complex algorithm to fit using all the vertices informations you have (in case your square become something else, e.g., lozenge).
The easy way is to add a translation factor to your OpenGL square CS for the first point + a size factor and the direction :)
I guess your OpenGL scene is in ortho (orthographic) mode and your square remain a square and not a lozenge.