Monday, November 24, 2014

Mixing OpenGL and OpenCV

Here's the idea, I want to combine 3D image drawn using OpenGL and adding some information into it. So what I have to do is to draw what I need into OpenGL, capture it, then draw it into bitmap, and then add some information on the bitmap.

There are so many information about drawing 3D surfaces using OpenGL on the web so I don't want to discuss it here. After the 3D surface is drawn, the window can be captured using glReadBuffer() and glReadPixels() functions, if you have no idea about how to use those functions, here's a snippets:

 // capture window  
   unsigned char * image;  
   image = new unsigned char[640*640*3];  
   glPixelStorei(GL_PACK_ALIGNMENT, 1);  
   glReadPixels(0, 0, 640, 640, GL_RGB, GL_UNSIGNED_BYTE, image);  
   IplImage *imagecv = cvCreateImage(cvSize(800, 640), IPL_DEPTH_8U, 3);  
   imagecv->origin = 0;  
   for(int row=0;row<640;row++)  
     for(int col=0;col<800;col++)  
       CvScalar pixVal;  
         pixVal.val[2] = image[3*row*640+3*col+0];  
         pixVal.val[1] = image[3*row*640+3*col+1];  
         pixVal.val[0] = image[3*row*640+3*col+2];  
         pixVal.val[2] = 255;  
         pixVal.val[1] = 255;  
         pixVal.val[0] = 255;  
       cvSet2D(imagecv, row, col, pixVal);  

Basically what we need is an unsigned char array for storing the pixel values of the window (window size is 640x640). Then the rest are manipulation of the image.

Here's an example of 3D window captured from OpenGL, the data is a plane circular surface tilted at certain angle:

And here's an example of the image after the adding some information onto it:


Post a Comment

<< Home