在Scipy中,为什么idct(dct(a))不等于a? [英] In scipy why doesn't idct(dct(a)) equal to a?

查看:318
本文介绍了在Scipy中,为什么idct(dct(a))不等于a?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用python实现JPEG压缩。当我尝试对tiff图像应用DCT,量化,IDCT处理时,我发现scipy.fftpack.dct / idct很奇怪。

I am trying to implement JPEG compression using python. When I tried to apply the DCT, quantization, IDCT process for a tiff image, I found something strange for scipy.fftpack.dct/idct.

由于只有一维scty包中的dct / idct,我是用二维dct

Since there is only 1D dct/idct within scipy package, I was doing this for a 2D dct

import numpy as np
from scipy.fftpack import dct, idct

def dct2(block):
    return dct(dct(block.T).T)

def idct2(block):
    return idct(idct(block.T).T)

我测试了2D dct / idct使用简单的3x3矩阵。我期望在这个测试用例中得到一个True矩阵。

I tested the 2D dct/idct using a simple 3x3 matrix. I was expecting to get a True matrix with this test case.

a = np.random.randint(0,255,9).reshape(3,3)
print a == idct2(dct2(a))

但是事实证明,在idct2(dct2(a))之后,与原始矩阵相比,结果以恒定因子进行缩放。

However it turned out that after idct2(dct2(a)) the result was scaled by a constant factor compared with the original a matrix.

我想问一下是否存在是一种实现一组二维dct / idct的方法,以便在进行idct(dct(a))操作后,我可以获得与输入相同的输出。

I would like to ask if there is a way to implement a set of 2D dct/idct such that after a idct(dct(a)) operation I can get the same output as the input.

推荐答案

对于 dct2 和<$ c $,都需要将缩放比例设置为 ortho c> idct2 :

You need to set scaling to ortho for both dct2 and idct2:

def dct2 (block):
  return dct(dct(block.T, norm = 'ortho').T, norm = 'ortho')

您不能期望值完全相同,但是在一定误差范围内几乎相同:

also, you cannot expect the values to be exactly the same, but almost the same within some margin of error:

np.allclose (a, idct2(dct2(a)))

这篇关于在Scipy中,为什么idct(dct(a))不等于a?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