C++ 如何限制OpenCV中的浮点型Mat精度?

C++ 如何限制OpenCV中的浮点型Mat精度?,c++,opencv,matrix,C++,Opencv,Matrix,我有一个类型为CV_32FC1的矩阵,我想限制它的精度。例如,它的值如下所示: [0.00021743798, 0.000174778698, 0.00011652464, 5.826234e-005, 1.561136e-005] 但是我想限制它的精度,使它看起来像这样(就像在Matlab中): 我不是在寻找一个std::setprecision解决方案,而是完全限制内部的矩阵值精度,因为在我的例子中,这将影响后面与其他矩阵的乘法。更改矩阵类型(至少)没有帮助。我该怎么办?多谢各位 可能是因

我有一个类型为
CV_32FC1
的矩阵,我想限制它的精度。例如,它的值如下所示:

[0.00021743798, 0.000174778698, 0.00011652464, 5.826234e-005, 1.561136e-005]
但是我想限制它的精度,使它看起来像这样(就像在Matlab中):

我不是在寻找一个
std::setprecision
解决方案,而是完全限制内部的矩阵值精度,因为在我的例子中,这将影响后面与其他矩阵的乘法。更改矩阵类型(至少)没有帮助。我该怎么办?多谢各位

可能是因为这些应该有效(进行一些初始检查)

我害怕选择缓冲区的大小,因为float可以为您提供23位有效位、8位指数和1位符号位。有人可以帮助您选择正确的缓冲区大小

根据Adi的建议

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;
}
float-roundtoPrecision(浮点值,整数精度)
{
//做初步检查
浮点输出=roundf(val*pow(10,精度))/pow(10,精度);
返回输出;
}
int main()
{
//你的密码在这里
浮动a[]={0.00021743798、0.000174778698、0.00011652464、5.826234e-005、1.561136e-005};
对于(int-index=0;index<5;index++)
{
float val=roundtoPrecision(a[索引],4);

cout这个函数
limit\u precision()
似乎适合我:

#include <iostream>
#include <vector>
#include <cmath>

template<typename T>
T limit_precision(T val, int precision) {
    return std::floor((val * std::pow(10, precision) + 0.5)) / std::pow(10, precision); 
}

int main() {
    std::vector<float> v{0.00021743798, 0.000174778698, 0.00011652464, 5.826234e-005, 1.561136e-005};
    for (auto &el : v) { el = limit_precision(el, 4); }
    for (auto el : v) { std::cout << el << ", "; }
    std::cout << std::endl;
}
#包括
#包括
#包括
模板
T极限精度(T值,int精度){
返回标准::地板((val*std::功率(10,精度)+0.5))/std::功率(10,精度);
}
int main(){
标准::向量v{0.00021743798,0.000174778698,0.00011652464,5.826234e-005,1.561136e-005};
对于(auto&el:v){el=limit_precision(el,4);}

用于(自动el:v){std::难道你不想把你的双精度整数四舍五入到小数点吗?勾选这个:只是为了显示?乘以10000,trunc/round,除以10000?谢谢大家,但我已经尝试了你的所有建议。顺便说一句,这不仅仅是为了显示,正如问题中提到的,这就是为什么我不使用“std::setprecision”方法。甚至是通过f或者循环乘以“10000”并再次除法并不能给出精确的输出。例如,当我期望得到“0.00030000000”时,它给出了“0.00030612201”。乘法、取整和除法可能不适用于所有情况!虽然它不能给出我想要的,但它至少给出了最接近的输出,谢谢。你的和斯里拉姆的方法都给了我同样的结果,谢谢。
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;
}
#include <iostream>
#include <vector>
#include <cmath>

template<typename T>
T limit_precision(T val, int precision) {
    return std::floor((val * std::pow(10, precision) + 0.5)) / std::pow(10, precision); 
}

int main() {
    std::vector<float> v{0.00021743798, 0.000174778698, 0.00011652464, 5.826234e-005, 1.561136e-005};
    for (auto &el : v) { el = limit_precision(el, 4); }
    for (auto el : v) { std::cout << el << ", "; }
    std::cout << std::endl;
}