Traitement d'images

Traitement d'images Images : Escher, Lenna

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import random as rd

# img = mpimg.imread('/home/kevin/Code/Python/Python_vrac/escher.png')
img = mpimg.imread('/home/kevin/Code/Python/Python_vrac/Lenna.png')
# img = img_to_rgb(img)

## Bruitage

def bruit(img):
    (n,p) = img.shape
    imgg = np.array(img)
    for i in range(n):
        for j in range(p):
            imgg[i,j] = img[i,j] + rd.random() - .5
    return imgg

## Luminosite

def luminosite(img,d):
    (n,p) = img.shape
    imgg = np.array(img)
    for i in range(n):
        for j in range(p):
            imgg[i,j] = img[i,j]+d
    return imgg

## Flou

def flou(img):
    (n,p) = img.shape
    imgg = np.array(img)
    for i in range(1,n-1):
        for j in range(1,p-1):
            imgg[i,j] = np.mean(img[i-1:i+2,j-1:j+2])
    return imgg

## Symétries

def symetrieV(img):
    (n,p)= img.shape
    imgV = np.zeros([n,p])
    for i in range(n):
        for j in range(p):
            imgV[i,j] = img[i,p-1-j]
    return imgV

def symetrieH(img):
    (n,p)= img.shape
    imgH = np.zeros([n,p])
    for i in range(n):
        for j in range(p):
            imgH[i,j] = img[n-1-i,j]
    return imgH

def symetrieV2(img):
    return img[:,-1::-1]

def symetrieH2(img):
    return img[-1::-1,:]

## Pixellisation

def pixel(img,pix):
    (n,p) = img.shape
    imgg = np.array(img)
    for i in range(0,n,pix):
        for j in range(0,p,pix):
            for k in range(pix+1):
                imgg[i:i+k,j:j+k] = np.mean(img[i:i+pix,j:j+pix])
    return imgg

## Filtre

def filtrer(img,mat):
    (n,p) = img.shape
    imgg = np.zeros(img.shape)
    a,b = len(mat), len(mat[0])
    for i in range(a//2,n-a//2):
        for j in range(b//2,p-b//2):
            rect = img[i-a//2:i+a//2+1,j-b//2:j+b//2+1]
            imgg[i,j] = min(max(sum(sum(rect*mat)),0),1)
    return imgg

mat = np.array([[0,-1,0],[-1,5,-1],[0,-1,0]])
# mat = np.array([[0,-1,0],[-1,4,-1],[0,-1,0]])
# mat = np.array([[-1,-1,-1],[-1,8,-1],[-1,-1,-1]])
# mat = np.array([[1/9,1/9,1/9],[1/9,1/9,1/9],[1/9,1/9,1/9]])
# mat = np.array([[0,-1/16,0],[-1/16,5/16,-1/16],[0,-1/16,0]])

## Couleur vers noir niveau de gris

def couleur_vers_gris(img):
    (n,p,_) = img.shape
    imgg = np.zeros([n,p])
    for i in range(n):
        for j in range(p):
            imgg[i,j] = img[i,j,0]*.299+img[i,j,1]*.587+img[i,j,2]*.114
    return imgg

## Bruitage

def bruit_color(img):
    (n,p,_) = img.shape
    imgg = np.array(img)
    for i in range(n):
        for j in range(p):
            imgg[i,j] = img[i,j] + rd.random() - .5
    return imgg

## Luminosite

def luminosite_color(img,d):
    (n,p,_) = img.shape
    imgg = np.array(img)
    for i in range(n):
        for j in range(p):
            imgg[i,j] = img[i,j]+d
    return imgg

## Flou

def flou_color(img):
    (n,p,_) = img.shape
    imgg = np.array(img)
    for i in range(1,n-1):
        for j in range(1,p-1):
            for k in range(3):
                imgg[i,j,k] = np.mean(img[i-1:i+2,j-1:j+2,k])
    return imgg

## Symétries

def symetrieV_color(img):
    (n,p,_)= img.shape
    imgV = np.zeros([n,p])
    for i in range(n):
        for j in range(p):
            imgV[i,j] = img[i,p-1-j]
    return imgV

def symetrieH_color(img):
    (n,p,_)= img.shape
    imgH = np.zeros([n,p])
    for i in range(n):
        for j in range(p):
            imgH[i,j] = img[n-1-i,j]
    return imgH

## Pixellisation

def pixel_color(img,pix):
    (n,p,_) = img.shape
    imgg = np.array(img)
    for i in range(0,n,pix):
        for j in range(0,p,pix):
            for k in range(pix+1):
                for l in range(3):
                    imgg[i:i+k,j:j+k,l] = np.mean(img[i:i+pix,j:j+pix,l])
    return imgg

## Filtre

def filtrer_color(img,mat):
    (n,p,_) = img.shape
    imgg = np.zeros(img.shape)
    a,b = len(mat), len(mat[0])
    for i in range(a//2,n-a//2):
        for j in range(b//2,p-b//2):
            for k in range(3):
                rect = img[i-a//2:i+a//2+1,j-b//2:j+b//2+1,k]
                imgg[i,j,k] = min(max(sum(sum(rect*mat)),0),1)
    return imgg

# mat = np.array([[0,-1,0],[-1,5,-1],[0,-1,0]])
mat = np.array([[0,-1,0],[-1,4,-1],[0,-1,0]])
# mat = np.array([[-1,-1,-1],[-1,8,-1],[-1,-1,-1]])
# mat = np.array([[1/9,1/9,1/9],[1/9,1/9,1/9],[1/9,1/9,1/9]])

## Filtre median

def filtre_median(img):
    (n,p,_) = img.shape
    imgg = np.array(img)
    for i in range(1,n-1):
        for j in range(1,p-1):
            for k in range(3):
                imgg[i,j,k] = np.median(img[i-1:i+2,j-1:j+2,k])
    return imgg

def bruite(img):
    (n,p,_) = img.shape
    imgg = np.zeros([n,p,3])
    for i in range(n):
        for j in range(p):
            if rd.random() > .9:
                imgg[i,j] = [1,1,1]
            else:
                imgg[i,j] = img[i,j]
    return imgg