MATLAB: How to calculate pixel value differences

MATLAB and Simulink Student Suiteneg pixel value

let my image be
temp=
[5 10;
10 15]
if i want to calculate temp(1,1)-temp(1,2) or temp(1,1)-temp(2,1) it always shows 0. how can i store the value?? i have also tried abs(temp(1,1)-temp(1,2)); bt failed. thanx in advance

Best Answer

In your example, it actually works the way you want. The most likely reason why it does not work with your real image is that your image is of type uint8 (or uint16). The range of uint8 is limited to [0, 255], any value below or above these values are clamped to 0 or 255.
The fix is simple, convert your image to double:
temp = uint8([5 10; 10 15]) %demo image
temp(1,1) - temp(1, 2) %output 0, since -5 is not allowed for uint8
temp = double(temp); %convert to double
temp(1,1) - temp(1, 2) %output -5 as it should