【发布时间】:2015-06-06 00:36:05
【问题描述】:
我正在尝试通过使用将 2D 离散余弦变换实现到图像
一维 DCT 操作。如果我将其与dct2 MATLAB 函数进行比较,我的输出不正确。我不明白我的代码出了什么问题以及发生在哪里。
如果有人能指出错误或任何其他建议,那将非常有帮助。
这是我用 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 -
感谢您指出这个错误。我已经纠正了。但结果仍然与我从“dct2”matlab 内置函数中得到的完全不同
-
我的主要算法实现是否正确,因为我的结果与正确结果相差甚远?
-
我真的不知道。无论如何,如果您发布一个可重现的示例(可以运行的代码,带有说明问题的实际数据),您更有可能得到答案
-
我已经包含了一些主要代码行来查找随机二维数字数组的 DCT。
标签: matlab image-processing signal-processing dct