Mercurial > ift6266
annotate transformations/local_elastic_distortions.py @ 37:5848e88f7a7a
Ajout de rature verticale, horizontale ou blique sur l'image. Compatible avec testmod.py
author | SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca> |
---|---|
date | Tue, 02 Feb 2010 10:44:54 -0500 |
parents | a8ac3402eb45 |
children | c89defea1e65 |
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(): |
30
a8ac3402eb45
Correction aux distortions élastiques (retourne float32) suite à tests avec testmod.py
fsavard <francois.savard@polymtl.ca>
parents:
29
diff
changeset
|
32 def __init__(self, image_size=(32,32), alpha=0.0, sigma=0.0): |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
33 self.image_size = image_size |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
34 self.alpha = alpha |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
35 self.sigma = sigma |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
36 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
37 h,w = self.image_size |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
38 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
39 self.matrix_tl_corners_rows = _raw_zeros((h,w)) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
40 self.matrix_tl_corners_cols = _raw_zeros((h,w)) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
41 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
42 self.matrix_tr_corners_rows = _raw_zeros((h,w)) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
43 self.matrix_tr_corners_cols = _raw_zeros((h,w)) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
44 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
45 self.matrix_bl_corners_rows = _raw_zeros((h,w)) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
46 self.matrix_bl_corners_cols = _raw_zeros((h,w)) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
47 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
48 self.matrix_br_corners_rows = _raw_zeros((h,w)) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
49 self.matrix_br_corners_cols = _raw_zeros((h,w)) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
50 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
51 # those will hold the precomputed ratios for |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
52 # bilinear interpolation |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
53 self.matrix_tl_multiply = numpy.zeros((h,w)) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
54 self.matrix_tr_multiply = numpy.zeros((h,w)) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
55 self.matrix_bl_multiply = numpy.zeros((h,w)) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
56 self.matrix_br_multiply = numpy.zeros((h,w)) |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
57 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
58 def alpha_sigma(self): |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
59 return [self.alpha, self.sigma] |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
60 |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
61 class LocalElasticDistorter(): |
30
a8ac3402eb45
Correction aux distortions élastiques (retourne float32) suite à tests avec testmod.py
fsavard <francois.savard@polymtl.ca>
parents:
29
diff
changeset
|
62 def __init__(self, image_size=(32,32)): |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
63 self.image_size = image_size |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
64 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to 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 |
29
b67d729ebfe3
Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents:
24
diff
changeset
|
109 # don't return anything, to avoid storing deterministic parameters |
b67d729ebfe3
Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents:
24
diff
changeset
|
110 return [] # self.current_params.alpha_sigma() |
b67d729ebfe3
Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents:
24
diff
changeset
|
111 |
b67d729ebfe3
Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents:
24
diff
changeset
|
112 def get_parameters_determined_by_complexity(self, complexity): |
b67d729ebfe3
Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents:
24
diff
changeset
|
113 tmp_params = self._initialize_new_params(complexity) |
b67d729ebfe3
Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents:
24
diff
changeset
|
114 return tmp_params.alpha_sigma() |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
115 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
116 # 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
|
117 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
|
118 # 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
|
119 # 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
|
120 # 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
|
121 # 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
|
122 # 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
|
123 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
|
124 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
|
125 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
|
126 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
|
127 # 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
|
128 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
|
129 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
|
130 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
|
131 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
|
132 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
|
133 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
|
134 # 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
|
135 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
|
136 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to 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 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
|
138 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
|
139 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to 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 # 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
|
141 # 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
|
142 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
|
143 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
|
144 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
|
145 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
|
146 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
|
147 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
|
148 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
149 # 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
|
150 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
|
151 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to 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 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
|
153 |
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
|
154 |
29
b67d729ebfe3
Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents:
24
diff
changeset
|
155 def _initialize_new_params(self, complexity=None): |
b67d729ebfe3
Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents:
24
diff
changeset
|
156 if not complexity: |
b67d729ebfe3
Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents:
24
diff
changeset
|
157 complexity = self.current_complexity |
b67d729ebfe3
Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents:
24
diff
changeset
|
158 |
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
|
159 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
|
160 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to 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 # 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
|
162 # tout en gardant les extrêmes de 0.0 et 1.0 |
29
b67d729ebfe3
Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents:
24
diff
changeset
|
163 complexity = complexity ** (1./3.) |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
164 |
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
|
165 # 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
|
166 # a max of 10 is reasonable |
29
b67d729ebfe3
Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents:
24
diff
changeset
|
167 params.alpha = complexity * 10.0 |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
168 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to 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 # 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
|
170 # 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
|
171 # 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
|
172 # slower the field generation, btw. |
29
b67d729ebfe3
Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents:
24
diff
changeset
|
173 params.sigma = 10.0 - (7.0 * complexity) |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
174 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
175 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
|
176 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
177 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
|
178 ''' |
8d1c37190122
Ajouté code de 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 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
|
180 - 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
|
181 - 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
|
182 - 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
|
183 - 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
|
184 - 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
|
185 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
|
186 (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
|
187 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
|
188 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
|
189 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
|
190 - 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
|
191 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
|
192 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
|
193 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
|
194 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
|
195 - 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
|
196 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
|
197 ''' |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
198 |
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
|
199 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
|
200 |
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
|
201 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
|
202 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
|
203 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
|
204 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to 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 #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
|
206 #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
|
207 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
208 # 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
|
209 # "_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
|
210 # "_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
|
211 # (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
|
212 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
|
213 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
214 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
|
215 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
|
216 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
|
217 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
|
218 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to 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 # 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
|
220 # (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
|
221 # 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
|
222 # 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
|
223 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
|
224 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to 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 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
|
226 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
|
227 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to 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 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
|
229 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
|
230 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
|
231 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
|
232 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
233 # 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
|
234 # 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
|
235 # (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
|
236 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
|
237 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
|
238 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
239 # 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
|
240 # 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
|
241 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
242 # 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
|
243 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
|
244 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
|
245 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
|
246 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
|
247 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
|
248 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
|
249 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
|
250 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
|
251 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
252 # if top 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
|
253 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
|
254 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
|
255 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
|
256 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
|
257 else: |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
258 p.matrix_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
|
259 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
|
260 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
|
261 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
262 # if 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
|
263 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
|
264 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
|
265 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
|
266 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
|
267 else: |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
268 p.matrix_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
|
269 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
|
270 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
|
271 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
272 # if bottom 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
|
273 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
|
274 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
|
275 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
|
276 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
|
277 else: |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
278 p.matrix_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
|
279 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
|
280 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
|
281 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to 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 # 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
|
283 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
|
284 |
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
|
285 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
|
286 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
|
287 |
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
|
288 # 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
|
289 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
|
290 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
|
291 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
|
292 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
|
293 |
8d1c37190122
Ajouté code de 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 # 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
|
295 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
|
296 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
|
297 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
|
298 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
|
299 |
8d1c37190122
Ajouté code de 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 # sum to finish bilinear combination |
30
a8ac3402eb45
Correction aux distortions élastiques (retourne float32) suite à tests avec testmod.py
fsavard <francois.savard@polymtl.ca>
parents:
29
diff
changeset
|
301 return numpy.sum([tl_pixels,tr_pixels,bl_pixels,br_pixels], axis=0).astype(numpy.float32) |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
302 |
8d1c37190122
Ajouté code de 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 # 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
|
304 |
8d1c37190122
Ajouté code de 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 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
|
306 _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
|
307 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
|
308 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
|
309 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
|
310 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
|
311 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
|
312 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
313 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
|
314 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
|
315 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
|
316 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
|
317 print dist.regenerate_parameters(0.5) |
29
b67d729ebfe3
Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents:
24
diff
changeset
|
318 img = dist.transform_image(img) |
b67d729ebfe3
Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents:
24
diff
changeset
|
319 print dist.get_parameters_determined_by_complexity(0.4) |
24
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
320 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
|
321 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
|
322 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to 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 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
|
324 imgpath = os.path.join(_TEST_DIR, "d.png") |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
325 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
|
326 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
|
327 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
|
328 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
|
329 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
|
330 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
|
331 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
|
332 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
|
333 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
|
334 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
|
335 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
|
336 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
|
337 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
|
338 _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
|
339 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
|
340 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
|
341 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
|
342 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
|
343 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
|
344 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to 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 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
|
346 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
|
347 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
|
348 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
|
349 |
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
|
350 # 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
|
351 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
|
352 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
|
353 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
|
354 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
|
355 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
|
356 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to 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 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
|
358 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to 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 # 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
|
360 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
|
361 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
|
362 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
|
363 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
|
364 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
|
365 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to 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 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
|
367 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to 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 # 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
|
369 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
|
370 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
|
371 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
|
372 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
|
373 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
|
374 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to 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 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
|
376 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to 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 # 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
|
378 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
|
379 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
|
380 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
|
381 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
|
382 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
|
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 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
|
385 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
386 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
387 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
388 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
|
389 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
|
390 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
|
391 |
010e826b41e8
Modifications to elastic distortions: fixed an important bug with distortions themselves (now result is much nicer visually), made interface to conform to Transformation standard, and added ability to save a certain amount of distortion fields to reuse them if complexity doesn't change
fsavard <francois.savard@polymtl.ca>
parents:
5
diff
changeset
|
392 # 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
|
393 ''' |
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
|
394 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
|
395 #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
|
396 #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
|
397 #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
|
398 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
399 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
|
400 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
|
401 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
|
402 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
|
403 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
|
404 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
|
405 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
|
406 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
|
407 ''' |
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
|
408 |
8d1c37190122
Ajouté code de 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 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
|
410 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
|
411 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
|
412 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
|
413 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
|
414 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
|
415 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
|
416 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
|
417 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
|
418 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
|
419 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
|
420 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
|
421 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
|
422 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
423 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
|
424 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
|
425 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
|
426 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
|
427 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
|
428 #_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
|
429 #_benchmark() |
29
b67d729ebfe3
Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents:
24
diff
changeset
|
430 _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
|
431 #_complexity_tests() |
29
b67d729ebfe3
Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents:
24
diff
changeset
|
432 #_complexity_benchmark() |
b67d729ebfe3
Adapted to avoid saving parameters completely determined by complexity
fsavard <francois.savard@polymtl.ca>
parents:
24
diff
changeset
|
433 |
5
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
434 |
8d1c37190122
Ajouté code de déformations élastiques locales, adapté depuis un travail que j'ai fait la session dernière
fsavard <francois.savard@polymtl.ca>
parents:
diff
changeset
|
435 |