查看以下代码
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)。
这是什么操作?
答案 0 :(得分:5)
OpenCV使用“Round half to even”舍入模式。如果fraction为0.5,则舍入到最接近的偶数整数。这就是为什么82.5四舍五入到82和69.5到70。
答案 1 :(得分:1)
在opencv源中实现cvRound
时发生了这种差异。其中一部分从下面的github复制并添加了评论。
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
(link),该代码调用了cvRound
。你可以在github(link)看到。
答案 2 :(得分:0)
如果您想获得浮点数,则需要使用:
Mat G = (Green + Blue) / 2.0;
只需使用:
Mat G = (Green + Blue) / 2;
使用整数除法,因为整数中没有小数点,它会被截断。