C++ 除法后循环

C++ 除法后循环,c++,opencv,rounding,mat,approximation,C++,Opencv,Rounding,Mat,Approximation,查看下面的代码 Blue = channel[0]; Green = channel[1]; Red = channel[2]; Mat G = (Green + Blue) / 2; 其中,红色、绿色和蓝色是图像的通道。当绿色和蓝色的总和是奇数时,有时是圆的,有时是固定的。例如,对于值为120和蓝色45的绿色像素,G值为82,因此它只取82,5的整数部分。而在另一种情况下,绿色是106,蓝色是33,我得到了G元素的值70,因此它是一个圆,因为33+106/2=69,5 哪个操作?如果要获取

查看下面的代码

Blue = channel[0];
Green = channel[1];
Red = channel[2];

Mat G = (Green + Blue) / 2;
其中,红色、绿色和蓝色是图像的通道。当绿色和蓝色的总和是奇数时,有时是圆的,有时是固定的。例如,对于值为120和蓝色45的绿色像素,G值为82,因此它只取82,5的整数部分。而在另一种情况下,绿色是106,蓝色是33,我得到了G元素的值70,因此它是一个圆,因为33+106/2=69,5


哪个操作?

如果要获取浮点数,则需要使用:

Mat G = (Green + Blue) / 2.0;
仅使用:

Mat G = (Green + Blue) / 2;

使用整数除法,由于整数中没有小数点,因此会被截断。

如果要获取浮点数,则需要使用:

Mat G = (Green + Blue) / 2.0;
仅使用:

Mat G = (Green + Blue) / 2;

使用整数除法,因为整数中没有小数点,所以它会被截断。

OpenCV使用舍入半到偶数的舍入模式。如果分数为0.5,则舍入为最接近的偶数整数。这就是为什么82.5四舍五入为82,69.5四舍五入为70。

OpenCV使用半舍五入到偶数四舍五入模式。如果分数为0.5,则舍入为最接近的偶数整数。这就是为什么82.5四舍五入为82,69.5四舍五入为70。

在opencv源代码中实现cvRound时发生了这种差异。其中一部分是从下面复制的,并添加了注释

int cvRound( float value )
{
    double intpart, fractpart;
    fractpart = modf(value, &intpart);

    //for +ve numbers, when fraction is 0.5, odd numbers are rounded up 
    //and even numbers are rounded down 
    //and vice versa for -ve numbers

    if ((fabs(fractpart) != 0.5) || ((((int)intpart) % 2) != 0))
        return (int)(value + (value >= 0 ? 0.5 : -0.5));
    else
        return (int)intpart;
}

我编写了一个小示例并进行了调试,以查看矩阵的加权加法调用了opencv代码中的saturate_cast,而inturn调用了cvRound。您可以在github上看到这一点。

在opencv源代码中实现cvRound时发生了这种差异。其中一部分是从下面复制的,并添加了注释

int cvRound( float value )
{
    double intpart, fractpart;
    fractpart = modf(value, &intpart);

    //for +ve numbers, when fraction is 0.5, odd numbers are rounded up 
    //and even numbers are rounded down 
    //and vice versa for -ve numbers

    if ((fabs(fractpart) != 0.5) || ((((int)intpart) % 2) != 0))
        return (int)(value + (value >= 0 ? 0.5 : -0.5));
    else
        return (int)intpart;
}

我编写了一个小示例并进行了调试,以查看矩阵的加权加法调用了opencv代码中的saturate_cast,而inturn调用了cvRound。您可以在github上看到这一点。

谢谢您的帮助。链接到一些有关这方面的文档会很好。谢谢。链接到一些有关这方面的文档会很好。