【发布时间】: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