Mercurial > ift6266
annotate transformations/add_background_image.py @ 58:fd02fd7e6557
Contraste de rature plus prononce, il y a maintenant rature avec proba=complexity, la clarte et la largeur de la bande ont maintenant une composante aleatoire
author | SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca> |
---|---|
date | Mon, 08 Feb 2010 13:57:46 -0500 |
parents | 4d4248f7e2fb |
children | ab70fbca513c |
rev | line source |
---|---|
8
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
1 #!/usr/bin/python |
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
2 # -*- coding: iso-8859-1 -*- |
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
3 |
32
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
4 ''' |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
5 Implementation of random background adding to a specific image |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
6 |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
7 Author: Guillaume Sicard |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
8 ''' |
8
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
9 |
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
10 import sys, os, random, fnmatch |
32
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
11 import Image, numpy |
8
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
12 |
32
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
13 class AddBackground(): |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
14 def __init__(self, threshold = 128): |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
15 self.h = 32 |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
16 self.w = 32 |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
17 self.threshold = threshold; |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
18 self.bg_image_dir = './images/' |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
19 self.pattern = '*.jpg' |
8
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
20 |
32
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
21 # get threshold value |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
22 def get_settings_names(self): |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
23 return [str(self.threshold)] |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
24 |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
25 # no need, except for testmod.py |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
26 def regenerate_parameters(self, complexity): |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
27 value = random.gauss(0, 0.5*complexity) |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
28 return [value] |
8
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
29 |
32
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
30 # load an image |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
31 def load_image(self,filename): |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
32 image = Image.open(filename).convert('L') |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
33 image = numpy.asarray(image) |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
34 image = (image / 255.0).astype(numpy.float32) |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
35 return image |
8
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
36 |
32
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
37 # save an image |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
38 def save_image(self,array, filename): |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
39 image = (array * 255.0).astype('int') |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
40 image = Image.fromarray(image) |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
41 if (filename != ''): |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
42 image.save(filename) |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
43 else: |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
44 image.show() |
8
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
45 |
32
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
46 # make a random 32x32 crop of an image |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
47 def rand_crop(self,image): |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
48 i_w, i_h = image.shape |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
49 x, y = random.randint(0, i_w - self.w), random.randint(0, i_h - self.h) |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
50 return image[x:x + self.w, y:y + self.h] |
8
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
51 |
32
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
52 # select a random background image from "bg_image_dir" and crops it |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
53 def rand_bg_image(self): |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
54 files = os.listdir(self.bg_image_dir) |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
55 image_files = fnmatch.filter(files, self.pattern) |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
56 i = random.randint(0, len(image_files) - 1) |
8
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
57 |
32
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
58 image = self.load_image(self.bg_image_dir + image_files[i]) |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
59 self.bg_image = self.rand_crop(image) |
8
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
60 |
32
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
61 # set "bg_image" as background to "image", based on a pixels threshold |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
62 def set_bg(self,image): |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
63 b = (image < self.threshold / 255.0).astype(numpy.float32) |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
64 return b * self.bg_image + ( 1 - b) * image |
8
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
65 |
32
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
66 # transform an image file and return an array |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
67 def transform_image_from_file(self, filename): |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
68 self.rand_bg_image() |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
69 image = self.load_image(filename) |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
70 image = self.set_bg(image) |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
71 return image |
8
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
72 |
32
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
73 # standard array to array transform |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
74 def transform_image(self, image): |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
75 self.rand_bg_image() |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
76 image = self.set_bg(image) |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
77 return image |
8
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
78 |
32
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
79 # test method |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
80 def test(self,filename): |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
81 import time |
8
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
82 |
32
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
83 sys.stdout.write('Starting addBackground test : loading image') |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
84 sys.stdout.flush() |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
85 |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
86 image = self.load_image(filename) |
8
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
87 |
32
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
88 t = 0 |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
89 n = 500 |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
90 for i in range(n): |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
91 t0 = time.time() |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
92 image2 = self.transform_image(image) |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
93 t = ( i * t + (time.time() - t0) ) / (i + 1) |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
94 sys.stdout.write('.') |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
95 sys.stdout.flush() |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
96 |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
97 print "Done!\nAverage time : " + str(1000 * t) + " ms" |
8
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
98 |
32
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
99 if __name__ == '__main__': |
8
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
100 |
32
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
101 myAddBackground = AddBackground() |
4d4248f7e2fb
Modified background adding class for compliance with testmod.py
Guillaume Sicard <guitch21@gmail.com>
parents:
9
diff
changeset
|
102 myAddBackground.test('./images/0-LiberationSans-Italic.ttf.jpg') |