ImageΒΆ

Methods:

class com::leapmotion::leap::Image

The Image class represents a single image from one of the Leap Motion cameras.

In addition to image data, the Image object provides a distortion map for correcting lens distortion.

 Frame frame = controller.frame();
 if(frame.isValid()){
   ImageList images = frame.images();
   for(Image image : images)
   {
     //Processing PImage class
     PImage camera = cameras[image.id()];
     camera = createImage(image.width(), image.height(), RGB);
     camera.loadPixels();
     
     //Get byte array containing the image data from Image object
     byte[] imageData = image.data();

     //Copy image data into display object, in this case PImage defined in Processing
     for(int i = 0; i < image.width() * image.height(); i++){
       r = (imageData[i] & 0xFF) << 16; //convert to unsigned and shift into place
       g = (imageData[i] & 0xFF) << 8;
       b = imageData[i] & 0xFF;
       camera.pixels[i] =  r | g | b; 
     }
     
     //Show the image
     camera.updatePixels();
     image(camera, 640 * image.id(), 0);  
   }
} 

Note that Image objects can be invalid, which means that they do not contain valid image data. Get valid Image objects from Frame::frames(). Test for validity with the Image::isValid() function.

Since
2.1.0

Public Functions

int bytesPerPixel()

The number of bytes per pixel.

Use this value along with Image::width() and Image:::height() to calculate the size of the data buffer.

int bufferSize = image.bytesPerPixel() * image.width() * image.height();

Since
2.2.0

byte[] data()

The image data.

The image data is a set of 8-bit intensity values. The buffer is image.width() * image.height() * image.bytesPerPixel() bytes long.

byte[] image_buffer = image.data();

Since
2.1.0

float[] distortion()

The distortion calibration map for this image.

The calibration map is a 64x64 grid of points. Each point is defined by a pair of 32-bit floating point values. Each point in the map represents a ray projected into the camera. The value of a grid point defines the pixel in the image data containing the brightness value produced by the light entering along the corresponding ray. By interpolating between grid data points, you can find the brightness value for any projected ray. Grid values that fall outside the range [0..1] do not correspond to a value in the image data and those points should be ignored.

float[] distortion_buffer = image.distortion();

The calibration map can be used to render an undistorted image as well as to find the true angle from the camera to a feature in the raw image. The distortion map itself is designed to be used with GLSL shader programs. In other contexts, it may be more convenient to use the Image rectify() and warp() functions.

Distortion is caused by the lens geometry as well as imperfections in the lens and sensor window. The calibration map is created by the calibration process run for each device at the factory (and which can be rerun by the user).

Return
The float array containing the camera lens calibration map.
Since
2.1.0

int distortionHeight()

The distortion map height.

Currently fixed at 64.

int correctionGridHeight = image.distortionHeight();

Since
2.1.0

int distortionWidth()

The stride of the distortion map.

Since each point on the 64x64 element distortion map has two values in the buffer, the stride is 2 times the size of the grid. (Stride is currently fixed at 2 * 64 = 128).

int correctionGridWidth = image.distortionWidth();

Since
2.1.0

boolean equals(Image arg0)

Compare Image object equality.

Two Image objects are equal if and only if both Image objects represent the exact same Image and both Images are valid.

Since
2.1.0

Image.FormatType format()

The image format.

if(image.format() == Image.FormatType.INFRARED){
    String openGL_format = "GL_RED";
    String openGL_type = "GL_UNSIGNED_BYTE";
}

Since
2.2.0

int height()

The image height.

int height = image.height();

Since
2.1.0

int id()

The image ID.

Images with ID of 0 are from the left camera; those with an ID of 1 are from the right camera (with the device in its standard operating position with the green LED facing the operator).

Since
2.1.0

Image()

Constructs a Image object.

An uninitialized image is considered invalid. Get valid Image objects from a ImageList object obtained from the Frame::images() method.

Since
2.1.0

boolean isValid()

Reports whether this Image instance contains valid data.

Return
true, if and only if the image is valid.
Since
2.1.0

float rayOffsetX()

The horizontal ray offset.

Used to convert between normalized coordinates in the range [0..1] and the ray slope range [-4..4].

Vector raySlopes = new Vector(-3.28f, 1.76f, 0.0f);
Vector normRay =
      new Vector(raySlopes.getX() * image.rayScaleX() + image.rayOffsetX(),
                 raySlopes.getY() * image.rayScaleY() + image.rayOffsetY(), 0);

