Skip to content

Commit

Permalink
Did Stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasBizzozzero committed May 24, 2018
1 parent 6adb11b commit 62b9d6b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 22 deletions.
47 changes: 38 additions & 9 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,53 @@


PATCH_SIZE = 5
STEP = PATCH_SIZE // 2
STEP = PATCH_SIZE
ALPHA = 1.0
MAX_ITERATIONS = 1000
TOLERANCE = 0.0001
VALUE_MISSING_PIXEL = VALUE_MISSING_PIXEL
VALUE_OUT_OF_BOUNDS = VALUE_OUT_OF_BOUNDS
CODAGE = Codage.RGB
PICTURE_PATH = LENA_COLOR_512


def main():
def main_lena():
# Chargement de l'image
original_picture = Picture(PICTURE_PATH, codage=CODAGE)
original_picture = Picture(picture_path=LENA_COLOR_512, codage=CODAGE)

# Ajout du bruit
noisy_picture = original_picture.copy()
noisy_picture.add_noise(0.005)

main_inpainting(original_picture, noisy_picture)


def main_castle():
# Chargement de l'image
original_picture = Picture(picture_path=CASTLE, codage=CODAGE)

# Ajout du bruit
noisy_picture = original_picture.copy()# Ajout du bruit
noisy_picture.add_rectangle(400, 380, 50, 20)

main_inpainting(original_picture, noisy_picture)


def main_outdoor():
# Chargement de l'image
original_picture = Picture(picture_path=OUTDOOR, codage=CODAGE)

# Ajout du bruit
noisy_picture = original_picture.copy()
# noisy_picture.add_noise(0.01)
noisy_picture.add_rectangle(288, 497, 190, 80)

# On inpaint l'image !
inpainting = InPainting(PATCH_SIZE)
inpainted_picture = inpainting.inpaint(noisy_picture)
main_inpainting(original_picture, noisy_picture)


def main_inpainting(original_picture, noisy_picture):
inpainting = InPainting(patch_size=PATCH_SIZE, step=STEP, alpha=ALPHA, max_iterations=MAX_ITERATIONS,
tolerance=TOLERANCE, value_missing_pixel=VALUE_MISSING_PIXEL,
value_out_of_bounds=VALUE_OUT_OF_BOUNDS)
inpainted_picture = inpainting.inpaint(noisy_picture)
# Affichage des résultats
show_pictures(pictures=[original_picture._get_showable_picture(), noisy_picture._get_showable_picture(),
inpainted_picture._get_showable_picture()],
Expand Down Expand Up @@ -70,4 +99,4 @@ def main_1_vs_all():


if __name__ == "__main__":
main()
main_castle()
6 changes: 3 additions & 3 deletions src/inpainting.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@


class InPainting:
def __init__(self, patch_size: int, step: int = None, value_missing_pixel: np.ndarray = VALUE_MISSING_PIXEL,
value_out_of_bounds: np.ndarray = VALUE_OUT_OF_BOUNDS, alpha: float = 1.0, max_iterations: int = 1000,
tolerance: float = 0.0001):
def __init__(self, patch_size: int, step: int = None, alpha: float = 1.0, max_iterations: int = 1000,
tolerance: float = 0.0001, value_missing_pixel: np.ndarray = VALUE_MISSING_PIXEL,
value_out_of_bounds: np.ndarray = VALUE_OUT_OF_BOUNDS):
self.patch_size = patch_size
self.step = patch_size if step is None else step
self.value_missing_pixel = value_missing_pixel
Expand Down
20 changes: 10 additions & 10 deletions src/picture_tools/picture.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ 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.
"""
for x in range(self.largeur):
for y in range(self.hauteur):
for x in range(self.hauteur):
for y in range(self.largeur):
self.pixels[x, y] = VALUE_MISSING_PIXEL if random.random(
) < threshold else self.pixels[x, y]

Expand All @@ -90,7 +90,7 @@ def add_rectangle(self, x: int, y: int, hauteur: int, largeur: int) -> None:
:param: hauteur, La hauteur du rectangle
:param: largeur, La largeur du rectangle
"""
self.pixels[x:x + largeur, y:y + hauteur] = VALUE_MISSING_PIXEL
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 @@ -115,7 +115,7 @@ def get_patch(self, x: int, y: int, size: int) -> np.ndarray:
new_line = []
for index_y in range(y - (size // 2), y + (size // 2) + 1):
if not self.out_of_bounds(index_x, index_y):
new_line.append(self.get_pixel(index_x, index_y))
new_line.append(self.pixels[index_x, index_y])
else:
new_line.append(VALUE_OUT_OF_BOUNDS)
patch.append(np.array(new_line))
Expand All @@ -125,7 +125,7 @@ def get_patches(self) -> np.ndarray:
""" Retourne tous les indices des centres des patches de l'image contenant des pixels manquants.
:return: Une list de patchs contenant des pixels manquants.
"""
return np.array([np.array([x, y]) for x in range(self.largeur) for y in range(self.hauteur) \
return np.array([np.array([x, y]) for x in range(self.hauteur) for y in range(self.largeur) \
if (self.get_pixel(x, y) == VALUE_MISSING_PIXEL).all()])

def get_dictionnaire(self, size: int, step: int = 1, max_missing_pixel: int = 0) -> np.ndarray:
Expand All @@ -136,8 +136,8 @@ def get_dictionnaire(self, size: int, step: int = 1, max_missing_pixel: int = 0)
: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):
for x in range(0, self.hauteur, step):
for y in range(0, self.largeur, step):
if not self.out_of_bounds_patch(x, y, size):
patch = self.get_patch(x, y, size)
if len(patch[patch == VALUE_MISSING_PIXEL]) // 3 <= max_missing_pixel:
Expand Down Expand Up @@ -166,13 +166,13 @@ def out_of_bounds(self, x: int, y: int) -> bool:
>>> picture.out_of_bounds(-1, 512)
True
"""
return not (0 <= x < self.largeur and 0 <= y < self.hauteur)
return not (0 <= x < self.hauteur and 0 <= y < self.largeur)

def out_of_bounds_patch(self, x: int, y: int, size: int) -> bool:
return (x - (size // 2) < 0) or \
(x + (size // 2) + 1 >= self.largeur) or \
(x + (size // 2) + 1 >= self.hauteur) or \
(y - (size // 2) < 0) or \
(y + (size // 2) + 1 >= self.hauteur)
(y + (size // 2) + 1 >= self.largeur)

def _get_showable_picture(self) -> np.ndarray:
""" Return the picture in a showable format (as in a format which can be plotted by invocating `plt.imshow`on
Expand Down

0 comments on commit 62b9d6b

Please sign in to comment.