annotate transformations/BruitGauss.py @ 106:313c249d638e

Correction bug division par zéro dans BruitGauss.py
author boulanni <nicolas_boulanger@hotmail.com>
date Mon, 15 Feb 2010 15:22:08 -0500
parents 939915371a6d
children 259439a4f9e7
rev   line source
38
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
1 #!/usr/bin/python
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
2 # coding: utf-8
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
3
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
4 '''
62
bab98bb47616 Correction majeure. Auparavant, le lissage gaussien etait global avec une seule gaussienne. Maintenant, le lissage gaussien est local. Un bruit correle est rajoute sur l'image
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents: 38
diff changeset
5 Ajout de bruit gaussien dans les donnees. A chaque iteration, un bruit poivre
bab98bb47616 Correction majeure. Auparavant, le lissage gaussien etait global avec une seule gaussienne. Maintenant, le lissage gaussien est local. Un bruit correle est rajoute sur l'image
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents: 38
diff changeset
6 et sel est ajoute, puis un lissage gaussien autour de ce point est ajoute.
bab98bb47616 Correction majeure. Auparavant, le lissage gaussien etait global avec une seule gaussienne. Maintenant, le lissage gaussien est local. Un bruit correle est rajoute sur l'image
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents: 38
diff changeset
7 On fait un nombre d'iteration = 1024*complexity/25 ce qui equivaud
bab98bb47616 Correction majeure. Auparavant, le lissage gaussien etait global avec une seule gaussienne. Maintenant, le lissage gaussien est local. Un bruit correle est rajoute sur l'image
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents: 38
diff changeset
8 a complexity/25 des points qui recoivent le centre du noyau gaussien.
bab98bb47616 Correction majeure. Auparavant, le lissage gaussien etait global avec une seule gaussienne. Maintenant, le lissage gaussien est local. Un bruit correle est rajoute sur l'image
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents: 38
diff changeset
9 Il y en a beaucoup moins que le bruit poivre et sel, car la transformation
bab98bb47616 Correction majeure. Auparavant, le lissage gaussien etait global avec une seule gaussienne. Maintenant, le lissage gaussien est local. Un bruit correle est rajoute sur l'image
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents: 38
diff changeset
10 est plutôt aggressive et touche beaucoup de pixels autour du centre
38
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
11
62
bab98bb47616 Correction majeure. Auparavant, le lissage gaussien etait global avec une seule gaussienne. Maintenant, le lissage gaussien est local. Un bruit correle est rajoute sur l'image
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents: 38
diff changeset
12 La grandeur de la gaussienne ainsi que son ecart type sont definit par complexity
bab98bb47616 Correction majeure. Auparavant, le lissage gaussien etait global avec une seule gaussienne. Maintenant, le lissage gaussien est local. Un bruit correle est rajoute sur l'image
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents: 38
diff changeset
13 et par une composante aleatoire normale.
38
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
14
89
cc641ee75d3b Il y a maintenant 25% de proba d'effectuer le bruitage
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents: 62
diff changeset
15 On a 25 % de chances d'effectuer le bruitage
cc641ee75d3b Il y a maintenant 25% de proba d'effectuer le bruitage
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents: 62
diff changeset
16
38
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
17 Ce fichier prend pour acquis que les images sont donnees une a la fois
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
18 sous forme de numpy.array de 1024 (32 x 32) valeurs entre 0 et 1.
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
19
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
20 Sylvain Pannetier Lebeuf dans le cadre de IFT6266, hiver 2010
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
21
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
22 '''
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
23
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
24 import numpy
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
25 import random
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
26 import scipy
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
27 from scipy import ndimage
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
28
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
29 class BruitGauss():
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
30
96
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
31 def __init__(self,complexity=1):
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
32 self.nb_chngmax =10 #Le nombre de pixels changes. Seulement pour fin de calcul
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
33 self.grandeurmax = 20
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
34 self.sigmamax = 6.0
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
35 self.regenerate_parameters(complexity)
38
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
36
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
37 def get_settings_names(self):
96
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
38 return ['nb_chng','sigma_gauss','grandeur','effectuer']
38
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
39
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
40 def regenerate_parameters(self, complexity):
96
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
41 self.nb_chng=3+int(numpy.random.rand()*self.nb_chngmax*complexity)
89
cc641ee75d3b Il y a maintenant 25% de proba d'effectuer le bruitage
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents: 62
diff changeset
42
62
bab98bb47616 Correction majeure. Auparavant, le lissage gaussien etait global avec une seule gaussienne. Maintenant, le lissage gaussien est local. Un bruit correle est rajoute sur l'image
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents: 38
diff changeset
43 if float(complexity) > 0:
96
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
44 self.sigma_gauss=2.0 + numpy.random.rand()*self.sigmamax*complexity
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
45 self.grandeur=12+int(numpy.random.rand()*self.grandeurmax*complexity)
89
cc641ee75d3b Il y a maintenant 25% de proba d'effectuer le bruitage
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents: 62
diff changeset
46 self.effectuer =numpy.random.binomial(1,0.25) ##### On a 25% de faire un bruit #####
62
bab98bb47616 Correction majeure. Auparavant, le lissage gaussien etait global avec une seule gaussienne. Maintenant, le lissage gaussien est local. Un bruit correle est rajoute sur l'image
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents: 38
diff changeset
47 else:
89
cc641ee75d3b Il y a maintenant 25% de proba d'effectuer le bruitage
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents: 62
diff changeset
48 self.effectuer = 0
106
313c249d638e Correction bug division par zéro dans BruitGauss.py
boulanni <nicolas_boulanger@hotmail.com>
parents: 99
diff changeset
49 self.sigma_gauss = 1 # eviter division par 0
62
bab98bb47616 Correction majeure. Auparavant, le lissage gaussien etait global avec une seule gaussienne. Maintenant, le lissage gaussien est local. Un bruit correle est rajoute sur l'image
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents: 38
diff changeset
50 self.grandeur=1
bab98bb47616 Correction majeure. Auparavant, le lissage gaussien etait global avec une seule gaussienne. Maintenant, le lissage gaussien est local. Un bruit correle est rajoute sur l'image
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents: 38
diff changeset
51 #Un peu de paranoia ici, mais on ne sait jamais
bab98bb47616 Correction majeure. Auparavant, le lissage gaussien etait global avec une seule gaussienne. Maintenant, le lissage gaussien est local. Un bruit correle est rajoute sur l'image
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents: 38
diff changeset
52
96
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
53 #creation du noyau gaussien
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
54 self.gauss=numpy.zeros((self.grandeur,self.grandeur))
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
55 x0 = y0 = self.grandeur/2.0
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
56 for i in xrange(self.grandeur):
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
57 for j in xrange(self.grandeur):
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
58 self.gauss[i,j]=numpy.exp(-((i-x0)**2 + (j-y0)**2) / self.sigma_gauss**2)
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
59 #creation de la fenetre de moyennage
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
60 self.moy=numpy.zeros((self.grandeur,self.grandeur))
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
61 x0 = y0 = self.grandeur/2
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
62 for i in xrange(0,self.grandeur):
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
63 for j in xrange(0,self.grandeur):
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
64 self.moy[i,j]=((numpy.sqrt(2*(self.grandeur/2.0)**2) - numpy.sqrt(numpy.abs(i-self.grandeur/2.0)**2+numpy.abs(j-self.grandeur/2.0)**2))/\
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
65 numpy.sqrt((self.grandeur/2.0)**2))**5
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
66
38
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
67 return self._get_current_parameters()
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
68
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
69 def _get_current_parameters(self):
96
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
70 return [self.nb_chng,self.sigma_gauss,self.grandeur,self.effectuer]
38
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
71
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
72
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
73 def transform_image(self, image):
89
cc641ee75d3b Il y a maintenant 25% de proba d'effectuer le bruitage
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents: 62
diff changeset
74 if self.effectuer == 0:
cc641ee75d3b Il y a maintenant 25% de proba d'effectuer le bruitage
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents: 62
diff changeset
75 return image
62
bab98bb47616 Correction majeure. Auparavant, le lissage gaussien etait global avec une seule gaussienne. Maintenant, le lissage gaussien est local. Un bruit correle est rajoute sur l'image
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents: 38
diff changeset
76 image=image.reshape((32,32))
96
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
77 filtered_image = ndimage.convolve(image,self.gauss,mode='constant')
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
78 assert image.shape == filtered_image.shape
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
79 filtered_image = (filtered_image - filtered_image.min() + image.min()) / (filtered_image.max() - filtered_image.min() + image.min()) * image.max()
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
80
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
81 #construction of the moyennage Mask
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
82 Mask = numpy.zeros((32,32))
62
bab98bb47616 Correction majeure. Auparavant, le lissage gaussien etait global avec une seule gaussienne. Maintenant, le lissage gaussien est local. Un bruit correle est rajoute sur l'image
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents: 38
diff changeset
83
bab98bb47616 Correction majeure. Auparavant, le lissage gaussien etait global avec une seule gaussienne. Maintenant, le lissage gaussien est local. Un bruit correle est rajoute sur l'image
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents: 38
diff changeset
84 for i in xrange(0,self.nb_chng):
bab98bb47616 Correction majeure. Auparavant, le lissage gaussien etait global avec une seule gaussienne. Maintenant, le lissage gaussien est local. Un bruit correle est rajoute sur l'image
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents: 38
diff changeset
85 x_bruit=int(numpy.random.randint(0,32))
bab98bb47616 Correction majeure. Auparavant, le lissage gaussien etait global avec une seule gaussienne. Maintenant, le lissage gaussien est local. Un bruit correle est rajoute sur l'image
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents: 38
diff changeset
86 y_bruit=int(numpy.random.randint(0,32))
96
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
87 offsetxmin = 0
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
88 offsetxmax = 0
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
89 offsetymin = 0
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
90 offsetymax = 0
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
91 if x_bruit < self.grandeur / 2:
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
92 offsetxmin = self.grandeur / 2 - x_bruit
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
93 if 32-x_bruit < numpy.ceil(self.grandeur / 2.0):
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
94 offsetxmax = numpy.ceil(self.grandeur / 2.0) - (32-x_bruit)
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
95 if y_bruit < self.grandeur / 2:
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
96 offsetymin = self.grandeur / 2 - y_bruit
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
97 if 32-y_bruit < numpy.ceil(self.grandeur / 2.0):
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
98 offsetymax = numpy.ceil(self.grandeur / 2.0) - (32-y_bruit)
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
99 Mask[x_bruit - self.grandeur/2 + offsetxmin : x_bruit + numpy.ceil(self.grandeur/2.0) - offsetxmax,\
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
100 y_bruit - self.grandeur/2 + offsetymin : y_bruit + numpy.ceil(self.grandeur/2.0)- offsetymax] +=\
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
101 self.moy[offsetxmin:self.grandeur - offsetxmax,offsetymin:self.grandeur - offsetymax]
62
bab98bb47616 Correction majeure. Auparavant, le lissage gaussien etait global avec une seule gaussienne. Maintenant, le lissage gaussien est local. Un bruit correle est rajoute sur l'image
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents: 38
diff changeset
102
96
17fabd74dd2b Changed behavior of gaussian noise to apply a gauss filter with a random sigma, and then select some point of the image to be averaged with it
Xavier Glorot <glorotxa@iro.umontreal.ca>
parents: 89
diff changeset
103 return numpy.asarray((image + filtered_image*Mask)/(Mask+1),dtype='float32')
38
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
104
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
105 #---TESTS---
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
106
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
107 def _load_image():
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
108 f = open('/home/sylvain/Dropbox/Msc/IFT6266/donnees/lower_test_data.ft') #Le jeu de donnees est en local.
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
109 d = ft.read(f)
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
110 w=numpy.asarray(d[0])
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
111 return (w/255.0).astype('float')
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
112
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
113 def _test(complexite):
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
114 img=_load_image()
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
115 transfo = BruitGauss()
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
116 pylab.imshow(img.reshape((32,32)))
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
117 pylab.show()
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
118 print transfo.get_settings_names()
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
119 print transfo.regenerate_parameters(complexite)
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
120
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
121 img_trans=transfo.transform_image(img)
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
122
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
123 pylab.imshow(img_trans.reshape((32,32)))
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
124 pylab.show()
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
125
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
126
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
127 if __name__ == '__main__':
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
128 from pylearn.io import filetensor as ft
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
129 import pylab
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
130 _test(0.5)
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
131
349d8dc9504c Ajout de bruit poivre et sel, puis filtre gaussien. Parametres deterministes par rapport a complexity. Compatible testmod.py
SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
parents:
diff changeset
132