Created
July 15, 2015 16:12
-
-
Save Craigson/36bb3735aa76e06ca46d to your computer and use it in GitHub Desktop.
Mapping kinect depth values to pixels
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //The Kinect gives you depth data for every pixel in the image (which is a 640 x 480 image). | |
| //These values are retreived using the depthMap() call. | |
| //This code is operating on the assumption that you're using a grid of 64 x 48 servo | |
| //motors for your "mirror", this way the aspect ratio is maintained. | |
| import SimpleOpenNI.*; | |
| SimpleOpenNI kinect; | |
| int[] depthValues; //this will hold integer values for the depth data for each pixel | |
| int[] indices; //we use this to store the converted values from the | |
| int[] servoArray; //this will hold the integer values for the depth data for each relative servo | |
| int numX = 64; //number of servos in the x-direction | |
| int numY = 48; //number of servos in the y-direction | |
| int space = 10; //this represents the reduction factor of the resolution, ie 640x480 and 64x48 is 10:1 | |
| void setup(){ | |
| size(640,480); | |
| kinect = new SimpleOpenNI(this); | |
| kinect.enableDepth(); | |
| servoArray = new float[3072]; //initialize the array to be the size of the total number of servos, ie 64*48, or 3072 | |
| } | |
| void draw(){ | |
| background(0,0); | |
| kinect.update(); | |
| depthValues = kinect.depthMap(); //this retreives the depth data from each pixel in the image | |
| //the array should hold 307200 values, equivalent to 640x480, check this by uncommenting the next line | |
| //println(depthValues.length); | |
| //now, here comes the important part: If your grid of servos is 64x48, it means that each servo requires | |
| //the depth data from every 10th pixel. So we custom-load the depth data of every 10th pixel into the servo | |
| //array | |
| int index = 0; //this allows us to fill up the array one by one by manually incrementing the index and loading in the relative pixel data | |
| for (int x = 0; x < numX; x++){ | |
| for (int y = 0; y < numY; y++){ | |
| indices[index] = x*space + (y * space * 640); //this array now holds the index positions of the desired pixels (ie every 10th pixel) | |
| index++; | |
| } | |
| } | |
| //now, in order to make use of the depth data for each servo, we access the values using the index array | |
| int i = 0; //we use this as an index to step through the indices array and access the depth data from the depthValues array | |
| for (float f : servoArray){ | |
| f = depthValues[indices[i]]; | |
| i++; | |
| } | |
| //at this point, the depth data for each servo should be stored in the servoArray. If you want to make use of the data, just map | |
| //it to a value that you can send to the servos via serial. for example, if the servo moves through angle 0-179, and the depth data | |
| //is a pixel value from 0 -255, you could do the following: | |
| for (float f : servoArray){ | |
| float newAngle = map(f,0,255,0,179); //this'll map the depth data (based on brightness) to the angle of the servo through 180' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment