F# 中的位移和字节到 int 转换
我正在使用 kinect 在 F# 中执行一些操作,但在处理深度数据时遇到一些问题。我一直在关注本教程: http://digitalrr0r.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);
}
}
}
我遇到的问题似乎与这部分有关: int distance = (p.Bits[n + 0] | p.Bits[n + 1] << 8) ;
在 F# 中,他应该变成 let distance = (p.Bits.[n+0] ||| p.Bits.[n+1] <<< 8)
这意味着,通过类型推断距离的类型为“byte”,然后将其转换为 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#!
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下面的代码片段显示了从
array
的两个相邻bytes
到int
的转换是正确的:The snippet below shows that conversion from two adjacent
bytes
ofarray
intoint
works right: