1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
#include <GL/freeglut.h>
#include<stdio.h>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
void myDisplay(void) { glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT); glPointSize(1); glBegin(GL_QUADS); glColor3f(1.0f, 1.0f, 1.0f); glVertex2f(-0.5f,-0.5f); glVertex2f(-0.5f,0.5f); glVertex2f(0.5f,0.5f); glVertex2f(0.5f,-0.5f); glEnd();
glBegin(GL_TRIANGLES); glColor3f(1.0f,0.0f,0.0f); glVertex2f(0.0f,1.0f);
glColor3f(0.0f,1.0f,0.0f); glVertex2f(0.8f,-0.5f);
glColor3f(0.0f, 0.0f, 1.0f); glVertex2f(-0.8f,-0.5f); glEnd();
glPointSize(3); glBegin(GL_POINTS); glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(-0.4f, -0.4f); glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(0.0f, 0.0f); glColor3f(0.0f, 0.0f, 1.0f); glVertex2f(0.4f, 0.4f); glEnd();
glFlush(); }
int main(int argc, char * argv[]) {
glutInit( & argc, argv); glutInitWindowPosition(100, 100); glutInitWindowSize(400, 400); glutCreateWindow("Hello Point!"); glutDisplayFunc( & myDisplay); glutMainLoopEvent();
GLubyte * pPixelData = (GLubyte * ) malloc(400 * 400 * 3); GLint viewport[4] = { 0 }; glReadBuffer(GL_FRONT); glPixelStorei(GL_UNPACK_ALIGNMENT, 4); glGetIntegerv(GL_VIEWPORT, viewport); glReadPixels(viewport[0], viewport[1], viewport[2], viewport[3], GL_RGB, GL_UNSIGNED_BYTE, pPixelData);
cv::Mat img; std::vector < cv::Mat > imgPlanes; img.create(400, 400, CV_8UC3); cv::split(img, imgPlanes);
for (int i = 0; i < 400; i++) { unsigned char * plane0Ptr = imgPlanes[0].ptr < unsigned char > (i); unsigned char * plane1Ptr = imgPlanes[1].ptr < unsigned char > (i); unsigned char * plane2Ptr = imgPlanes[2].ptr < unsigned char > (i); for (int j = 0; j < 400; j++) { int k = 3 * (i * 400 + j); plane2Ptr[j] = pPixelData[k]; plane1Ptr[j] = pPixelData[k + 1]; plane0Ptr[j] = pPixelData[k + 2]; } } cv::merge(imgPlanes, img); cv::flip(img, img, 0); cv::namedWindow("openglGrab"); cv::imshow("openglGrab", img); cv::imwrite("../img_step2/test.jpg", img); return 0; }
|