Using the SciPy DCT function to create a 2D DCT-II

I don't think that a rotation is what you want, since it converts rows into columns, but it also messes with the order of the data. Use np.transpose instead.

To apply dct first by columns, then by rows, you would do something like:

dct(dct(a.T).T)

The trailing .T is equivalent to np.transpose. Note how you need to undo the transposition after you operate on the columns, to get the return aligned by rows again.

I don't think that the order in which you apply the dct, i.e. columns then rows vs. rows then columns, makes any difference, but you could get rows then columns as:

dct(dct(a).T).T

@Jaime's answer is fine. I'll add that dct has an axis argument for just this purpose. First apply it along, say, axis 0, then along axis 1 of the result:

In [30]: from scipy.fftpack import dct

In [31]: a.shape
Out[31]: (8, 8)

In [32]: t1 = dct(dct(a, axis=0), axis=1)

In [33]: t2 = dct(dct(a.T).T)

In [34]: np.abs(t1 - t2).max()
Out[34]: 0.0

Tags:

Scipy