【问题标题】:Problem reconstructing the image using inverse DCT in Matlab在 Matlab 中使用逆 DCT 重建图像的问题
【发布时间】:2021-05-29 00:58:34
【问题描述】:

我使用 1D DCT 和 IDCT 实现了我自己的 2D DCT。我的 DCT 结果与 Matlab 的实现相匹配,但 IDCT 给出了不同的结果。从重建图像可以看出,差异并没有完全消失。

【问题讨论】:

    标签: matlab image-processing dct


    【解决方案1】:

    我对此有所了解 - 你的 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
    

    【讨论】:

    • 更具体一点 - 你的代码中引起我注意的问题是 DCT/IDCT 方程的索引。 MATLAB 是 1 索引的,但 DCT/IDCT 通常写为 0 索引。您的代码需要考虑到这一点。
    猜你喜欢
    • 1970-01-01
    • 2014-11-15
    • 1970-01-01
    • 1970-01-01
    • 2013-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-30
    相关资源
    最近更新 更多