I am using the kinect to do a few things in F# but am having a little trouble working with depth data. I have been following this tutorial: http://digitalerr0r.wordpress.com/2011/06/21/kinect-fundamentals-3-getting-data-from-the-depth-sensor/
which has c# examples which I have been trying to convert to F#.
This part of code is being problematic:
void kinectSensor_DepthFrameReady(object sender, ImageFrameReadyEventArgs e)
{
PlanarImage p = e.ImageFrame.Image;
Color[] DepthColor = new Color[p.Height * p.Width];
float maxDist = 4000;
float minDist = 850;
float distOffset = maxDist – minDist;
kinectDepthVideo = new Texture2D(GraphicsDevice, p.Width, p.Height);
int index = 0;
for (int y = 0; y < p.Height; y++)
{
for (int x = 0; x < p.Width; x++, index += 2)
{
int n = (y * p.Width + x) * 2;
int distance = (p.Bits[n + 0] | p.Bits[n + 1] << 8);
byte intensity = (byte)(255 – (255 * Math.Max(distance – minDist, 0) / (distOffset)));
DepthColor[y * p.Width + x] = new Color(intensity, intensity, intensity);
}
}
}
The problem I am having appears to be with this part: int distance = (p.Bits[n + 0] | p.Bits[n + 1] << 8);
in F# his should become let distance = (p.Bits.[n+0] ||| p.Bits.[n+1] <<< 8)
this means, through type inferance that distance is of type “byte” which I then cast to an int like this: let distance = int(p.Bits.[n+0] ||| p.Bits.[n+1] <<< 8). is this the correct way to convert a bit to an int? Are my bitwise operations correct? As I have been learning F# from scratch myself I am unsure but this doesnt throw any syntax errors.
However, it does mean that all my depth measurements come out as 0. If I have it as byte they are slightly more sensible but they dont work with the next line (this one is in c# but I do have an F# version of it… does the same thing!)byte intensity = (byte)(255 – (255 * Math.Max(distance – minDist, 0) / (distOffset)));
Essentially I cannot get it to do anything other than give me 255 for each output intensity.
Any help would be really appreciated and apologies for the obscure things I am trying to do! Should probably just use C#!
Thanks
The snippet below shows that conversion from two adjacent
bytesofarrayintointworks right: