Search code examples
matlabdct

Matlab multiply with a negativ number shows me 0


Im trying to read the rgb's of an image and multiply them with -1. The problem is that every Output of this script is 0. A(1,1) for example is 144 and 144 * -1 isnt 0. So what am i missing here?

A = imread('image.ppm');
[M,N] = size(A);
blocksize = 8;

for i=1:1:blocksize
   for j=1:1:blocksize
         disp(A(i,j) * - 1);
   end
end

Solution

  • A is of type uint8. MATLAB uses saturated arithmetic with integers. In saturated arithmetic, 144 * -1 = -144 = 0.

    The solution is to use im2double:

    A=im2double(A)
    

    Note that MATLAB keeps images of class double in the range [0,1], and of class uint8 in the range [0,255]. You will notice this difference when using imshow and imsave. im2double will convert the ranges properly.