annotate transformations/local_elastic_distortions.py @ 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
author fsavard <francois.savard@polymtl.ca>
date Fri, 29 Jan 2010 13:37:52 -0500
parents 8d1c37190122
children b67d729ebfe3
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
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
26 _TEST_DIR = "/home/francois/Desktop/dist_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
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():
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
32 def __init__(self, image_size, alpha=0.0, sigma=0.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
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():
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
62 def __init__(self, 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
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
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
65 self.current_complexity = 0.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
66
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 # 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
68 # (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
69 # 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
70 # 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
71 # randomization, but we're much faster).
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 self.to_precompute = 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
73
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 # 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
75 self.current_params = 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
76 self.precomputed_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
77
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 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
80 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
81
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 # 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
83 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
84
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
85 def get_settings_names(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
86 return ['alpha', '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
87
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 def regenerate_parameters(self, 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
89 if abs(complexity - self.current_complexity) > 1e-4:
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
90 self.current_complexity = 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
91
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
92 # complexity changed, fields must be regenerated
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
93 self.precomputed_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
94
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
95 if len(self.precomputed_params) <= self.to_precompute:
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
96 # 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
97 # 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
98 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
99 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
100 self.current_params = 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
101 self.precomputed_params.append(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
102 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
103 # 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
104 # 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
105 # when the field was generated
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 idx = numpy.random.randint(0, len(self.precomputed_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 = self.precomputed_params[idx]
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
108
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 return self.current_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
110
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
111 # 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
112 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
113 # 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
114 # 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
115 # 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
116 # 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
117 # 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
118 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
119 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
120 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
121 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
122 # 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
123 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
124 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
125 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
126 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
127 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
128 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
129 # 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
130 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
131
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 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
133 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
134
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 # 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
136 # 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
137 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
138 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
139 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
140 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
141 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
142 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
143
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
144 # 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
145 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
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 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
148
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
149
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
150 def _initialize_new_params(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
151 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
152
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 cpx = self.current_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
154 # 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
155 # tout en gardant les extrêmes de 0.0 et 1.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
156 cpx = cpx ** (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
157
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
158 # 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
159 # a max of 10 is reasonable
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 params.alpha = cpx * 10.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
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 # 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
163 # 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
164 # 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
165 # slower the field generation, btw.
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
166 params.sigma = 10.0 - (7.0 * cpx)
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
167
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
168 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
169
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 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
171 '''
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
172 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
173 - 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
174 - 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
175 - 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
176 - 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
177 - 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
178 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
179 (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
180 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
181 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
182 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
183 - 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
184 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
185 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
186 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
187 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
188 - 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
189 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
190 '''
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
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
192 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
193
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
194 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
195 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
196 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
197
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
198 #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
199 #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
200
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 # 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
202 # "_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
203 # "_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
204 # (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
205 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
206
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 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
208 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
209 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
210 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
211
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 # 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
213 # (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
214 # 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
215 # 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
216 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
217
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
218 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
219 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
220
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
221 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
222 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
223 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
224 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
225
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 # 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
227 # 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
228 # (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
229 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
230 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
231
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
232 # 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
233 # 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
234
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
235 # 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
236 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
237 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
238 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
239 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
240 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
241 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
242 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
243 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
244
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
245 # 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
246 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
247 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
248 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
249 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
250 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
251 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
252 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
253 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
254
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
255 # 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
256 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
257 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
258 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
259 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
260 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
261 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
262 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
263 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
264
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
265 # 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
266 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
267 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
268 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
269 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
270 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
271 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
272 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
273 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
274
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 # 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
276 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
277
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 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
279 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
280
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 # 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
282 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
283 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
284 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
285 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
286
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 # 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
288 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
289 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
290 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
291 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
292
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
293 # sum to finish bilinear combination
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 return numpy.sum([tl_pixels,tr_pixels,bl_pixels,br_pixels], axis=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
295
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
296 # 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
297
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 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
299 _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
300 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
301 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
302 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
303 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
304 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
305
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
306 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
307 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
308 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
309 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
310 print dist.regenerate_parameters(0.5)
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
311 img = dist.distort_image(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
312 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
313 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
314
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
315 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
316 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
317 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
318 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
319 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
320 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
321 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
322 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
323 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
324 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
325 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
326 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
327 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
328 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
329 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
330 _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
331 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
332 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
333 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
334 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
335 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
336
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 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
338 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
339 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
340 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
341
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
342 # time the first 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
343 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
344 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
345 dist.regenerate_parameters(0.2)
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 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
347 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
348
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 print "first 10, total = ", t2-t1, ", avg=", (t2-t1)/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
350
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 # time the next 40
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 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
353 for i in range(40):
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 dist.regenerate_parameters(0.2)
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 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
356 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
357
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 print "next 40, total = ", t2-t1, ", avg=", (t2-t1)/40
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
359
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
360 # time the next 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
361 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
362 for i in range(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
363 dist.regenerate_parameters(0.2)
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
364 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
365 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
366
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 print "next 50, total = ", t2-t1, ", avg=", (t2-t1)/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
368
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
369 # time the next 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
370 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
371 for i in range(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
372 dist.regenerate_parameters(0.2)
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
373 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
374 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
375
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
376 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
377
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
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
379
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
380 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
381 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
382 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
383
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
384 # 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
385 '''
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
386 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
387 #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
388 #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
389 #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
390
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
391 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
392 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
393 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
394 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
395 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
396 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
397 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
398 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
399 '''
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
400
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
401 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
402 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
403 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
404 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
405 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
406 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
407 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
408 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
409 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
410 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
411 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
412 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
413 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
414
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 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
416 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
417 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
418 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
419 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
420 #_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
421 #_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
422 #_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
423 #_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
424 _complexity_benchmark()
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
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