فهرست منبع

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.

unknown 14 سال پیش
والد
کامیت
2de250dc74
2فایلهای تغییر یافته به همراه46 افزوده شده و 0 حذف شده
  1. 28 0
      src/LocalBinaryPatternizer.py
  2. 18 0
      src/LocalBinaryPatternizerTest.py

+ 28 - 0
src/LocalBinaryPatternizer.py

@@ -0,0 +1,28 @@
+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

+ 18 - 0
src/LocalBinaryPatternizerTest.py

@@ -0,0 +1,18 @@
+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()