2014-03-06 77 views
6

假設我有一個量化的8位灰度級圖像的量化函數:爲什麼(A - B)。^ 2在MATLAB中不等於(B - A)。^ 2?

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); 

是不同的。任何人都可以請指出我最新的問題?
謝謝

+2

我與示例代碼測試,它似乎是確定Q =蘭特(4,3) I =蘭特(4- (和)(sum((I-Q)。^ 2,1),其中mse = sum(sum((Q-I)。^ 2,1),2)/ numel(I) mse = sum ,2)/ numel(I) – michaeltang

+1

你能否提供一個可重複的例子,可能是一個3乘3的圖像? – Dan

+1

兩種結果有什麼不同? – kkuilla

回答

9

問題是矩陣的類型。您正在合併兩個未簽名的矩陣。所以如果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); 
+0

哇,你是對的,非常感謝的人! –

+0

但'int8'的範圍是-128到+127,我的圖像的像素值可能爲255 –

+0

@ABFORCE我添加了新代碼來使用uint8計算MSE。希望它有助於 – phyrox

相關問題