annotate data_generation/transformations/local_elastic_distortions.py @ 346:7bc555cc9aab

Ajouté dans set_batches : choix de la classe principale
author Guillaume Sicard <guitch21@gmail.com>
date Mon, 19 Apr 2010 07:09:44 -0400
parents 1f5937e9e530
children
rev   line source
5
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
1 #!/usr/bin/python
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
2 # coding: utf-8
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
3
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
4 '''
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
5 Implementation of elastic distortions as described in
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
6 Simard, Steinkraus, Platt, "Best Practices for Convolutional
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
7 Neural Networks Applied to Visual Document Analysis", 2003
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
8
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
9 Author: François Savard
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
10 Date: Fall 2009, revised Winter 2010
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
11
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
12 Usage: create the Distorter with proper alpha, sigma etc.
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
13 Then each time you want to change the distortion field applied,
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
14 call regenerate_field().
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
15
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
16 (The point behind this is that regeneration takes some time,
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
17 so we better reuse the fields a few times)
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
18 '''
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
19
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
20 import sys
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
21 import math
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
22 import numpy
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
23 import numpy.random
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
24 import scipy.signal # convolve2d
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
25
52
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
26 _TEST_DIR = "/u/savardf/ift6266/debug_images/"
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
27
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
28 def _raw_zeros(size):
5
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
29 return [[0 for i in range(size[1])] for j in range(size[0])]
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
30
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
31 class ElasticDistortionParams():
30
a8ac3402eb45 Correction aux distortions élastiques (retourne float32) suite à tests avec testmod.py
fsavard <francois.savard@polymtl.ca>
parents: 29
diff changeset
32 def __init__(self, image_size=(32,32), alpha=0.0, sigma=0.0):
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
33 self.image_size = image_size
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
34 self.alpha = alpha
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
35 self.sigma = sigma
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
36
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
37 h,w = self.image_size
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
38
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
39 self.matrix_tl_corners_rows = _raw_zeros((h,w))
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
40 self.matrix_tl_corners_cols = _raw_zeros((h,w))
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
41
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
42 self.matrix_tr_corners_rows = _raw_zeros((h,w))
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
43 self.matrix_tr_corners_cols = _raw_zeros((h,w))
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
44
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
45 self.matrix_bl_corners_rows = _raw_zeros((h,w))
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
46 self.matrix_bl_corners_cols = _raw_zeros((h,w))
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
47
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
48 self.matrix_br_corners_rows = _raw_zeros((h,w))
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
49 self.matrix_br_corners_cols = _raw_zeros((h,w))
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
50
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
51 # those will hold the precomputed ratios for
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
52 # bilinear interpolation
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
53 self.matrix_tl_multiply = numpy.zeros((h,w))
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
54 self.matrix_tr_multiply = numpy.zeros((h,w))
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
55 self.matrix_bl_multiply = numpy.zeros((h,w))
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
56 self.matrix_br_multiply = numpy.zeros((h,w))
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
57
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
58 def alpha_sigma(self):
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
59 return [self.alpha, self.sigma]
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
60
5
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
61 class LocalElasticDistorter():
30
a8ac3402eb45 Correction aux distortions élastiques (retourne float32) suite à tests avec testmod.py
fsavard <francois.savard@polymtl.ca>
parents: 29
diff changeset
62 def __init__(self, image_size=(32,32)):
5
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
63 self.image_size = image_size
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
64
52
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
65 self.current_complexity_10 = 0
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
66 self.current_complexity = 0
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
67
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
68 # number of precomputed fields
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
69 # (principle: as complexity doesn't change often, we can
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
70 # precompute a certain number of fields for a given complexity,
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
71 # each with its own parameters. That way, we have good
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
72 # randomization, but we're much faster).
52
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
73 self.to_precompute_per_complexity = 50
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
74
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
75 # Both use ElasticDistortionParams
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
76 self.current_params = None
52
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
77 self.precomputed_params = [[] for i in range(10)]
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
78
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
79 #
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
80 self.kernel_size = None
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
81 self.kernel = None
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
82
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
83 # set some defaults
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
84 self.regenerate_parameters(0.0)
5
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
85
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
86 def get_settings_names(self):
86
b3d76ebf2fac Modifs pour interface parameters determined by complexity
fsavard <francois.savard@polymtl.ca>
parents: 52
diff changeset
87 return []
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
88
52
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
89 def _floor_complexity(self, complexity):
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
90 return self._to_complexity_10(complexity) / 10.0
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
91
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
92 def _to_complexity_10(self, complexity):
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
93 return min(9, max(0, int(complexity * 10)))
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
94
52
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
95 def regenerate_parameters(self, complexity):
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
96 complexity_10 = self._to_complexity_10(complexity)
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
97
52
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
98 if complexity_10 != self.current_complexity_10:
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
99 self.current_complexity_10 = complexity_10
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
100 self.current_complexity = self._floor_complexity(complexity)
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
101
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
102 if len(self.precomputed_params[complexity_10]) <= self.to_precompute_per_complexity:
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
103 # not yet enough params generated, produce one more
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
104 # and append to list
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
105 new_params = self._initialize_new_params()
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
106 new_params = self._generate_fields(new_params)
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
107 self.current_params = new_params
52
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
108 self.precomputed_params[complexity_10].append(new_params)
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
109 else:
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
110 # if we have enough precomputed fields, just select one
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
111 # at random and set parameters to match what they were
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
112 # when the field was generated
52
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
113 idx = numpy.random.randint(0, len(self.precomputed_params[complexity_10]))
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
114 self.current_params = self.precomputed_params[complexity_10][idx]
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
115
29
b67d729ebfe3 Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents: 24
diff changeset
116 # don't return anything, to avoid storing deterministic parameters
b67d729ebfe3 Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents: 24
diff changeset
117 return [] # self.current_params.alpha_sigma()
b67d729ebfe3 Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents: 24
diff changeset
118
b67d729ebfe3 Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents: 24
diff changeset
119 def get_parameters_determined_by_complexity(self, complexity):
52
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
120 tmp_params = self._initialize_new_params(_floor_complexity(complexity))
29
b67d729ebfe3 Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents: 24
diff changeset
121 return tmp_params.alpha_sigma()
5
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
122
86
b3d76ebf2fac Modifs pour interface parameters determined by complexity
fsavard <francois.savard@polymtl.ca>
parents: 52
diff changeset
123 def get_settings_names_determined_by_complexity(self, complexity):
b3d76ebf2fac Modifs pour interface parameters determined by complexity
fsavard <francois.savard@polymtl.ca>
parents: 52
diff changeset
124 return ['alpha', 'sigma']
b3d76ebf2fac Modifs pour interface parameters determined by complexity
fsavard <francois.savard@polymtl.ca>
parents: 52
diff changeset
125
5
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
126 # adapted from http://blenderartists.org/forum/showthread.php?t=163361
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
127 def _gen_gaussian_kernel(self, sigma):
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
128 # the kernel size can change DRAMATICALLY the time
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
129 # for the blur operation... so even though results are better
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
130 # with a bigger kernel, we need to compromise here
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
131 # 1*s is very different from 2*s, but there's not much difference
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
132 # between 2*s and 4*s
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
133 ks = self.kernel_size
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
134 s = sigma
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
135 target_ks = (1.5*s, 1.5*s)
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
136 if not ks is None and ks[0] == target_ks[0] and ks[1] == target_ks[1]:
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
137 # kernel size is good, ok, no need to regenerate
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
138 return
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
139 self.kernel_size = target_ks
5
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
140 h,w = self.kernel_size
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
141 a,b = h/2.0, w/2.0
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
142 y,x = numpy.ogrid[0:w, 0:h]
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
143 gauss = numpy.exp(-numpy.square((x-a)/s))*numpy.exp(-numpy.square((y-b)/s))
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
144 # Normalize so we don't reduce image intensity
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
145 self.kernel = gauss/gauss.sum()
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
146
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
147 def _gen_distortion_field(self, params):
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
148 self._gen_gaussian_kernel(params.sigma)
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
149
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
150 # we add kernel_size on all four sides so blurring
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
151 # with the kernel produces a smoother result on borders
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
152 ks0 = self.kernel_size[0]
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
153 ks1 = self.kernel_size[1]
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
154 sz0 = self.image_size[1] + ks0
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
155 sz1 = self.image_size[0] + ks1
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
156 field = numpy.random.uniform(-1.0, 1.0, (sz0, sz1))
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
157 field = scipy.signal.convolve2d(field, self.kernel, mode='same')
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
158
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
159 # crop only image_size in the middle
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
160 field = field[ks0:ks0+self.image_size[0], ks1:ks1+self.image_size[1]]
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
161
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
162 return params.alpha * field
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
163
5
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
164
29
b67d729ebfe3 Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents: 24
diff changeset
165 def _initialize_new_params(self, complexity=None):
b67d729ebfe3 Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents: 24
diff changeset
166 if not complexity:
b67d729ebfe3 Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents: 24
diff changeset
167 complexity = self.current_complexity
b67d729ebfe3 Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents: 24
diff changeset
168
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
169 params = ElasticDistortionParams(self.image_size)
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
170
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
171 # pour faire progresser la complexité un peu plus vite
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
172 # tout en gardant les extrêmes de 0.0 et 1.0
29
b67d729ebfe3 Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents: 24
diff changeset
173 complexity = complexity ** (1./3.)
5
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
174
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
175 # the smaller the alpha, the closest the pixels are fetched
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
176 # a max of 10 is reasonable
29
b67d729ebfe3 Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents: 24
diff changeset
177 params.alpha = complexity * 10.0
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
178
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
179 # the bigger the sigma, the smoother is the distortion
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
180 # max of 1 is "reasonable", but produces VERY noisy results
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
181 # And the bigger the sigma, the bigger the blur kernel, and the
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
182 # slower the field generation, btw.
29
b67d729ebfe3 Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents: 24
diff changeset
183 params.sigma = 10.0 - (7.0 * complexity)
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
184
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
185 return params
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
186
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
187 def _generate_fields(self, params):
5
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
188 '''
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
189 Here's how the code works:
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
190 - We first generate "distortion fields" for x and y with these steps:
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
191 - Uniform noise over [-1, 1] in a matrix of size (h,w)
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
192 - Blur with a Gaussian kernel of spread sigma
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
193 - Multiply by alpha
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
194 - Then (conceptually) to compose the distorted image, we loop over each pixel
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
195 of the new image and use the corresponding x and y distortions
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
196 (from the matrices generated above) to identify pixels
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
197 of the old image from which we fetch color data. As the
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
198 coordinates are not integer, we interpolate between the
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
199 4 nearby pixels (top left, top right etc.).
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
200 - That's just conceptually. Here I'm using matrix operations
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
201 to speed up the computation. I first identify the 4 nearby
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
202 pixels in the old image for each pixel in the distorted image.
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
203 I can then use them as "fancy indices" to extract the proper
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
204 pixels for each new pixel.
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
205 - Then I multiply those extracted nearby points by precomputed
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
206 ratios for the bilinear interpolation.
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
207 '''
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
208
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
209 p = params
5
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
210
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
211 dist_fields = [None, None]
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
212 dist_fields[0] = self._gen_distortion_field(params)
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
213 dist_fields[1] = self._gen_distortion_field(params)
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
214
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
215 #pylab.imshow(dist_fields[0])
5
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
216 #pylab.show()
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
217
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
218 # regenerate distortion index matrices
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
219 # "_rows" are row indices
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
220 # "_cols" are column indices
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
221 # (separated due to the way fancy indexing works in numpy)
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
222 h,w = p.image_size
5
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
223
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
224 for y in range(h):
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
225 for x in range(w):
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
226 distort_x = dist_fields[0][y,x]
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
227 distort_y = dist_fields[1][y,x]
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
228
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
229 # the "target" is the coordinate we fetch color data from
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
230 # (in the original image)
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
231 # target_left and _top are the rounded coordinate on the
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
232 # left/top of this target (float) coordinate
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
233 target_pixel = (y+distort_y, x+distort_x)
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
234
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
235 target_left = int(math.floor(x + distort_x))
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
236 target_top = int(math.floor(y + distort_y))
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
237
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
238 index_tl = [target_top, target_left]
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
239 index_tr = [target_top, target_left+1]
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
240 index_bl = [target_top+1, target_left]
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
241 index_br = [target_top+1, target_left+1]
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
242
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
243 # x_ratio is the ratio of importance of left pixels
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
244 # y_ratio is the """" of top pixels
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
245 # (in bilinear combination)
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
246 y_ratio = 1.0 - (target_pixel[0] - target_top)
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
247 x_ratio = 1.0 - (target_pixel[1] - target_left)
5
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
248
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
249 # We use a default background color of 0 for displacements
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
250 # outside of boundaries of the image.
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
251
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
252 # if top left outside bounds
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
253 if index_tl[0] < 0 or index_tl[0] >= h or index_tl[1] < 0 or index_tl[1] >= w:
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
254 p.matrix_tl_corners_rows[y][x] = 0
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
255 p.matrix_tl_corners_cols[y][x] = 0
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
256 p.matrix_tl_multiply[y,x] = 0
5
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
257 else:
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
258 p.matrix_tl_corners_rows[y][x] = index_tl[0]
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
259 p.matrix_tl_corners_cols[y][x] = index_tl[1]
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
260 p.matrix_tl_multiply[y,x] = x_ratio*y_ratio
5
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
261
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
262 # if top right outside bounds
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
263 if index_tr[0] < 0 or index_tr[0] >= h or index_tr[1] < 0 or index_tr[1] >= w:
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
264 p.matrix_tr_corners_rows[y][x] = 0
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
265 p.matrix_tr_corners_cols[y][x] = 0
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
266 p.matrix_tr_multiply[y,x] = 0
5
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
267 else:
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
268 p.matrix_tr_corners_rows[y][x] = index_tr[0]
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
269 p.matrix_tr_corners_cols[y][x] = index_tr[1]
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
270 p.matrix_tr_multiply[y,x] = (1.0-x_ratio)*y_ratio
5
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
271
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
272 # if bottom left outside bounds
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
273 if index_bl[0] < 0 or index_bl[0] >= h or index_bl[1] < 0 or index_bl[1] >= w:
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
274 p.matrix_bl_corners_rows[y][x] = 0
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
275 p.matrix_bl_corners_cols[y][x] = 0
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
276 p.matrix_bl_multiply[y,x] = 0
5
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
277 else:
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
278 p.matrix_bl_corners_rows[y][x] = index_bl[0]
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
279 p.matrix_bl_corners_cols[y][x] = index_bl[1]
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
280 p.matrix_bl_multiply[y,x] = x_ratio*(1.0-y_ratio)
5
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
281
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
282 # if bottom right outside bounds
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
283 if index_br[0] < 0 or index_br[0] >= h or index_br[1] < 0 or index_br[1] >= w:
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
284 p.matrix_br_corners_rows[y][x] = 0
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
285 p.matrix_br_corners_cols[y][x] = 0
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
286 p.matrix_br_multiply[y,x] = 0
5
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
287 else:
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
288 p.matrix_br_corners_rows[y][x] = index_br[0]
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
289 p.matrix_br_corners_cols[y][x] = index_br[1]
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
290 p.matrix_br_multiply[y,x] = (1.0-x_ratio)*(1.0-y_ratio)
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
291
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
292 # not really necessary, but anyway
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
293 return p
5
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
294
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
295 def transform_image(self, image):
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
296 p = self.current_params
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
297
5
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
298 # index pixels to get the 4 corners for bilinear combination
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
299 tl_pixels = image[p.matrix_tl_corners_rows, p.matrix_tl_corners_cols]
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
300 tr_pixels = image[p.matrix_tr_corners_rows, p.matrix_tr_corners_cols]
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
301 bl_pixels = image[p.matrix_bl_corners_rows, p.matrix_bl_corners_cols]
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
302 br_pixels = image[p.matrix_br_corners_rows, p.matrix_br_corners_cols]
5
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
303
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
304 # bilinear ratios, elemwise multiply
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
305 tl_pixels = numpy.multiply(tl_pixels, p.matrix_tl_multiply)
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
306 tr_pixels = numpy.multiply(tr_pixels, p.matrix_tr_multiply)
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
307 bl_pixels = numpy.multiply(bl_pixels, p.matrix_bl_multiply)
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
308 br_pixels = numpy.multiply(br_pixels, p.matrix_br_multiply)
5
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
309
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
310 # sum to finish bilinear combination
30
a8ac3402eb45 Correction aux distortions élastiques (retourne float32) suite à tests avec testmod.py
fsavard <francois.savard@polymtl.ca>
parents: 29
diff changeset
311 return numpy.sum([tl_pixels,tr_pixels,bl_pixels,br_pixels], axis=0).astype(numpy.float32)
5
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
312
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
313 # TESTS ----------------------------------------------------------------------
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
314
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
315 def _load_image(filepath):
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
316 _RGB_TO_GRAYSCALE = [0.3, 0.59, 0.11, 0.0]
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
317 img = Image.open(filepath)
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
318 img = numpy.asarray(img)
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
319 if len(img.shape) > 2:
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
320 img = (img * _RGB_TO_GRAYSCALE).sum(axis=2)
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
321 return (img / 255.0).astype('float')
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
322
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
323 def _specific_test():
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
324 imgpath = os.path.join(_TEST_DIR, "d.png")
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
325 img = _load_image(imgpath)
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
326 dist = LocalElasticDistorter((32,32))
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
327 print dist.regenerate_parameters(0.5)
29
b67d729ebfe3 Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents: 24
diff changeset
328 img = dist.transform_image(img)
b67d729ebfe3 Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents: 24
diff changeset
329 print dist.get_parameters_determined_by_complexity(0.4)
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
330 pylab.imshow(img)
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
331 pylab.show()
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
332
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
333 def _complexity_tests():
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
334 imgpath = os.path.join(_TEST_DIR, "d.png")
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
335 dist = LocalElasticDistorter((32,32))
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
336 orig_img = _load_image(imgpath)
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
337 html_content = '''<html><body>Original:<br/><img src='d.png'>'''
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
338 for complexity in numpy.arange(0.0, 1.1, 0.1):
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
339 html_content += '<br/>Complexity: ' + str(complexity) + '<br/>'
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
340 for i in range(10):
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
341 t1 = time.time()
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
342 dist.regenerate_parameters(complexity)
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
343 t2 = time.time()
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
344 print "diff", t2-t1
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
345 img = dist.transform_image(orig_img)
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
346 filename = "complexity_" + str(complexity) + "_" + str(i) + ".png"
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
347 new_path = os.path.join(_TEST_DIR, filename)
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
348 _save_image(img, new_path)
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
349 html_content += '<img src="' + filename + '">'
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
350 html_content += "</body></html>"
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
351 html_file = open(os.path.join(_TEST_DIR, "complexity.html"), "w")
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
352 html_file.write(html_content)
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
353 html_file.close()
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
354
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
355 def _complexity_benchmark():
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
356 imgpath = os.path.join(_TEST_DIR, "d.png")
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
357 dist = LocalElasticDistorter((32,32))
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
358 orig_img = _load_image(imgpath)
5
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
359
52
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
360 for cpx in (0.21, 0.35):
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
361 # time the first 10
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
362 t1 = time.time()
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
363 for i in range(10):
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
364 dist.regenerate_parameters(cpx)
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
365 img = dist.transform_image(orig_img)
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
366 t2 = time.time()
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
367
52
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
368 print "first 10, total = ", t2-t1, ", avg=", (t2-t1)/10
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
369
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
370 # time the next 40
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
371 t1 = time.time()
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
372 for i in range(40):
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
373 dist.regenerate_parameters(cpx)
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
374 img = dist.transform_image(orig_img)
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
375 t2 = time.time()
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
376
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
377 print "next 40, total = ", t2-t1, ", avg=", (t2-t1)/40
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
378
52
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
379 # time the next 50
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
380 t1 = time.time()
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
381 for i in range(50):
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
382 dist.regenerate_parameters(cpx)
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
383 img = dist.transform_image(orig_img)
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
384 t2 = time.time()
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
385
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
386 print "next 50, total = ", t2-t1, ", avg=", (t2-t1)/50
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
387
52
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
388 # time the next 1000
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
389 t1 = time.time()
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
390 for i in range(1000):
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
391 dist.regenerate_parameters(cpx)
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
392 img = dist.transform_image(orig_img)
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
393 t2 = time.time()
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
394
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
395 print "next 1000, total = ", t2-t1, ", avg=", (t2-t1)/1000
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
396
52
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
397 # time the next 1000 with old complexity
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
398 t1 = time.time()
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
399 for i in range(1000):
52
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
400 dist.regenerate_parameters(0.21)
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
401 img = dist.transform_image(orig_img)
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
402 t2 = time.time()
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
403
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
404 print "next 1000, total = ", t2-t1, ", avg=", (t2-t1)/1000
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
405
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
406
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
407
52
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
408
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
409 def _save_image(img, path):
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
410 img2 = Image.fromarray((img * 255).astype('uint8'), "L")
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
411 img2.save(path)
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
412
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
413 # TODO: reformat to follow new class... it function of complexity now
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
414 '''
5
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
415 def _distorter_tests():
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
416 #import pylab
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
417 #pylab.imshow(img)
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
418 #pylab.show()
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
419
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
420 for letter in ("d", "a", "n", "o"):
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
421 img = _load_image("tests/" + letter + ".png")
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
422 for alpha in (1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0):
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
423 for sigma in (1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0):
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
424 id = LocalElasticDistorter((32,32))
5
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
425 img2 = id.distort_image(img)
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
426 img2 = Image.fromarray((img2 * 255).astype('uint8'), "L")
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
427 img2.save("tests/"+letter+"_alpha"+str(alpha)+"_sigma"+str(sigma)+".png")
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
428 '''
5
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
429
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
430 def _benchmark():
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
431 img = _load_image("tests/d.png")
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
432 dist = LocalElasticDistorter((32,32))
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
433 dist.regenerate_parameters(0.0)
5
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
434 import time
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
435 t1 = time.time()
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
436 for i in range(10000):
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
437 if i % 1000 == 0:
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
438 print "-"
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
439 dist.distort_image(img)
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
440 t2 = time.time()
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
441 print "t2-t1", t2-t1
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
442 print "avg", 10000/(t2-t1)
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
443
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
444 if __name__ == '__main__':
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
445 import time
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
446 import pylab
5
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
447 import Image
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
448 import os.path
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
449 #_distorter_tests()
5
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
450 #_benchmark()
52
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
451 #_specific_test()
24
010e826b41e8 Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents: 5
diff changeset
452 #_complexity_tests()
52
c89defea1e65 Modification aux déformations élastiques pour mettre en cache 50x10 champs de déformation, selon 10 niveaux de complexité
fsavard
parents: 30
diff changeset
453 _complexity_benchmark()
29
b67d729ebfe3 Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents: 24
diff changeset
454
5
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
455
8d1c37190122 Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff changeset
456