2016-06-20 12 views
1

python의 이미지 배열에 scipy.fftpack.dct 및 scipy.fftpack.idct를 사용하고 있습니다. 그러나 전체 이미지에 적용하는 것이 아니라 이미지의 개별 8x8 블록에 적용하고 싶습니다. 이것은 내가 데 문제는 내가 8 × 8 블록에 DCT를 적용 할 때, 다음 IDCT 바로 이후에 모든 정보가 손실된다는 점이다이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 

을 테스트에 쓴 간단한 클래스이며, 이미지는 엉망처럼 보인다 . 제가 부르고있는 것은

입니다.이 이미지를 실행하면 이미지가 모두 잡음에 불과합니다. 그러나 apply_dct() 및 apply_idct()에서 보면 8x8 블록 대신 전체 이미지에서 DCT 및 IDCT를 시도한 부분이 주석 처리되어 있습니다. 이 작업을 수행하면 완벽하게 작동하지만 8x8 블록을 사용하려고하면 작동하지 않으며 전체 이미지가 아닌 8x8 블록에만 적용해야합니다.

추가 정보 필요한 경우 이미지는 회색조이므로 채널이 하나뿐입니다.

답변

1

이미지 배열의 데이터 유형 (self.image.dtype)을 확인하십시오. 아마 8 비트 부호없는 정수입니다. DCT는 부동 소수점 값이지만, DCT의 결과를 8x8 블록 의 자리에에 할당하면 부동 소수점 값이 8 비트 정수로 변환됩니다. IDCT를 적용하면 똑같은 일이 일어납니다.

이미지를 __init__()의 64 비트 부동 소수점으로 변환하는 옵션이 있습니다 (예 : im = np.asarray(Image.open(image_name), dtype=np.float64)). 그게 의미가 있는지 여부는 어레이로 무엇을 할 것인지에 달려 있습니다.