2016-06-20 23 views
1

我在scthon中使用scipy.fftpack.dct和scipy.fftpack.idct來創建圖像數組。然而,我不想將它應用於整個圖像,而是應用於圖像中的單個8x8塊。這是簡單的類我寫的用於測試該python scipy DCT對圖像上的較小塊不起作用

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的8×8塊,然後IDCT權之後,將所有的信息丟失和圖像看起來像一個爛攤子。所有我打電話是

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

當我運行這個,圖像都只是噪音。但是,如果您在apply_dct()和apply_idct()中看到了一些註釋,那麼我在哪裏試圖在整個映像上嘗試DCT和IDCT,而不是在8x8塊上。當我這樣做的時候它完美地工作,當我嘗試8x8塊時,它不起作用,我需要將它應用到8x8塊而不是整個圖像。

如果需要額外的信息,圖像是灰色的,所以只有1個通道。

回答

1

檢查圖像數組的數據類型(self.image.dtype)。它可能是8位無符號整數。 DCT將是浮點值,但是當您將DCT的結果分配給位置爲的8x8塊時,浮點值將轉換爲8位整數。然後,在應用IDCT時也會發生同樣的情況。

避免該問題的一個選項是將圖像轉換爲__init__()中的64位浮點,例如im = np.asarray(Image.open(image_name), dtype=np.float64)。這是否有意義取決於你要對陣列做些什麼。