【问题标题】:Python scipy DCT on smaller block on image not working图像上较小块上的 Python scipy DCT 不起作用
【发布时间】:2016-06-20 21:22:21
【问题描述】:

我正在将 scipy.fftpack.dct 和 scipy.fftpack.idct 用于 python 中的图像数组。但是,我不想将其应用于整个图像,而是应用于图像中的单个 8x8 块。这是我写的用于测试的简单类

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

class ImageController():
    def __init__(self, image_name):
        im = np.asarray(Image.open(image_name))
        self.origional_size = im.shape
        im_x_dim, im_y_dim = self.origional_size
        self.image = im
        if not self.image.flags.writeable:
            self.image.flags.writeable = True

    def get(self):
        return self.image

    def display(self):
        Image.fromarray(self.image).show()
        return self

    def apply_dct(self):
        # self.image = dct(self.image, norm='ortho')
        self.loop_through_8_x_8(dct)
        return self

    def apply_idct(self):
        # self.image = idct(self.image, norm='ortho')
        self.loop_through_8_x_8(idct)
        return self

    def loop_through_8_x_8(self, appyFunc):
        print appyFunc
        row = 0
        while row < len(self.image):
            col = 0
            while col < len(self.image[row]):
                self.image[row:row+8, self.get_list(col)] = appyFunc(self.image[row:row+8, self.get_list(col)] , norm='ortho')
                col += 8
            row += 8
        print row, col
        return self;

    def get_list(self, index):
        x = []
        for i in range(index, index + 8):
            x.append(i)
        return x

我遇到的问题是,当我将 DCT 应用于 8x8 块时,IDCT 之后所有信息都丢失了,图像看起来一团糟。我只是打电话给

ImageController('lena.jpg').apply_dct().apply_idct().display()

当我运行它时,图像都是噪点。但是,如果您在 apply_dct() 和 apply_idct() 中看到,我有一些注释掉了,这是我在整个图像上而不是在 8x8 块上尝试 DCT 和 IDCT 的地方。当我这样做时,它工作得很好,但当我尝试 8x8 块时它不起作用,我需要将它应用到 8x8 块而不是整个图像。

如果需要额外信息,图像是灰度的,所以只有 1 个通道。

【问题讨论】:

    标签: python image numpy scipy dct


    【解决方案1】:

    检查图像数组的数据类型 (self.image.dtype)。它可能是 8 位无符号整数。 DCT 将是浮点值,但是当您将 DCT 的结果分配给 就地 的 8x8 块时,浮点值将转换为 8 位整数。然后,当您应用 IDCT 时,也会发生同样的事情。

    避免该问题的一种方法是将图像转换为 __init__() 中的 64 位浮点数,例如 im = np.asarray(Image.open(image_name), dtype=np.float64)。这是否有意义取决于您还要对数组做什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-12
      • 2012-12-13
      • 2014-08-13
      • 2018-03-11
      • 2015-08-26
      • 2014-09-07
      • 1970-01-01
      • 2012-12-28
      相关资源
      最近更新 更多