F#中的位移和字节到int转换

Ewe*_*ley 0 xna f# bitwise-operators kinect

我正在使用kinect在F#中做一些事情,但是在使用深度数据时遇到了一些麻烦.我一直在关注本教程:http://digitalerr0r.wordpress.com/2011/06/21/kinect-fundamentals-3-getting-data-from-the-depth-sensor/

有c#例子,我一直试图转换为F#.

这部分代码存在问题:

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);

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我遇到的问题似乎与这部分有关: int distance = (p.Bits[n + 0] | p.Bits[n + 1] << 8);

在F#他应该成为 let distance = (p.Bits.[n+0] ||| p.Bits.[n+1] <<< 8)

这意味着,通过类型感染,距离是"字节"类型,然后我将其转换为如下所示的int : let distance = int(p.Bits.[n+0] ||| p.Bits.[n+1] <<< 8). 这是将一个位转换为int的正确方法吗?我的按位操作是否正确?由于我自己从头开始学习F#,我不确定,但这并没有抛出任何语法错误.

然而,它确实意味着我所有的深度测量结果都是0.如果我把它作为字节它们稍微更明智但它们不适用于下一行(这个是在c#中但我确实有它的F#版本. ..做同样的事!)byte intensity = (byte)(255 – (255 * Math.Max(distance – minDist, 0) / (distOffset)));

基本上我不能让它做任何事情,除了给我每个输出强度255.

对于我想要做的模糊事情,任何帮助都会非常感激和道歉!应该只使用C#!

谢谢

Gen*_*ski 5

下面的代码段显示从两个相邻bytesarray转换为int正确的工作:

let bytes = [|1uy;1uy|]
let distance = int bytes.[0] ||| (int bytes.[1] <<< 8)
printfn "%08X" distance
00000101
val bytes : byte [] = [|1uy; 1uy|]
val distance : int = 257
Run Code Online (Sandbox Code Playgroud)