Mario.Tapilouw

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);  
   glReadBuffer(GL_FRONT_RIGHT);  
   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;  
         
       if(col<640)  
       {  
         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];  
       }  
       else  
       {  
         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:




Sunday, November 23, 2014

XCoding..

So I start XCoding. The first impression is that the code editor is fast and very intelligent. If we don't make all the configuration right, it won't even compile. I found color scheme that looks pretty:


I tried to code some GLUT and OpenGL and found out that some functions have been deprecated. But I still can use them anyway, so there's no problem. Here's what I made at the first attempt, still have many things to improve but so far I like it.


I'm still using C++, not the Objective C. Have to squeeze some time learning Objective C and make a better UI.



Sunday, November 09, 2014

Coding in Mac!

Yeah! After a year I finally start coding in Mac. Just for my spare time because I am not working with Mac in the office. So far what I do is just trying simple things that I might need in my real program. This is a practice to make a platform independent code, maintaining portability across compilers.

So, what I have done so far? I have installed OpenCV 2.4.9 in my Mac, many bloggers have already posted the steps and thanks to that I have managed to install and run it on my Mac, Yeaaah!!

Another library that I have installed is fftw, this is a cool library which can be used almost everywhere, so I will make use of it.

The code is similar with those coding in Windows, but there's one thing which is different, the directory naming and environment settings. Let's see what I can do in the future.

I'll squeeze some time to post some exciting blog entries about this...