Search code examples
c++opencvroundingmatapproximation

OpenCv round after division


Looking at the following code

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

Mat G = (Green + Blue) / 2;

where Red Green and Blue are the channels of an image. Where the sum of Green and Blue is odd, sometimes it make a round and sometimes a "fix". For example for a Green pixel with value 120 and Blue 45, the G value is 82 (so that it takes just the integer part of 82,5). While in another case where the Green is 106 and the Blue is 33 i get the value 70 for that element of G (so that, it makes a round because (33+106)/2 = 69,5 ).

Which is the operation?


Solution

  • OpenCV uses "Round half to even" rounding mode. If fraction is 0.5, it rounds to the nearest even integer. That's why 82.5 is rounded to 82 and 69.5 to 70.