24-Add 2 Images Quiz Sol
The key to solving this problem is to know that both these images are of type uint8. This means that all pixel values are integers in the range 0 to 255. So here's what happens. Since the images are unsigned integers, Octave tries to retain the same data type throughout the arithmetic operation. So 183 by 2 comes out to be 92. Note that Octave rounds to the nearest integer. In this case, it is rounding up. Similarly, 152 by 2 is 76, and their sum comes out to be 168. In the second case, the addition is performed first, so 183 plus 152 is 335. But note that this number cannot fit in the unsigned int 8-bit range. The maximum value possible is 255. So this number gets truncated to the upper limit. The division proceeds as expected, and the result is 128. You can imagine that in a number of places, the pixel values add up to more than 255. In all these locations, you will only get 128 as the result. This is often less than the actual average of the two numbers. Hence we see that the first method better preserves pixel values. You will certainly come across these odd arithmetic errors. Just be mindful of the image data type you are using, and the order in which you perform arithmetic operations.