Objective :
To render OpenCV image captured from webcam into openGL window (GLUT)
Code :
#include <Windows.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include <GL/freeglut.h>
#include "opencv2\core\core.hpp"
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\video\video.hpp"
using namespace cv;
using namespace std;
////////////////////////////////////////////DECLARATION///////////////////////////////////////////////////////
VideoCapture cap(0);
Mat frame;
void drawImage()
{
GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glEnable(GL_TEXTURE_2D);
//render image
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 640, 480, 0, GL_RGB, GL_UNSIGNED_BYTE, frame.data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glBegin(GL_POLYGON);
glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex2f(1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex2f(1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0f, 1.0f);
glEnd();
glFlush();
}
void display()
{
cap >> frame; //capture frame
flip(frame, frame, 0); //mirror around x axis
cvtColor(frame, frame, CV_BGR2RGB);
drawImage();
glFlush();
glutSwapBuffers();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv); //GLUT initialization
glutInitWindowSize(640, 480); //define window size
glutInitWindowPosition(0, 0); //starting posiiton
glutCreateWindow("frame"); //name of the window
glutDisplayFunc(display); //set display function
glutIdleFunc(display); //idle function
glutMainLoop();
}
To render OpenCV image captured from webcam into openGL window (GLUT)
Code :
#include <Windows.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include <GL/freeglut.h>
#include "opencv2\core\core.hpp"
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\video\video.hpp"
using namespace cv;
using namespace std;
////////////////////////////////////////////DECLARATION///////////////////////////////////////////////////////
VideoCapture cap(0);
Mat frame;
void drawImage()
{
GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glEnable(GL_TEXTURE_2D);
//render image
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 640, 480, 0, GL_RGB, GL_UNSIGNED_BYTE, frame.data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glBegin(GL_POLYGON);
glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex2f(1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex2f(1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0f, 1.0f);
glEnd();
glFlush();
}
void display()
{
cap >> frame; //capture frame
flip(frame, frame, 0); //mirror around x axis
cvtColor(frame, frame, CV_BGR2RGB);
drawImage();
glFlush();
glutSwapBuffers();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv); //GLUT initialization
glutInitWindowSize(640, 480); //define window size
glutInitWindowPosition(0, 0); //starting posiiton
glutCreateWindow("frame"); //name of the window
glutDisplayFunc(display); //set display function
glutIdleFunc(display); //idle function
glutMainLoop();
}