short2,10 位整数,5 位小数,1 位符号位

问题描述 投票:0回答:1

NVIDIA 的光流库文档指出流向量“由 32 位值表示,每个水平和垂直分量为 16 位值。最低 5 位保存小数值,后面是 10 位整数值,最后是 10 位整数值”。最高有效位是符号位”。

所以,对于short2类型:

struct __device_builtin__ __align__(4) short2
{
    short x, y;
};

如何提取两个带有正确符号的浮点数?我在这里的尝试并没有那么令人信服。至少我的运动矢量看起来不正确。我不确定的一件事是这是否是两个人的恭维。文档里没说。

struct MotionVector {
    float x;
    float y;
};

float ExtractSingleMotionValue(short value) {
    bool isNegative = (value & 0x8000) != 0; // Check the sign bit

    // Clear the sign bit
    value = value & 0x7FFF;

    // Extract integer part (10 bits)
    int intPart = (value >> 5);

    // Extract fractional part (5 bits)
    float fracPart = value & 0x001F;
    fracPart /= 32;  // Convert fractional part to actual decimal

    float result = intPart + fracPart;

    if (isNegative) {
        result = -result;
    }

    return result;
}

MotionVector ExtractMotionVector(short2 value) {
    MotionVector mv;

    mv.x = ExtractSingleMotionValue(value.x);
    mv.y = ExtractSingleMotionValue(value.y);

    return mv;
}

有人可以提供一些建议吗?我真的不喜欢我的解决方案。我确信那里有一行。

c++ math nvidia fixed-point opticalflow
1个回答
0
投票

https://docs.nvidia.com/video-technologies/optical-flow-sdk/nvofa-programming-guide/index.html#generate-flow-vectors

流向量由 32 位值表示,每个水平和垂直分量为 16 位值。最低 5 位保存小数值,后面是 10 位整数值,最高有效位是符号位。

这是定点格式。

这样解释:

x_pixels = x_raw * (1.f / 32)

这显然不是浮点格式。他们不谈论任何尾数或指数。

© www.soinside.com 2019 - 2024. All rights reserved.