Leap Motion C API  4.1.0
The API to the LeapC library.
Rectifying Points Example

This example builds on Textured Quad Example. Instead of texturing the quad with the uncorrected mage data, this example rectifies the image data to a second texture buffer and uses the result. You will notice that the image no longer fills the entire rectangular area. This is because rays of light that would have filled in the border areas were actually outside the field of view of the camera.

The main changes to this example occur in the display() function. For each pixel in the target texture, the function generates the slopes of the camera ray intersecting that point and looks up the correct brightness value in the uncorrected image buffer (as described in Rectifying Image Points). The result is the a texture containing the rectified image.

1 //display the textured quad
2 static void display(void)
3 {
4  glMatrixMode(GL_MODELVIEW);
5  glPushMatrix();
6  glTranslatef(-32, -24, -50); //"Camera" viewpoint
7  glClear(GL_COLOR_BUFFER_BIT);
8  if(imageReady){
9  //Left image
10  for( float row = 0; row < TEX_HEIGHT; row++ ) {
11  for( float col = 0; col < TEX_WIDTH; col++ ) {
12 
13  //Normalize from pixel xy to range [0..1]
14  LEAP_VECTOR input;
15  input.x = col/TEX_WIDTH;
16  input.y = row/TEX_HEIGHT;
17 
18  //Convert from normalized [0..1] to ray slopes
19  input.x = (input.x - .5) * MAX_FOV;
20  input.y = (input.y - .5) * MAX_FOV;
21 
22  LEAP_VECTOR pixel = LeapRectilinearToPixel(*connection,
23  eLeapPerspectiveType_stereo_left,
24  input);
25  int dindex = (int)floor(row * TEX_WIDTH + col);
26  int pindex = (int)roundf(pixel.y) * image_width + (int)roundf(pixel.x);
27  if(pixel.x >= 0 && pixel.x < image_width && pixel.y >=0 && pixel.y < image_height){
28  undistorted_image_left[dindex] = ((char*)image_buffer)[pindex];
29  } else {
30  undistorted_image_left[dindex] = 128;
31  }
32  }
33  }
34  // right image
35  for( float row = 0; row < TEX_HEIGHT; row++ ) {
36  for( float col = 0; col < TEX_WIDTH; col++ ) {
37 
38  //Normalize from pixel xy to range [0..1]
39  LEAP_VECTOR input;
40  input.x = col/TEX_WIDTH;
41  input.y = row/TEX_HEIGHT;
42 
43  //Convert from normalized [0..1] to ray slopes
44  input.x = (input.x - .5) * MAX_FOV;
45  input.y = (input.y - .5) * MAX_FOV;
46 
47  LEAP_VECTOR pixel = LeapRectilinearToPixel(*connection,
48  eLeapPerspectiveType_stereo_right,
49  input);
50  int dindex = (int)floor(row * TEX_WIDTH + col);
51  int pindex = (int)roundf(pixel.y + image_height) * image_width + (int)roundf(pixel.x);
52 
53  if(pixel.x >= 0 && pixel.x < image_width && pixel.y >=0 && pixel.y < image_height){
54  undistorted_image_right[dindex] = ((char*)image_buffer)[pindex];
55  } else {
56  undistorted_image_right[dindex] = 200;
57  }
58  }
59  }
60 
61  DrawImageQuad(0, 0, 64, 24, TEX_WIDTH, TEX_HEIGHT, undistorted_image_left);
62  DrawImageQuad(0, 26, 64, 50, TEX_WIDTH, TEX_HEIGHT, undistorted_image_right);
63  imageReady = false;
64  }
65  glFlush();
66  glPopMatrix();
67  glutSwapBuffers();
68 }
69 //end of display

To rectify an image while taking advantage of a GPU, see Rectifying with a Shader Example.

This example is only supported on platforms for which a working version of GLUT exists. It should not be overly difficult to port the example to a different OpenGL-based context, however.