如何在 Python 中将 DCT 应用于图像?

发布于 2024-11-30 10:03:54 字数 84 浏览 3 评论 0 原文

我想在 Python 中对图像应用离散余弦变换(以及逆变换),我想知道最好的方法是什么以及如何实现。我看过PIL和OpenCV,但我仍然不明白如何使用它。

I want to apply a Discrete Cosine Transform (as well as the inverse) to an image in Python and I'm wondering what is the best way to do it and how. I've looked at PIL and OpenCV but I still don't understand how to use it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千寻… 2024-12-07 10:03:54

scipy.fftpack 示例:

from scipy.fftpack import dct, idct

# implement 2D DCT
def dct2(a):
    return dct(dct(a.T, norm='ortho').T, norm='ortho')

# implement 2D IDCT
def idct2(a):
    return idct(idct(a.T, norm='ortho').T, norm='ortho')    

from skimage.io import imread
from skimage.color import rgb2gray
import numpy as np
import matplotlib.pylab as plt

# read lena RGB image and convert to grayscale
im = rgb2gray(imread('images/lena.jpg')) 
imF = dct2(im)
im1 = idct2(imF)

# check if the reconstructed image is nearly equal to the original image
np.allclose(im, im1)
# True

# plot original and reconstructed images with matplotlib.pylab
plt.gray()
plt.subplot(121), plt.imshow(im), plt.axis('off'), plt.title('original image', size=20)
plt.subplot(122), plt.imshow(im1), plt.axis('off'), plt.title('reconstructed image (DCT+IDCT)', size=20)
plt.show()

此外,如果绘制一小部分 2D DCT 系数数组 imF(在 log 域中),您将得到如下图(带有棋盘图案):

在此处输入图像描述

Example with scipy.fftpack:

from scipy.fftpack import dct, idct

# implement 2D DCT
def dct2(a):
    return dct(dct(a.T, norm='ortho').T, norm='ortho')

# implement 2D IDCT
def idct2(a):
    return idct(idct(a.T, norm='ortho').T, norm='ortho')    

from skimage.io import imread
from skimage.color import rgb2gray
import numpy as np
import matplotlib.pylab as plt

# read lena RGB image and convert to grayscale
im = rgb2gray(imread('images/lena.jpg')) 
imF = dct2(im)
im1 = idct2(imF)

# check if the reconstructed image is nearly equal to the original image
np.allclose(im, im1)
# True

# plot original and reconstructed images with matplotlib.pylab
plt.gray()
plt.subplot(121), plt.imshow(im), plt.axis('off'), plt.title('original image', size=20)
plt.subplot(122), plt.imshow(im1), plt.axis('off'), plt.title('reconstructed image (DCT+IDCT)', size=20)
plt.show()

Also, if you plot a small slice of the 2D DCT coefficients array imF (in log domain), you will get a figure like the following (with a checkerboard pattern):

enter image description here

泪冰清 2024-12-07 10:03:54

来自OpenCV

DCT(src, dst, flags) → None

    Performs a forward or inverse Discrete Cosine transform of a 1D or 2D 
    floating-point array.

    Parameters: 

        src (CvArr) – Source array, real 1D or 2D array
        dst (CvArr) – Destination array of the same size and same type as the source
        flags (int) –

        Transformation flags, a combination of the following values
            CV_DXT_FORWARD do a forward 1D or 2D transform.
            CV_DXT_INVERSE do an inverse 1D or 2D transform.
            CV_DXT_ROWS do a forward or inverse transform of every individual row of 
the input matrix. This flag allows user to transform multiple vectors simultaneously 
and can be used to decrease the overhead (which is sometimes several times larger 
than the processing itself), to do 3D and higher-dimensional transforms and so forth.

这是一个使用的示例

DCT 也可以在 scipy 中找到.fftpack

From OpenCV:

DCT(src, dst, flags) → None

    Performs a forward or inverse Discrete Cosine transform of a 1D or 2D 
    floating-point array.

    Parameters: 

        src (CvArr) – Source array, real 1D or 2D array
        dst (CvArr) – Destination array of the same size and same type as the source
        flags (int) –

        Transformation flags, a combination of the following values
            CV_DXT_FORWARD do a forward 1D or 2D transform.
            CV_DXT_INVERSE do an inverse 1D or 2D transform.
            CV_DXT_ROWS do a forward or inverse transform of every individual row of 
the input matrix. This flag allows user to transform multiple vectors simultaneously 
and can be used to decrease the overhead (which is sometimes several times larger 
than the processing itself), to do 3D and higher-dimensional transforms and so forth.

Here is an example of it being used.

The DCT is also available in scipy.fftpack.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文