Skip to content

Commit

Permalink
Added add_rectangle, get_patches and get_dictionnaire
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasBizzozzero committed Apr 17, 2018
1 parent 2987b96 commit f22244c
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 16 deletions.
23 changes: 18 additions & 5 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,30 @@

PATH_DIR_USPS = "../res/USPS"

PATCH_SIZE = 201
PATCH_SIZE = 50
STEP = PATCH_SIZE
PICTURE_PATH = LENA_COLOR_512
CODAGE = Codage.RGB


def main():
# Chargement de l'image
picture = Picture(PICTURE_PATH, codage=CODAGE)
picture.add_noise()
# picture.show()
patch = picture.get_patch(122, 122, PATCH_SIZE)
show_patch(patch, codage=CODAGE)
picture.show()

# Ajout du bruit
picture.add_rectangle(45, 45, 50, 80)
picture.add_noise(0.0001)
picture.show()

# Récuperation des patchs et du dictionnaire
patches = picture.get_patches(size=PATCH_SIZE, step=STEP)
dictionnaire = picture.get_dictionnaire(size=PATCH_SIZE, step=STEP)

print("Affichage d'un patch avec pixels manquant")
show_patch(patches[0], codage=CODAGE)
print("Affichage d'un patch sans pixel manquant")
show_patch(dictionnaire[0], codage=CODAGE)


if __name__ == "__main__":
Expand Down
67 changes: 56 additions & 11 deletions src/picture_tools/picture.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
""" Ce module contient toutes les méthodes et fonctions nécessaires pour
pouvoir manipuler facilement des images.
"""
from typing import Tuple
from typing import Tuple, List
import os.path
import random

import matplotlib.pyplot as plt
import numpy as np
from numpy import uint8, float64, int64
from numpy import uint8

from src.picture_tools.codage import Codage, change_codage
from src.common import normalize
Expand All @@ -18,15 +18,18 @@


class Picture:
""" Permet de manipuler très facilement une image tout en supportant plusieurs encodages.
""" Permet de manipuler très facilement une image tout en supportant
plusieurs encodages.
Attributs :
- picture_path : str, le chemin vers l'image.
- codage : Codage, le codage des pixels de l'image (par défaut, HSV).
- pixels : np.ndarray, les pixels (le contenu) de l'image. Quel que soit le codage de l'image, les valeurs des
pixels sont normalisées entre -1 et 1.
- pixels : np.ndarray, les pixels (le contenu) de l'image. Quel que
soit le codage de l'image, les valeurs des pixels sont normalisées
entre -1 et 1.
- hauteur : int, la hauteur de l'image.
- largeur : int, la largeur de l'image.
"""

def __init__(self, picture_path: str, codage: Codage = Codage.HSV):
self.picture_path = picture_path
self.codage = codage
Expand All @@ -39,7 +42,8 @@ def show(self, show: bool = True) -> None:
"""
# Remove missing values
picture = np.copy(self.pixels)
picture[picture == VALUE_MISSING_PIXEL] = np.random.uniform(low=-1, high=1)
picture[picture == VALUE_MISSING_PIXEL] = np.random.uniform(
low=-1, high=1)

picture = change_codage(picture, self.codage, Codage.RGB)
picture = normalize(picture, 0, 255, -1, 1).astype(uint8)
Expand All @@ -58,22 +62,31 @@ def save(self, picture_path: str = None) -> None:

# Remove missing values
picture = np.copy(self.pixels)
picture[picture == VALUE_MISSING_PIXEL] = np.random.uniform(low=-1, high=1)
picture[picture == VALUE_MISSING_PIXEL] = np.random.uniform(
low=-1, high=1)

picture = change_codage(self.pixels, self.codage, Codage.RGB)
picture = normalize(picture, 0, 255, -1, 1).astype(uint8)
plt.imshow(picture)
plt.savefig(picture_path)

def add_noise(self, threshold: float = 0.2):
def add_noise(self, threshold: float = 0.05):
""" Ajoute aléatoirement du bruit dans l'image.
:param: threshold, seuil en dessous duquel on bruite le pixel.
"""
print(self.pixels.min())
for x in range(self.largeur):
for y in range(self.hauteur):
self.pixels[x, y] = VALUE_MISSING_PIXEL if random.random() < threshold else self.pixels[x, y]
print(self.pixels.min())
self.pixels[x, y] = VALUE_MISSING_PIXEL if random.random(
) < threshold else self.pixels[x, y]

def add_rectangle(self, x: int, y: int, hauteur: int, largeur: int) -> None:
""" Ajoute aléatoirement un rectangle de bruit dans l'image.
:param: x, Le pixel d'abscisse x du coint haut gauche du rectangle
:param: y, Le pixel d'ordonnée y du coint haut gauche du rectangle
:param: hauteur, La hauteur du rectangle
:param: largeur, La largeur du rectangle
"""
self.pixels[x:x + hauteur, y:y + largeur] = VALUE_MISSING_PIXEL

def get_pixel(self, x: int, y: int) -> np.ndarray:
""" Retourne le pixel de l'image aux indexes (x, y).
Expand All @@ -92,6 +105,38 @@ def get_patch(self, x: int, y: int, size: int) -> np.ndarray:
"""
return self.pixels[x - (size // 2):x + (size // 2) + 1, y - (size // 2): y + (size // 2) + 1]

def get_patches(self, size: int, step: int = 1, min_missing_pixel: int = 1) -> List[np.ndarray]:
""" Retourne tous les patches de l'image contenant des pixels manquants.
:param: size, la taille de chaque patch.
:param: step, la taille du pas d'itération.
:param: min_missing_pixel, le nombre de pixels manquants à prendre en compte pour retourner ce patch.
:return: Une list de patchs contenant des pixels manquants.
"""
result = []
for x in range(0, self.largeur, step):
for y in range(0, self.hauteur, step):
patch = self.get_patch(x, y, size)
if len(patch[patch == VALUE_MISSING_PIXEL]) // 3 >= min_missing_pixel:
result.append(patch)
return result

def get_dictionnaire(self, size: int, step: int = 1, max_missing_pixel: int = 0) -> List[np.ndarray]:
""" Retourne tous les patches de l'image ne contenant pas de pixels manquants.
:param: size, la taille de chaque patch.
:param: step, la taille du pas d'itération.
:param: max_missing_pixel, le nombre de pixels manquants à prendre en compte pour retourner ce patch.
:return: Une list de patchs ne contenant pas de pixels manquants.
"""
result = []
for x in range(0, self.largeur, step):
for y in range(0, self.hauteur, step):
patch = self.get_patch(x, y, size)
if len(patch[patch == VALUE_MISSING_PIXEL]) // 3 <= max_missing_pixel:
# Prevent out of bound patches to be returned
if 0 not in patch.shape:
result.append(patch)
return result


def show_patch(patch: np.ndarray, codage: Codage = Codage.RGB, show: bool = True):
""" Plot le patch sur matplotlib et l'affiche sur demande.
Expand Down

0 comments on commit f22244c

Please sign in to comment.