为什么(A-B).^ 2不等于(B-A).^ 2在MATLAB中?

fro*_*tto 6 matlab

假设我有量化函数来量化8位灰度图像:

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
Run Code Online (Sandbox Code Playgroud)

此功能对图像执行均匀量化I并将其转换为b位图像,然后在0-255范围内缩放,现在我想计算此过程的 MSE(均方误差)

但结果是

mse = sum(sum((I - Q) .^ 2, 1), 2) / numel(I);
Run Code Online (Sandbox Code Playgroud)

mse = sum(sum((Q - I) .^ 2, 1), 2) / numel(I);
Run Code Online (Sandbox Code Playgroud)

是不同的.任何人都可以请指出我的问题是什么?
谢谢

phy*_*rox 9

问题是矩阵的类型.您正在组合两个无符号矩阵.那么如果Q-I<0那时结果是0并且它与IQ不同.

要使用uint8,您可以分两步计算MSE:

%Compute the absolute difference, according to the sign
difference = Q-I;
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);
Run Code Online (Sandbox Code Playgroud)