由于项目需要,在windows平台需要实现openGL预览摄像头。于是想到将openCV和openGL结合起来,而openCV恰好支持openGL。
首先感谢https://www.cnblogs.com/feifanrensheng/p/9201629.html链接中作者的无私奉献,本文在TA基础上加工而成,希望能够帮助到有需要的朋友。需要安装openGL库glew,以及openCV库。
1 #include <iostream> 2 #include <GL/glew.h> 3 #include <opencv2/opencv.hpp> 4 #include <windows.h> 5 using namespace cv; 6 using namespace std; 7 8 9 cv::VideoCapture capture = cv::VideoCapture(0); 10 GLuint m_backgroundTextureId; 11 Mat m_backgroundImage; 12 13 14 void drawCameraFrame() 15 { 16 glGenTextures(1, &m_backgroundTextureId); 17 glBindTexture(GL_TEXTURE_2D, m_backgroundTextureId); 18 19 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 20 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 21 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 22 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 23 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 24 25 26 int w = m_backgroundImage.cols; 27 int h = m_backgroundImage.rows; 28 29 glPixelStorei(GL_PACK_ALIGNMENT, 1); 30 glBindTexture(GL_TEXTURE_2D, m_backgroundTextureId); 31 32 // Upload new texture data: 33 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 34 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, m_backgroundImage.data); 35 36 //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, m_backgroundImage.data); 37 38 //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, m_backgroundImage.data); 39 40 const GLfloat bgTextureVertices[] = { 0, 0, w, 0, 0, h, w, h }; 41 const GLfloat bgTextureCoords[] = { 1, 0, 1, 1, 0, 0, 0, 1 }; 42 const GLfloat proj[] = { 0, -2.f / w, 0, 0, -2.f / h, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1 }; 43 44 glMatrixMode(GL_PROJECTION); 45 glLoadMatrixf(proj); 46 47 glMatrixMode(GL_MODELVIEW); 48 glLoadIdentity(); 49 50 glEnable(GL_TEXTURE_2D); 51 glBindTexture(GL_TEXTURE_2D, m_backgroundTextureId); 52 53 // Update attribute values. 54 glEnableClientState(GL_VERTEX_ARRAY); 55 glEnableClientState(GL_TEXTURE_COORD_ARRAY); 56 57 glVertexPointer(2, GL_FLOAT, 0, bgTextureVertices); 58 glTexCoordPointer(2, GL_FLOAT, 0, bgTextureCoords); 59 60 glColor4f(1, 1, 1, 1); 61 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); 62 63 glDisableClientState(GL_VERTEX_ARRAY); 64 glDisableClientState(GL_TEXTURE_COORD_ARRAY); 65 glDisable(GL_TEXTURE_2D); 66 } 67 void draw(void* param) 68 { 69 glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); 70 drawCameraFrame(); 71 glFlush(); 72 } 73 74 const int g_nMaxAlphaValue = 100; 75 double g_dAlphaValue; 76 double g_dBetaValue; 77 int initTrackVal = 0;//滑块的初始位置 78 void onTracker(int nVal, void*pVal) 79 { 80 g_dAlphaValue = (double)nVal/g_nMaxAlphaValue; 81 g_dBetaValue = (1.0) - g_dAlphaValue; 82 updateWindow("windowName"); 83 84 } 85 86 int main( int argc, char** argv) 87 { 88 if (!capture.isOpened()) 89 { 90 printf("[%s][%d]could not load video data... ", __FUNCTION__, __LINE__); 91 return -1; 92 } 93 94 while (capture.read(m_backgroundImage)) 95 { 96 namedWindow("windowName", cv::WINDOW_OPENGL); 97 resizeWindow("windowName", m_backgroundImage.cols, m_backgroundImage.rows); 98 setOpenGlContext("windowName"); 99 setOpenGlDrawCallback("windowName", draw, NULL); 100 updateWindow("windowName"); 101 char szTrackName[100]; 102 sprintf(szTrackName, "参数 %d", g_nMaxAlphaValue); 103 //滑块 104 createTrackbar(szTrackName, "windowName", &initTrackVal, g_nMaxAlphaValue, onTracker); 105 onTracker(initTrackVal, 0); 106 char c = waitKey(10); 107 if (c == 27)//Esc键 108 { 109 break; 110 } 111 } 112 return 0; 113 }