Manipulations de base sur les images

par Joseph Razik, le 2019-10-18
Manips_Image
In [1]:
%pylab inline
Populating the interactive namespace from numpy and matplotlib
In [2]:
from numpy import empty, mean
import scipy.misc
import PIL.Image as PILI
In [3]:
def quantize(img, niveau, niveau_depart = 256):
    img2 = (img//(2**niveau))*(2**niveau)
    return img2
In [4]:
def neighbour(img, n):
    row, col = img.shape
    img2 = empty(img.shape, dtype=int)
    for i in range(0, row):
        for j in range(0, col):
            if i > n//2 and i < row - n//2:
                if j > n//2 and j < col - n//2:
                    img2[i,j] = int(mean(img[i-n//2:i+n//2+1,j-n//2:j+n//2+1]))
                else:
                    if j > n//2:
                        img2[i,j] = int(mean(img[i-n//2:i+n//2+1,j-n//2:]))
                    else:
                        img2[i,j] = int(mean(img[i-n//2:i+n//2+1,:j+n//2+1]))
            else:
                if i > n//2:
                    if j > n//2 and j < col - n//2:
                        img2[i,j] = int(mean(img[i-n//2:,j-n//2:j+n//2+1]))
                    else:
                        if j > n//2:
                            img2[i,j] = int(mean(img[i-n//2:,j-n//2:]))
                        else:
                            img2[i,j] = int(mean(img[i-n//2:,:j+n//2+1]))
                else:
                    if j > n//2 and j < col - n//2:
                        img2[i,j] = int(mean(img[:i+n//2+1,j-n//2:j+n//2+1]))
                    else:
                        if j > n//2:
                            img2[i,j] = int(mean(img[:i+n//2+1,j-n//2:]))
                        else:
                            img2[i,j] = int(mean(img[:i+n//2+1,:j+n//2+1]))
    return img2
In [5]:
def downscale(img, taille):
    img2 = zeros(img.shape, dtype=int)
    row, col = img.shape
    for i in range(0, row//taille):
        for j in range(0, col//taille):
            img2[i*taille:(i+1)*taille, j*taille:(j+1)*taille] = int(mean(img[i*taille:(i+1)*taille, j*taille:(j+1)*taille]))
    return img2
In [6]:
img = scipy.misc.lena()
img.shape
f = imshow(img, cmap=cm.gray)
In [7]:
# manips sur la quantification
figure(figsize=(12,12));
subplot(3,3,1); f = imshow(img, cmap=cm.gray);  title('Original')
subplot(3,3,2); f = imshow(quantize(img,1), cmap=cm.gray); title('7 bits de quantification')
subplot(3,3,3); f = imshow(quantize(img,2), cmap=cm.gray); title('6 bits de quantification')
subplot(3,3,4); f = imshow(quantize(img,3), cmap=cm.gray); title('5 bits de quantification')
subplot(3,3,5); f = imshow(quantize(img,4), cmap=cm.gray); title('4 bits de quantification')
subplot(3,3,6); f = imshow(quantize(img,5), cmap=cm.gray); title('3 bits de quantification')
subplot(3,3,7); f = imshow(quantize(img,6), cmap=cm.gray); title('2 bits de quantification')
subplot(3,3,8); f = imshow(quantize(img,7), cmap=cm.gray); title('1 bits de quantification')
subplot(3,3,9); f = imshow(quantize(img,8), cmap=cm.gray); title('0 bits de quantification')
Out[7]:
<matplotlib.text.Text at 0x7f9ed1505a58>
In [8]:
# manips en moyenne sur les voisins
figure(figsize=(12,12))
subplot(2,2,1); f = imshow(img, cmap=cm.gray);  title('Original')
subplot(2,2,2); f = imshow(neighbour(img,3), cmap=cm.gray); title('Moyenne blocs de 3')
subplot(2,2,3); f = imshow(neighbour(img,9), cmap=cm.gray); title('Moyenne blocs de 9')
subplot(2,2,4); f = imshow(neighbour(img,21), cmap=cm.gray); title('Moyenne blocs de 21')
Out[8]:
<matplotlib.text.Text at 0x7f9ed1392320>
In [9]:
# manips sur le sous-echantillonage
figure(figsize=(12,12))
subplot(2,2,1); f = imshow(img, cmap=cm.gray);  title('Original')
subplot(2,2,2); f = imshow(downscale(img,3), cmap=cm.gray); title('Sous-echantillonage blocs de 3')
subplot(2,2,3); f = imshow(downscale(img,9), cmap=cm.gray); title('Sous-echantillonage blocs de 9')
subplot(2,2,4); f = imshow(downscale(img,20), cmap=cm.gray); title('Sous-echantillonage blocs de 20')
Out[9]:
<matplotlib.text.Text at 0x7f9ed1214780>
In [10]:
# manips rotation
figure(figsize=(12,12))
imgpil = PILI.fromarray(uint8(img))
subplot(2,2,1) ; imshow(numpy.asarray(imgpil), cmap=cm.gray)
imgpil2 = imgpil.rotate(45)
subplot(2,2,2) ; imshow(numpy.asarray(imgpil2), cmap=cm.gray)
imgpil3 = imgpil.rotate(90)
subplot(2,2,3) ; imshow(numpy.asarray(imgpil3), cmap=cm.gray)
Out[10]:
<matplotlib.image.AxesImage at 0x7f9ed10b3630>