简体   繁体   English

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

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

Suppose I have a quantization function which quantize a 8bit gray scale image : 假设我有量化函数来量化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

This function perform a uniform quantization on image I and convert it into a b bit image, then scale it in 0-255 range, Now I want to calculate MSE (Mean Square Error) of this process 此功能对图像I执行均匀量化并将其转换为b位图像,然后在0-255范围内进行缩放,现在我想计算此过程的MSE(均方误差)

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 anyone please point me out whats the problem? 任何人都可以请指出我的问题是什么?
Thanks 谢谢

The problem is the type of the matrixes. 问题是矩阵的类型。 You are combining two unsigned matrixes. 您正在组合两个无符号矩阵。 So if QI<0 then the result is 0 and it is different from IQ. 因此,如果QI<0那么结果为0并且它与IQ不同。

In order to use uint8 , you can compute MSE in two steps: 要使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM