我有一个 CV_32FC1
类型的矩阵,我想限制其精度。例如,它的值是这样的:
[0.00021743798, 0.000174778698, 0.00011652464, 5.826234e-005, 1.561136e-005]
但我想限制它的精度,使其看起来像这样(就像在 Matlab 中一样):
[0.0002, 0.0002, 0.0001, 0.0001, 0.0000]
我不是在寻找 std::set precision
解决方案,而是完全限制内部的矩阵值精度,因为这会影响我稍后与其他矩阵的乘法。更改矩阵类型没有帮助(至少)。我应该怎么办?谢谢。
最佳答案
也许这些应该可以工作(做一些初步检查)
float customPrecision(float in)
{
char buffer[15];
int bufLen = sprintf(buffer, "%.4f", in);
return atof(buffer);
}
我担心选择缓冲区大小,因为浮点可以为您提供23位有效数、8位指数和1位符号位。有人可以帮忙选择正确的缓冲区大小。
根据阿迪的建议,
float roundtoPrecision(float val,int precision)
{
// Do initial checks
float output = roundf(val * pow(10,precision))/pow(10,precision);
return output;
}
int main()
{
// your code goes here
float a[] = {0.00021743798, 0.000174778698, 0.00011652464, 5.826234e-005, 1.561136e-005};
for (int index = 0; index < 5; index++)
{
float val = roundtoPrecision(a[index], 4);
cout << val << endl;
}
return 0;
}
关于c++ - 如何在OpenCV中限制float类型Mat精度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30612779/