Commit 2de250dc authored by unknown's avatar unknown

Bestaande LBP omgezet naar een OO variant die gebruik maak van de

GrayscaleImage klasse als voorstel, orginele variant behouden.
Extra functie voor GrayscaleImage in_bounds(y,x) om te kijken of
coordinaten in de afbeelding vallen.
parent 44ce5bcc
from numpy import zeros, byte
class LocalBinaryPatternizer:
def __init__(self, image, cell_size = 16):
self.cell_size = cell_size
self.image = image
self.features = zeros(self.image.shape)
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
8-bit feature number of a pixel'''
for y, x, value in self.image:
self.features[y, x] = \
(self.is_pixel_darker(y - 1, x - 1, value) << 7) \
| (self.is_pixel_darker(y - 1, x , value) << 6) \
| (self.is_pixel_darker(y - 1, x + 1, value) << 5) \
| (self.is_pixel_darker(y , x + 1, value) << 4) \
| (self.is_pixel_darker(y + 1, x + 1, value) << 3) \
| (self.is_pixel_darker(y + 1, x , value) << 2) \
| (self.is_pixel_darker(y + 1, x - 1, value) << 1) \
| (self.is_pixel_darker(y , x - 1, value) << 0)
return self.features
def is_pixel_darker(self, x, y, value):
return self.image.in_bounds(y, x) and self.image[y, x] > value
\ No newline at end of file
from GrayscaleImage import GrayscaleImage
from LocalBinaryPatternizer import LocalBinaryPatternizer
from matplotlib.pyplot import imshow, subplot, show, axis
image = GrayscaleImage("../images/test.png")
lbp = LocalBinaryPatternizer(image)
feature_vector = lbp.create_features_vector()
feature_vector /= 255 # Prepare for displaying -> 0 - 255 -> 0 - 1
subplot(121)
imshow(image.data, cmap='gray')
subplot(122)
imshow(feature_vector, cmap='gray')
axis('off')
show()
\ 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