Commit a1848059 authored by Richard Torenvliet's avatar Richard Torenvliet

Resolved conflicts.

parents c3c35032 9b5b2543
from pylab import imshow, imread, show
from scipy.misc import imresize
from matplotlib.pyplot import hist
from scipy.misc import imresize, imsave
class GrayscaleImage:
def __init__(self, image_path = None, data = None):
if image_path != None:
if image_path:
self.data = imread(image_path)
self.convert_to_grayscale()
elif data != None:
elif data:
self.data = data
def __iter__(self):
......@@ -30,7 +31,8 @@ class GrayscaleImage:
return self.data[position]
def convert_to_grayscale(self):
self.data = self.data.sum(axis=2) / 3
if len(self.data.shape) > 2:
self.data = self.data[:,:,:3].sum(axis=2) / 3
def crop(self, rectangle):
self.data = self.data[rectangle.y : rectangle.y + rectangle.height,
......@@ -46,19 +48,27 @@ class GrayscaleImage:
# size is of type tuple of integers (DEFAULT = (50, 50))
def resize(self, size):
print size
def resize(self, size): # size is of type float
self.data = imresize(self.data, size)
def get_shape(self):
return self.data.shape
shape = property(get_shape)
def get_width(self):
return self.get_shape()[1]
width = property(get_width)
def get_height(self):
return self.get_shape()[0]
height = property(get_height)
def in_bounds(self, y, x):
return x >= 0 and x < self.width and y >= 0 and y < self.height
def save(self, path):
imsave(path, self.data)
#!/usr/bin/python
import Image
from numpy import array, zeros, byte
from matplotlib.pyplot import imshow, subplot, show, axis
# Divide the examined window to cells (e.g. 16x16 pixels for each cell).
# For each pixel in a cell, compare the pixel to each of its 8 neighbors
# (on its left-top, left-middle, left-bottom, right-top, etc.). Follow the
# pixels along a circle, i.e. clockwise or counter-clockwise.
# Where the center pixel's value is greater than the neighbor, write "1".
# Otherwise, write "0". This gives an 8-digit binary number (which is usually
# converted to decimal for convenience).
# Compute the histogram, over the cell, of the frequency of each "number"
# occurring (i.e., each combination of which pixels are smaller and which are
# greater than the center).
# Optionally normalize the histogram. Concatenate normalized histograms of all
# cells. This gives the feature vector for the window.
CELL_SIZE = 16
def domain_iterator(shape):
"""Iterate over the pixels of an image."""
for y in xrange(shape[0]):
for x in xrange(shape[1]):
yield y, x
image = array(Image.open('../images/test.png').convert('L'))
def in_image(y, x, F):
"""Check if given pixel coordinates are within the bounds of image F."""
return 0 <= y < F.shape[0] and 0 <= x < F.shape[1]
def features(image):
"""Compare each pixel to each of its eight neigheach pixel to each of its
eight neighbours."""
features = zeros(image.shape, dtype=byte)
def cmp_pixels(y, x, p):
"""Check if two pixels (y, x) and p are in the image, and if the value
at (y, x) is larger than the value at p."""
return in_image(y, x, image) and image[y, x] > p
for y, x in domain_iterator(features.shape):
p = image[y, x]
# Walk around the pixel in counter-clokwise order, shifting 1 bit less
# at each neighbour starting at 7 in the top-left corner. This gives a
# 8-bit feature number of a pixel
features[y, x] = byte(cmp_pixels(y - 1, x - 1, p)) << 7 \
| byte(cmp_pixels(y - 1, x, p)) << 6 \
| byte(cmp_pixels(y - 1, x + 1, p)) << 5 \
| byte(cmp_pixels(y, x + 1, p)) << 4 \
| byte(cmp_pixels(y + 1, x + 1, p)) << 3 \
| byte(cmp_pixels(y + 1, x, p)) << 2 \
| byte(cmp_pixels(y + 1, x - 1, p)) << 1 \
| byte(cmp_pixels(y, x - 1, p))
return features
def feature_vectors(image):
"""Create cell histograms of an image"""
F = features(image)
V = feature_vectors(image)
subplot(121)
imshow(image, cmap='gray')
subplot(122)
imshow(V, cmap='gray')
axis('off')
show()
......@@ -9,7 +9,6 @@ class LocalBinaryPatternizer:
self.image = image
self.setup_histograms()
def setup_histograms(self):
cells_in_width = int(ceil(self.image.width / float(self.cell_size)))
cells_in_height = int(ceil(self.image.height / float(self.cell_size)))
......@@ -19,7 +18,6 @@ class LocalBinaryPatternizer:
for j in xrange(cells_in_width):
self.features[i].append(Histogram(256,0,256))
def create_features_vector(self):
''' Walk around the pixels in clokwise order, shifting 1 bit less
at each neighbour starting at 7 in the top-left corner. This gives a
......@@ -40,14 +38,11 @@ class LocalBinaryPatternizer:
return self.get_features_as_array()
def is_pixel_darker(self, y, x, value):
return self.image.in_bounds(y, x) and self.image[y, x] > value
def get_cell_index(self, y, x):
return (y / self.cell_size, x / self.cell_size)
def get_features_as_array(self):
return [item for sublist in self.features for item in sublist]
return [item for sublist in self.features for item in sublist]
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment