假设我有一个量化 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 (Mean Square Error)这个过程
但是结果为
mse = sum(sum((I - Q) .^ 2, 1), 2) / numel(I);
和
mse = sum(sum((Q - I) .^ 2, 1), 2) / numel(I);
是不同的。谁能指出我的问题是什么?
谢谢
最佳答案
问题是矩阵的类型。您正在组合两个无符号 矩阵。所以如果Q-I<0
则结果为 0,与 I-Q 不同。
为了使用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);
关于matlab - 为什么 (A - B) .^ 2 在 MATLAB 中不等于 (B - A) .^ 2?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22219523/