在 Matlab 中使用逆 DCT 重建图像的问题

问题描述

我使用 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,3]);
recovered_img = uint8(permute(idct1d(permute( ...
                    idct1d(transformed_img),...
                        [2,3]));

figure('position',[0,600,200])
subplot(1,3,1),imshow(original_img),title 'Original'
subplot(1,2),imshow(log(abs(transformed_img)),[]),title 'DCT'
subplot(1,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,1);
  end
  
end

enter image description here

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...