在 Matlab 中使用逆 DCT 重建图像的问题
Problem reconstructing the image using inverse DCT in Matlab
我使用 1D DCT 和 IDCT 实现了自己的 2D DCT。我的 DCT 结果与 Matlab 的实现相匹配,但 IDCT 给出了不同的结果。从重建图像可以看出,差异并不完全。
我对此进行了破解 - 你的 DCT/IDCT 方程式对我来说不太正确。我使用了 SciPy 文档 here.
中的 DCT-2 和 DCT-3 公式
original_img = imread('nggyu.jpeg');
transformed_img = permute(dct1d(permute( ...
dct1d(double(original_img)), ...
[2,1,3])), [2,1,3]);
recovered_img = uint8(permute(idct1d(permute( ...
idct1d(transformed_img), ...
[2,1,3])), [2,1,3]));
figure('position', [0, 0, 600, 200])
subplot(1,3,1), imshow(original_img), title 'Original'
subplot(1,3,2), imshow(log(abs(transformed_img)),[]), title 'DCT'
subplot(1,3,3), imshow(recovered_img), title 'IDCT'
function y = dct1d(x)
% Compute normalized DCT-2 over the first dimension of the input.
N = size(x, 1);
y = zeros(size(x));
n = (1:N)';
for k = 1:N
if k == 1
scale = sqrt(1/(4*N));
else
scale = sqrt(1/(2*N));
end
y(k,:,:) = scale * 2 * sum(x(n,:,:) .* cos((pi/(2*N)) * (2*n-1) * (k-1)), 1);
end
end
function x = idct1d(y)
% Compute normalized DCT-3 over the first dimension of the input.
N = size(y, 1);
x = zeros(size(y));
k = (2:N)';
for n = 1:N
x(n,:,:) = y(1,:,:)/sqrt(N) + sqrt(2/N) * sum(y(k,:,:) .* cos((pi/(2*N)) * (2*n-1) * (k-1)), 1);
end
end
我使用 1D DCT 和 IDCT 实现了自己的 2D DCT。我的 DCT 结果与 Matlab 的实现相匹配,但 IDCT 给出了不同的结果。从重建图像可以看出,差异并不完全。
我对此进行了破解 - 你的 DCT/IDCT 方程式对我来说不太正确。我使用了 SciPy 文档 here.
中的 DCT-2 和 DCT-3 公式original_img = imread('nggyu.jpeg');
transformed_img = permute(dct1d(permute( ...
dct1d(double(original_img)), ...
[2,1,3])), [2,1,3]);
recovered_img = uint8(permute(idct1d(permute( ...
idct1d(transformed_img), ...
[2,1,3])), [2,1,3]));
figure('position', [0, 0, 600, 200])
subplot(1,3,1), imshow(original_img), title 'Original'
subplot(1,3,2), imshow(log(abs(transformed_img)),[]), title 'DCT'
subplot(1,3,3), imshow(recovered_img), title 'IDCT'
function y = dct1d(x)
% Compute normalized DCT-2 over the first dimension of the input.
N = size(x, 1);
y = zeros(size(x));
n = (1:N)';
for k = 1:N
if k == 1
scale = sqrt(1/(4*N));
else
scale = sqrt(1/(2*N));
end
y(k,:,:) = scale * 2 * sum(x(n,:,:) .* cos((pi/(2*N)) * (2*n-1) * (k-1)), 1);
end
end
function x = idct1d(y)
% Compute normalized DCT-3 over the first dimension of the input.
N = size(y, 1);
x = zeros(size(y));
k = (2:N)';
for n = 1:N
x(n,:,:) = y(1,:,:)/sqrt(N) + sqrt(2/N) * sum(y(k,:,:) .* cos((pi/(2*N)) * (2*n-1) * (k-1)), 1);
end
end