我想要的是向相机显示一个标记,并在找到后用图像覆盖它,例如:(让我们调用image)
我要做的是:
1)我用opencv打开我的网络摄像头。我加载图像。我检测到捕获帧中的标记。计算标记相对于frame的坐标。
#define WIDTH 600 //opengl window width
#define HEIGHT 440 //opengl window height
VideoCapture cap;
GLint window;
GLuint *textures = new GLuint[2];
vector <Point2f> coord(4);
int flag =0;
// Capture next frame
Mat frame;
cap >> frame;
flip(frame,frame,1);
aruco::CameraParameters CamParam;
aruco::MarkerDetector MDetector;
std::vector <Marker> Markers;
float MarkerSize=170;
//marker detection
CamParam.readFromXMLFile("parametri_camera.yml");
CamParam.resize( frame.size());
MDetector.detect(frame, Markers, CamParam, MarkerSize);
Mat image = imread("Immagine.png");
if (Markers.size()!=0) flag =1;
else flag =0;
for (int i=0; i< Markers.size(); i++) {
coord_marker(frame, Markers[i], CamParam, coord);
}
2)我将图像和帧转换/转换为两个纹理。
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
glBindTexture(GL_TEXTURE_2D, textures[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, 4,frame.cols, frame.rows, 0, GL_RGB,GL_UNSIGNED_BYTE, frame.data);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glBindTexture(GL_TEXTURE_2D, textures[1]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, 4,image.cols, image.rows, 0, GL_RGB,GL_UNSIGNED_BYTE, image.data);
3)我展示了两个纹理,它们textures[1]
的位置与标记的坐标相对应。
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
// Set Projection Matrix
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, WIDTH, HEIGHT, 0);
// Switch to Model View Matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBindTexture(GL_TEXTURE_2D, textures[0]);
// Draw a textured quad
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex2f(WIDTH, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex2f(WIDTH, HEIGHT);
glTexCoord2f(0.0f, 1.0f); glVertex2f(0.0f, HEIGHT);
glEnd();
if(flag ==1) {
glBindTexture(GL_TEXTURE_2D, textures[1]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f((GLfloat)((coord[1].x)),(GLfloat)(coord[1].y));
glTexCoord2f(1.0f, 0.0f); glVertex2f((GLfloat)((coord[2].x)),(GLfloat)(coord[2].y));
glTexCoord2f(1.0f, 1.0f); glVertex2f((GLfloat)((coord[3].x)),(GLfloat)(coord[3].y));
glTexCoord2f(0.0f, 1.0f); glVertex2f((GLfloat)((coord[0].x)),(GLfloat)(coord[0].y));
glEnd();
}
glFlush();
glutSwapBuffers();
这是结果:
如您所见,textures[1]
相对于标记移动。我错过了什么?看起来我需要做一些从 Mat 坐标到纹理坐标的转换/转换。但是怎么做?