Why (A - B). ^ 2 is not equal to (B - A). ^ 2 in MATLAB?

Suppose I have a quantization function that quantizes an image with a gray scale of 8 bits:

function mse = uni_quan(I, b) Q = I / 2 ^ (8 - b); Q = uint8(Q); Q = Q * 2 ^ (8 - b); mse = sum(sum((I - Q) .^ 2, 1), 2) / numel(I); end 

This function performs uniform quantization on image I and converts it to bitmap b , then scales it in the range 0-255, now I want to calculate the MSE (mean square error) of this process

But the result for

 mse = sum(sum((I - Q) .^ 2, 1), 2) / numel(I); 

and

 mse = sum(sum((Q - I) .^ 2, 1), 2) / numel(I); 

is different. Can someone tell me what the problem is?
Thanks

+6
source share
1 answer

The problem is the type of matrices. You combine two unsigned matrices. Therefore, if QI<0 , then the result is 0 and differs from IQ.

To use uint8 , you can calculate MSE in two steps:

 %Compute the absolute difference, according to the sign difference = QI; neg_idx = find(I>Q); difference(neg_idx) = I(neg_idx)-Q(neg_idx); %Compute the MSE mse = sum(sum((difference) .^ 2, 1), 2) / numel(I); 
+9
source

All Articles