Mercurial > ift6266
comparison transformations/PermutPixel.py @ 46:48a21d19b8eb
Effectue une permutation aleatoire de certains pixels choisit avec un voisin
author | SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca> |
---|---|
date | Thu, 04 Feb 2010 10:30:27 -0500 |
parents | |
children | 9936c4886299 |
comparison
equal
deleted
inserted
replaced
45:f8a92292b299 | 46:48a21d19b8eb |
---|---|
1 #!/usr/bin/python | |
2 # coding: utf-8 | |
3 | |
4 ''' | |
5 Un echange de pixels est effectue entre certain pixels choisit aleatoirement | |
6 et un de ses 4 voisins, tout aussi choisi aleatoirement. | |
7 | |
8 Le nombre de pixels permutes est definit pas complexity*1024 | |
9 | |
10 Sylvain Pannetier Lebeuf dans le cadre de IFT6266, hiver 2010 | |
11 | |
12 ''' | |
13 | |
14 import numpy | |
15 import random | |
16 | |
17 class PermutPixel(): | |
18 | |
19 def __init__(self): | |
20 self.nombre=10 #Le nombre de pixels a permuter | |
21 self.proportion=0.3 | |
22 | |
23 def get_settings_names(self): | |
24 return ['nombre'] | |
25 | |
26 def regenerate_parameters(self, complexity): | |
27 self.proportion=float(complexity) | |
28 self.nombre=int(256*self.proportion)*4 #Par multiple de 4 (256=1024/4) | |
29 return self._get_current_parameters() | |
30 | |
31 def _get_current_parameters(self): | |
32 return [] | |
33 | |
34 def get_parameters_determined_by_complexity(self, complexity): | |
35 return [int(complexity*256)*4] | |
36 | |
37 def transform_image(self, image): | |
38 image=image.reshape(1024,1) | |
39 temp=0 #variable temporaire | |
40 #constitution de l'echantillon | |
41 echantillon=random.sample(xrange(0,1024),self.nombre) | |
42 for i in xrange(0,self.nombre,4): | |
43 #gauche | |
44 if echantillon[i] > 0: | |
45 temp=image[echantillon[i]-1] | |
46 image[echantillon[i]-1]=image[echantillon[i]] | |
47 image[echantillon[i]]=temp | |
48 #droite | |
49 if echantillon[i+1] < 1023: | |
50 temp=image[echantillon[i+1]+1] | |
51 image[echantillon[i+1]+1]=image[echantillon[i+1]] | |
52 image[echantillon[i+1]]=temp | |
53 #haut | |
54 if echantillon[i+2] > 31: | |
55 temp=image[echantillon[i+2]-32] | |
56 image[echantillon[i+2]-32]=image[echantillon[i+2]] | |
57 image[echantillon[i+2]]=temp | |
58 #bas | |
59 if echantillon[i+3] < 992: | |
60 temp=image[echantillon[i+3]+32] | |
61 image[echantillon[i+3]+32]=image[echantillon[i+3]] | |
62 image[echantillon[i+3]]=temp | |
63 | |
64 | |
65 return image.reshape((32,32)) | |
66 | |
67 | |
68 #---TESTS--- | |
69 | |
70 def _load_image(): | |
71 f = open('/home/sylvain/Dropbox/Msc/IFT6266/donnees/lower_test_data.ft') #Le jeu de donnees est en local. | |
72 d = ft.read(f) | |
73 w=numpy.asarray(d[random.randint(0,100)]) | |
74 return (w/255.0).astype('float') | |
75 | |
76 def _test(complexite): | |
77 img=_load_image() | |
78 transfo = PermutPixel() | |
79 pylab.imshow(img.reshape((32,32))) | |
80 pylab.show() | |
81 print transfo.get_settings_names() | |
82 print transfo.regenerate_parameters(complexite) | |
83 | |
84 img_trans=transfo.transform_image(img) | |
85 | |
86 pylab.imshow(img_trans.reshape((32,32))) | |
87 pylab.show() | |
88 | |
89 | |
90 if __name__ == '__main__': | |
91 from pylearn.io import filetensor as ft | |
92 import pylab | |
93 for i in xrange(0,5): | |
94 _test(0.5) | |
95 | |
96 |