Since
2.1.0

float rayOffsetY()

The vertical ray offset.

Used to convert between normalized coordinates in the range [0..1] and the ray slope range [-4..4].

Vector normSlopes = new Vector(.09f, .72f, 0.0f);
Vector slope = new Vector((normSlopes.getX() - image.rayOffsetX())/image.rayScaleX(),
                          (normSlopes.getY() - image.rayOffsetY())/image.rayScaleY(), 0);

Since
2.1.0

float rayScaleX()

The horizontal ray scale factor.

Used to convert between normalized coordinates in the range [0..1] and the ray slope range [-4..4].

Vector raySlopes = new Vector(-3.28f, 1.76f, 0.0f);
Vector normRay =
      new Vector(raySlopes.getX() * image.rayScaleX() + image.rayOffsetX(),
                 raySlopes.getY() * image.rayScaleY() + image.rayOffsetY(), 0);

Since
2.1.0

float rayScaleY()

The vertical ray scale factor.

Used to convert between normalized coordinates in the range [0..1] and the ray slope range [-4..4].

Vector normSlopes = new Vector(.09f, .72f, 0.0f);
Vector slope = new Vector((normSlopes.getX() - image.rayOffsetX())/image.rayScaleX(),
                          (normSlopes.getY() - image.rayOffsetY())/image.rayScaleY(), 0);

Since
2.1.0

Vector rectify(Vector uv)

Provides the corrected camera ray intercepting the specified point on the image.

Given a point on the image, rectify() corrects for camera distortion and returns the true direction from the camera to the source of that image point within the Leap Motion field of view.

This direction vector has an x and y component [x, y, 0], with the third element always zero. Note that this vector uses the 2D camera coordinate system where the x-axis parallels the longer (typically horizontal) dimension and the y-axis parallels the shorter (vertical) dimension. The camera coordinate system does not correlate to the 3D Leap Motion coordinate system.

Vector feature = new Vector(127, 68, 0);
Vector slopes = image.rectify(feature);

Return
A Vector containing the ray direction (the z-component of the vector is always 0).
Since
2.1.0
Parameters
  • uv -

    A Vector containing the position of a pixel in the image.

long sequenceId()

The image sequence ID.

long imageSequence = image.sequenceId();

Since
2.2.1

long timestamp()

Returns a timestamp indicating when this frame began being captured on the device.

Since
2.2.7

String toString()

A string containing a brief, human readable description of the Image object.

Return
A description of the Image as a string.
Since
2.1.0

Vector warp(Vector xy)

Provides the point in the image corresponding to a ray projecting from the camera.

Given a ray projected from the camera in the specified direction, warp() corrects for camera distortion and returns the corresponding pixel coordinates in the image.

The ray direction is specified in relationship to the camera. The first vector element corresponds to the “horizontal” view angle; the second corresponds to the “vertical” view angle.

float horizontal_slope = (float)Math.tan(65 * Math.PI/180);
float vertical_slope = (float)Math.tan(15 * Math.PI/180);
Vector pixel = image.warp(new Vector(horizontal_slope, vertical_slope, 0));
if(pixel.getX() >= 0 && pixel.getY() >= 0 && pixel.getX() <= image.width() && pixel.getY() <= image.height()){
    int data_index = (int)pixel.getY() * image.width() + (int)pixel.getX();
    byte brightness = image.data()[data_index];
}

The warp() function returns pixel coordinates outside of the image bounds if you project a ray toward a point for which there is no recorded data.

warp() is typically not fast enough for realtime distortion correction. For better performance, use a shader program executed on a GPU.

Return
A Vector containing the pixel coordinates [x, y, 0] (with z always zero).
Since
2.1.0
Parameters
  • xy -

    A Vector containing the ray direction.

int width()

The image width.

int width = image.width();

Since
2.1.0

Public Static Functions

Image invalid()

Returns an invalid Image object.

You can use the instance returned by this function in comparisons testing whether a given Image instance is valid or invalid. (You can also use the Image::isValid() function.)

Return
The invalid Image instance.
Since
2.1.0

class FormatType

Enumerates the possible image formats.

The Image::format() function returns an item from the FormatType enumeration.

Since
2.2.0