Menu

[r6030]: / trunk / py4science / examples / faces / imtools.py  Maximize  Restore  History

Download this file

187 lines (145 with data), 6.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
"""Image handling utilities for the SVD-based image matching library."""
import os
import numpy as N
import scipy as S
import pylab as P
def imshow2(m1,m2,labels=(None,None)):
"""Display two images side by side.
Returns the created figure instance."""
fig = P.figure()
ax1 = [0.025,0.1,0.45,0.775]
ax2 = [0.525,0.1,0.45,0.775]
for m,ax_coord,label in [(m1,ax1,labels[0]),(m2,ax2,labels[1])]:
ax = fig.add_axes(ax_coord)
ax.imshow(m,cmap=P.cm.gray)
if label:
ax.set_xlabel(label)
P.xticks([],[])
P.yticks([],[])
P.draw_if_interactive()
return fig
class ImageCollection(object):
"""Class to hold a collection of image files stored in a directory"""
def __init__(self,images,names=None):
"""Construct a collection from a list of images and names.
Inputs:
- images: a sequence of valid image objects.
Optional inputs:
- names: a sequence of strings to be used as names for the images.
If not given, the images are simply named by their index."""
num_images = len(images)
if names is None:
names = map(str,range(num_images))
assert (len(names) == num_images,
'List of names must be of same length as image sequence')
self.images = images
self.names = names
# Assume all images have the same shape.
self.im_shape = images[0].shape
# make a dict for keyed access
self.img_dict = dict(zip(names,images))
self.num_images = num_images
# Public attributes
self.interpolation = 'nearest'
self.images
def __getitem__(self, n):
"""Return image number n."""
if n > self.num_images-1:
return 0
else:
return self.images[n]
@staticmethod
def from_directory(dir_name,verbose=False):
"""Read all images in a given directory, return an ImageCollection.
It reads all files in the directory and tries to compute an image
(using scipy's imread) for all of them and stores a list of
(filename,array) pairs.
Inputs:
- dir_name: a string containing the directory name to scan.
Optional inputs:
- verbose(False): print extra verbose information about skipped
files while running. Set to any True value for basic diagnostics of
error conditions, and to higher integer values for further messages.
Outputs:
- an ImageCollection instance.
"""
img = []
names = []
imread = S.misc.pilutil.imread
for fname in os.listdir(dir_name):
full_fname = os.path.join(dir_name,fname)
try:
if verbose > 1:
print 'Reading file:',fname
img.append(imread(full_fname))
names.append(fname)
except IOError:
if verbose:
print 'Skipping non-image file:',fname
# Safety warning
if not img:
print 'WARNING: empty image collection, no valid images found.'
return ImageCollection(img,names)
def show_image(self,number,fignum=None,interpolation=None):
"""Show a single image, by its index.
Inputs:
- number: index (0-offset) of the image to display.
Optional inputs:
- fignum(None): a matplotlib figure number, to reuse for display.
If not given, a new figure is automatically created.
- interpolation: interpolation mode for display. If not given, the
instance .interpolation attribute is used.
Outputs:
The number of the created figure window, so it can be reused."""
if interpolation is None:
interpolation = self.interpolation
image = self.images[number]
name = self.names[number]
if fignum is None:
# make a new figure from scratch
fig = P.matshow(image,cmap=P.cm.gray,interpolation=interpolation)
else:
# draw into an existing figure directly
fig = P.figure(fignum)
ax_im = fig.axes[0].images[0]
ax_im.set_data(image)
P.draw()
P.title('Image [%d]: %s - (%d/%d)' %
(number,name,number+1,self.num_images))
P.draw_if_interactive()
return fig.number
def browse_images(self):
"""Browse a set of image files"""
# Show the first figure separately, so we can reuse the figure window
fignum = self.show_image(0)
count = 0
while count < self.num_images:
self.show_image(count,fignum)
ans = raw_input('Enter for next, <p> for previous, <q> to quit: ')
if ans=='p':
if count>0:
count -= 1
elif ans=='q':
break
else:
count += 1
def list_images(self):
"""Print a listing of all images in the collection"""
print 'Total number of images:',self.num_images
for i,name in enumerate(self.names):
print '%3d - %s' % (i,name)
def flat(self):
"""Return an array which contains each image 'flattened' as a
column vector."""
# Make a single matrix where we'll store the flattened version of all
# the images for further processing. We pick up the image dimensions
# from the first one without checking they all have the same
# dimensions, we can validate this more strictly later.
(imheight,imwidth) = self.images[0].shape
shape = (imheight*imwidth,self.num_images)
# The flat image has to be in a floating-point type so we can do SVD
# and similar things with it
img_flat = N.empty(shape,N.float32)
for col,im in enumerate(self.images):
img_flat[:,col] = im.flat
return img_flat
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.