我正在嘗試使用1D DCT運算實現2D離散餘弦變換到圖像。如果我將它與MATLAB函數dct2
進行比較,我的輸出是不正確的。我不明白我的代碼出了什麼問題以及它發生了什麼。使用1D DCT進行2D離散餘弦變換
如果有人能指出錯誤或任何其他建議,那將是非常有用的。
這裏是寫在MATLAB
% main function
signal=rand(100);
signal_dct=myDCT(signal);
figure; imshow((signal_dct));
% function to calculate 2D DCT of an image
function res=myDCT(signal)
signal=double(signal);
l=size(signal,1);
res=zeros(l); %initialize the final result matrix
for k=1:l %calculate 1D DCT of each row of image
res(k,:)=mdct(signal(k,:));
end
for k=1:l %calculate 1D DCT of each column of image
res(:,k)=mdct(res(:,k));
end
end
%% function to calculate 1D DFT of a 1D signal
function res=mdct(signal)
l=size(signal,1);
for i=1:l
if i==1 %for signal index of 1, alpha is 1/sqrt(l)
alpha=sqrt(1/l);
else %for signal index of greater than 1
alpha=sqrt(2/l);
end
j=[1:l];
% summation calculates single entry of res by applying the
% formula of DCT on the signal
summation=sum(sum(signal(j)*cos((pi*(2*(j-1)+1)*(i-1))/(2*l))));
res(i)=alpha*summation;
end
end
看起來你正在使用'cosd'與角度弧度。感謝您指出錯誤,請使用'cos'代替 – 2015-04-01 11:38:51
。我已糾正它。但仍然結果是什麼,就像我從「dct2」matlab內置函數 – 2015-04-01 11:41:23
得到的是我的主要算法實現是否正確,因爲我的結果離正確的結果很遠? – 2015-04-01 11:44:03