In [1]:
%pylab inline
In [2]:
import scipy.misc
from scipy.fftpack import dct, idct
In [3]:
def decoupe_bloc(image, N = 8):
l, c = image.shape
# découpage en blocs de 8x8 pixels
# tant pis pour les bords si la taille n'est pas multiple de 8
# blocs = [image[i*8:(i+1)*8, j*8:(j+1)*8] for i in xrange(0, l/8) for j in xrange(0, c/8)]
blocs = []
for i in range(0,l//N):
for j in range(0,c//N):
blocs.append(image[i*N:(i+1)*N, j*N:(j+1)*N])
return blocs, l//N, c//N
In [4]:
def do_jpg(image, N = 32):
l, c = image.shape
# découpage en blocs de 8x8 pixels
blocs, nb_bl, nb_bc = decoupe_bloc(image)
# on fait la dct de chaque bloc
dct_blocs = [dct(b, norm='ortho') for b in blocs]
# quantification
quantized_blocs = [((b//N).astype(int)*N).astype(float) for b in dct_blocs]
return quantized_blocs, l//8, c//8
#return dct_blocs, l/8, c/8
In [5]:
def invert_jpg(q_blocs, nb_bl, nb_bc):
# dct inverse
dct_blocs = [idct(b, norm='ortho') for b in q_blocs]
# reconstruction de l'image
image = reshape_im(dct_blocs, nb_bl, nb_bc)
return image
In [6]:
def reshape_im(blocs, nb_bl, nb_bc, N = 8):
image = empty((nb_bl*N, nb_bc*N))
for i in range(0, nb_bl):
for j in range(0, nb_bc):
image[i*N:(i+1)*N, j*N:(j+1)*N] = blocs[i*nb_bl + j]
return image
In [7]:
img = scipy.misc.lena()
print(img.shape)
f = imshow(img, cmap=cm.gray)
In [8]:
figure(figsize=(10,12))
for i in range(0,4):
subplot(4, 4, i+1)
imshow(img[0:8,8*i:8*(i+1)], cmap=cm.gray)
title(i)
subplot(4, 4, i+1+4)
imshow(img[0:8,8*(i+60):8*(i+1+60)], cmap=cm.gray)
title(i+60)
subplot(4, 4, i+1+8)
imshow(img[8:16,8*i:8*(i+1)], cmap=cm.gray)
title(i+64)
subplot(4, 4, i+1+4+8)
imshow(img[8:16,8*(i+60):8*(i+1+60)], cmap=cm.gray)
title(i+60+64)
In [9]:
print(img.dtype)
img2 = img.astype(float)
print(img2.dtype)
b, nb_bl, nb_bc = decoupe_bloc(img2)
In [10]:
img3 = reshape_im(b, nb_bl, nb_bc)
In [11]:
figure(figsize=(10,12))
for i in range(0,4):
subplot(4, 4, i+1)
imshow(img3[0:8,8*i:8*(i+1)], cmap=cm.gray)
title(i)
subplot(4, 4, i+1+4)
imshow(img3[0:8,8*(i+60):8*(i+1+60)], cmap=cm.gray)
title(i+60)
subplot(4, 4, i+1+8)
imshow(img3[8:16,8*i:8*(i+1)], cmap=cm.gray)
title(i+64)
subplot(4, 4, i+1+4+8)
imshow(img3[8:16,8*(i+60):8*(i+1+60)], cmap=cm.gray)
title(i+60+64)
In [12]:
imshow(img3, cmap=cm.gray)
Out[12]:
In [13]:
img_j, nb_bl, nb_bc = do_jpg(img2)
In [14]:
imshow(reshape_im(img_j, nb_bl, nb_bc), cmap=cm.gray)
Out[14]:
In [15]:
img4 = invert_jpg(img_j, nb_bl, nb_bl)
In [16]:
imshow(img4, cmap=cm.gray)
Out[16]:
In [17]:
from numpy.fft import fft; fft2, ifft2
def do_jpg_fft(image, N=32):
l, c = image.shape
# découpage en blocs de 8x8 pixels
blocs, nb_bl, nb_bc = decoupe_bloc(image)
# on fait la dct de chaque bloc
dct_blocs = [fft2(b) for b in blocs]
# quantification
quantized_blocs = [((b//N).astype(int))*N for b in dct_blocs]
return quantized_blocs, l//8, c//8
In [18]:
img_j_fft, nb_nl, nb_nc = do_jpg_fft(img2)
In [19]:
def invert_jpg_fft(q_blocs, nb_bl, nb_bc):
# dct inverse
dct_blocs = [abs(ifft2(b,)) for b in q_blocs]
# reconstruction de l'image
image = reshape_im(dct_blocs, nb_bl, nb_bc)
return image
In [20]:
img5 = invert_jpg_fft(img_j_fft, nb_bl, nb_bl)
In [21]:
imshow(img5, cmap=cm.gray)
Out[21]:
In [22]:
img6 = (img//32)*32
imshow(img6, cmap=cm.gray)
Out[22]:
In [23]:
def rgb2ycbcr(image):
im = empty(image.shape)
im[:,:,0] = 77*image[:,:,0] + 150*image[:,:,1] + 29*image[:,:,2]
im[:,:,1] = ((-44*image[:,:,0] - 87*image[:,:,1] + 131*image[:,:,2])//256 + 0.5)*256
im[:,:,2] = ((131*image[:,:,0] - 110*image[:,:,1] - 21*image[:,:,3])//256 + 0.5)*256
return im