Skip to content

Commit

Permalink
Added get_pixel, get_patch and show_patch
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasBizzozzero committed Apr 15, 2018
1 parent 627ae02 commit 398555c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 19 deletions.
10 changes: 7 additions & 3 deletions src/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@
Number = Union[int, float]


def normalize(array: np.ndarray, floor: Number = 0, ceil: Number = 1):
def normalize(array: np.ndarray, floor: Number = 0, ceil: Number = 1, origin_floor: Number = None,
origin_ceil: Number = None):
""" Normalise un tableau dans un seuil minimal et maximal.
:param: array, le tableau à normaliser.
:param: floor, le seuil bas de la normalisation.
:param: ceil, le seuil haut de la normalisation.
:param: origin_floor, la valeur minimale dans le tableau de départ.
:param: origin_ceil, le valeur maximale dans le tableau de départ.
:return: Le tableau normalisé.
"""
array_min, array_max = array.min(), array.max()
array_min = origin_floor if origin_floor is not None else array.min()
array_max = origin_ceil if origin_ceil is not None else array.max()
upper = (ceil - floor) / (array_max - array_min)
lower = (ceil - upper * array_max) # Or (floor - upper * array_min)
lower = (ceil - upper * array_max) # or (floor - upper * array_min)
return upper * array + lower


Expand Down
10 changes: 7 additions & 3 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@
LENA_GRAY_512, LIVINGROOM, MANDRIL_COLOR, MANDRIL_GRAY, PEPPERS_COLOR, PEPPERS_GRAY, PIRATE, WALKBRIDGE, \
WOMAN_BLONDE, WOMAN_DARKHAIR
from src.picture_tools.codage import Codage
from src.picture_tools.picture import Picture
from src.picture_tools.picture import Picture, show_patch


PATH_DIR_USPS = "../res/USPS"

PATCH_SIZE = 201
PICTURE_PATH = LENA_COLOR_512
CODAGE = Codage.RGB


def main():
picture = Picture(PICTURE_PATH, codage=Codage.HSV)
picture = Picture(PICTURE_PATH, codage=CODAGE)
picture.add_noise()
picture.show()
# picture.show()
patch = picture.get_patch(122, 122, PATCH_SIZE)
show_patch(patch, codage=CODAGE)


if __name__ == "__main__":
Expand Down
66 changes: 53 additions & 13 deletions src/picture_tools/picture.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

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

from src.picture_tools.codage import Codage, change_codage
from src.common import normalize


VALUE_MISSING_PIXEL = -100
VALUE_MISSING_PIXEL = np.ones((3,)) * -100


class Picture:
Expand All @@ -33,15 +33,16 @@ def __init__(self, picture_path: str, codage: Codage = Codage.HSV):
pixels, self.hauteur, self.largeur = _load_pixels(picture_path)
self.pixels = change_codage(pixels, Codage.RGB, self.codage)

def show(self, show=True) -> None:
def show(self, show: bool = True) -> None:
""" Plot l'image sur matplotlib et l'affiche sur demande.
:param: show, waut `True` si on affiche l'image après l'avoir plottée.
"""
picture = change_codage(self.pixels, self.codage, Codage.RGB)
print("to RGB", picture.shape, picture.min(), picture.max(), picture.argmin(), picture.argmax())
picture = normalize(picture, 0, 255).astype(uint8)
print("normalized", picture.shape, picture.min(), picture.max(), picture.argmin(), picture.argmax())
print(picture[0, 0])
# Remove missing values
picture = np.copy(self.pixels)
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)
plt.imshow(picture)
if show:
plt.show()
Expand All @@ -54,19 +55,59 @@ def save(self, picture_path: str = None) -> None:
"""
if picture_path is None:
picture_path = os.path.basename(self.picture_path)

# Remove missing values
picture = np.copy(self.pixels)
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)
picture = normalize(picture, 0, 255, -1, 1).astype(uint8)
plt.imshow(picture)
plt.savefig(picture_path)

def add_noise(self, threshold: float = 0.2):
""" 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] = np.random.randint(low=-1, high=1, size=(3,)) \
if random.random() < threshold else self.pixels[x, y]
self.pixels[x, y] = VALUE_MISSING_PIXEL if random.random() < threshold else self.pixels[x, y]
print(self.pixels.min())

def get_pixel(self, x: int, y: int) -> np.ndarray:
""" Retourne le pixel de l'image aux indexes (x, y).
:param: x, l'index de la colonne.
:param: y, l'index de la ligne.
:return: Le contenu du pixel demandé.
"""
return self.pixels[x, y]

def get_patch(self, x: int, y: int, size: int) -> np.ndarray:
""" Retourne le patch de l'image centré aux indexes (x, y).
:param: x, l'index de la colonne.
:param: y, l'index de la ligne.
:param: size, la longueur du patch.
:return: Le contenu du patch demandé.
"""
return self.pixels[x - (size // 2):x + (size // 2) + 1, y - (size // 2): y + (size // 2) + 1]


def show_patch(patch: np.ndarray, codage: Codage = Codage.RGB, show: bool = True):
""" Plot le patch sur matplotlib et l'affiche sur demande.
:param: patch, le patch à afficher.
:param: codage, le codage utilisé pour le patch.
:param: show, waut `True` si on affiche le patch après l'avoir plottée.
"""
# Remove missing values
new_patch = np.copy(patch)
new_patch[patch == VALUE_MISSING_PIXEL] = np.random.uniform(low=-1, high=1)

new_patch = change_codage(new_patch, codage, Codage.RGB)
new_patch = normalize(new_patch, 0, 255, -1, 1).astype(uint8)
plt.imshow(new_patch)
if show:
plt.show()


def _load_pixels(picture_path: str) -> Tuple[np.ndarray, int, int]:
Expand All @@ -92,8 +133,7 @@ def _load_pixels(picture_path: str) -> Tuple[np.ndarray, int, int]:
image_hauteur, image_largeur, _ = pixels.shape

# Normalisation des pixels dans l'intervalle -1, 1
print(pixels[0, 0], pixels.dtype)
pixels = normalize(pixels, -1, 1)
pixels = normalize(pixels, -1, 1, 0, 255)

return pixels, image_hauteur, image_largeur

Expand Down

0 comments on commit 398555c

Please sign in to comment.