[英]Why (A - B) .^ 2 is not equal to (B - A) .^ 2 in 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
此功能对图像I
执行均匀量化并将其转换为b
位图像,然后在0-255范围内进行缩放,现在我想计算此过程的MSE(均方误差)
但结果是
mse = sum(sum((I - Q) .^ 2, 1), 2) / numel(I);
和
mse = sum(sum((Q - I) .^ 2, 1), 2) / numel(I);
是不同的。 任何人都可以请指出我的问题是什么?
谢谢
问题是矩阵的类型。 您正在组合两个无符号矩阵。 因此,如果QI<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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.