NVIDIA's Optical Flow library documentation states the flow vectors are "represented by a 32-bit value with each horizontal and vertical compoment being 16-bit value. The lowest 5 bits holding fractional value, followed by a 10-bit integer value and the most significant bit being a sign bit".
So, with the short2 type:
struct __device_builtin__ __align__(4) short2
{
short x, y;
};
How would I extract two floats, with the correct sign? My attempt here I don't find all that convincing. At least my motion vectors don't look right. One thing I'm not sure about is whether it's two's compliment or not. The document doesn't say.
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;
}
Can anyone offer some advice? I really don't like my solution. I'm sure there's a single line one out there.
Flow vector is preresented by a 32-bit value with each horizontal and vertical compoment being 16-bit value. The lowest 5 bits holding fractional value, followed by a 10-bit integer value and the most significant bit being a sign bit.
That is a fixed point format. All the bits are mantissa*. The decimal point is fixed at 5 fractional bits. 25 = 32. That's the required "shift"/divisor.
Interpret it like so:
short x_raw;
float x_pixels = x_raw * (1.f / 32);
*) let's not argue about the sign... OK let's
Granted, they failed to specify how negative values are represented. From my experience, two's complement is the obvious choice, resulting in concise code and no acrobatics required in the arithmetic. Any other format is a war crime.
This is not a floating point format. There is no mention of an exponent or mantissa.