Commit 1a295b77 authored by Claire Lemoine's avatar Claire Lemoine
Browse files

sketching

parent d88635a4
#!/usr/bin/env python3
"""
Transforme une image en dessin noir et blanc.
"""
import matplotlib
matplotlib.use("TkAgg")
import cv2
import matplotlib.pyplot as plt
import numpy as np
import os
def scketching(filename):
"""
|==============================|
| Donne l'impression a l'image |
| d'etre dessinee au crayon. |
|==============================|
parametres
----------
:param filename: Chemin qui pointe sur l'image a transformer.
:type filename: str
sortie
------
:return: L'image apres transformation.
:rtype: np.ndarray
"""
assert isinstance(filename, str), "'filename' doit etre un str, pas un %s." % type(filename).__name__
assert os.path.isfile(filename), "%s n'est pas un fichier." % repr(filename)
image = cv2.imread(filename, cv2.IMREAD_GRAYSCALE) # Lecture de l'image, directement avec la bonne luminance
plt.subplot(3, 3, 1)
plt.title("Initiale")
plt.imshow(image, vmin=0, vmax=255, cmap="gray")
fourier = np.fft.fftshift(np.fft.fft2(image))
plt.subplot(3, 3, 2)
plt.title("Fourier")
plt.imshow(np.log(1 + np.abs(fourier)), cmap="gray")
rh, rl, delta = 3.0, 0.5, 0.5
rh, rl, delta = 0.8, 0.2, 0.06
matrice = np.zeros(fourier.shape)
[U, V] = np.ogrid[-0.5:0.5:1.0/matrice.shape[0], -0.5:0.5:1.0/matrice.shape[1]]
filtre = (rh-rl)*(1-np.exp(-(U*U+V*V)/(2*delta**2))) + rl
plt.subplot(3, 3, 3)
plt.title("Filtre")
plt.imshow(filtre, vmin=0, vmax=1, cmap="gray")
image = np.abs(np.fft.ifft2(np.fft.ifftshift(fourier*filtre)))
image *= 255/np.max(image)
plt.subplot(3, 3, 4)
plt.title("Sans continue")
plt.imshow(image, vmin=0, vmax=255, cmap="gray")
image = cv2.equalizeHist(np.uint8(image))
plt.subplot(3, 3, 5)
plt.title("Histograme egalise")
plt.imshow(image, vmin=0, vmax=255, cmap="gray")
image = 255 - image
plt.subplot(3, 3, 6)
plt.title("Inversee")
plt.imshow(image, vmin=0, vmax=255, cmap="gray")
facteur = min(image.shape)/150
gauss = cv2.GaussianBlur(image, (0, 0), facteur)
plt.subplot(3, 3, 7)
plt.title("Lissee")
plt.imshow(gauss, vmin=0, vmax=255, cmap="gray")
rapport = gauss/(image + 1/255)
seuil = 1.0
ancien = set()
while np.round(np.mean(image)) != np.round(230/255, 2) and seuil not in ancien:
ancien.add(seuil)
if np.mean(image) < 230/255:
seuil /= 1.01
else:
seuil *= 1.01
print(seuil)
image = rapport*(rapport<seuil) + (rapport>=seuil)
image /= np.max(image)
image *= 255
plt.subplot(3, 3, 8)
plt.title("crayon")
plt.imshow(image, cmap="gray")
image = cv2.GaussianBlur(cv2.equalizeHist(np.uint8(image)), (0, 0), facteur/5)
plt.subplot(3, 3, 9)
plt.title("crayon egalise hist")
plt.imshow(image, cmap="gray")
plt.show()
plt.imshow(image, vmin=0, vmax=255, cmap="gray")
plt.show()
return np.uint8(image)
def main():
# target = 'robin_souriant.jpg'
target = 'soeur.jpg'
# dirpath = '/home/robin/documents/coffre/administratif'
dirpath = ""
filepath = os.path.join(dirpath, target)
print(filepath)
scketching(filepath)
if __name__ == '__main__':
main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment