forked from scottpurdy/nupic.vision
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_encoders.py
50 lines (40 loc) · 1.65 KB
/
image_encoders.py
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
# ----------------------------------------------------------------------
# Numenta Platform for Intelligent Computing (NuPIC)
# Copyright (C) 2015, Numenta, Inc. Unless you have purchased from
# Numenta, Inc. a separate commercial license for this software code, the
# following terms and conditions apply:
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses.
#
# http://numenta.org/licenses/
# ----------------------------------------------------------------------
"""
These routines convert images to bit vectors that can be used as input to
the spatial pooler.
"""
import numpy
from PIL import Image
def imageToVector(image):
'''
Returns a bit vector representation (list of ints) of a PIL image.
'''
# Convert the image to black and white
image = image.convert('1',dither=Image.NONE)
# Pull out the data, turn that into a list, then a numpy array,
# then convert from 0 255 space to binary with a threshold.
# Finally cast the values into a type CPP likes
vector = (numpy.array(list(image.getdata())) < 100).astype('uint32')
return vector
def imagesToVectors(images):
vectors = [imageToVector(image) for image in images]
return vectors