Atheer
Atheer

Reputation: 921

Local Binary Patterns features

I have a dataset of images that differ in sizes, and when I extract the local binary pattern (uniform local binary 59 feature) from them I get several features as zeros, is it acceptable as features, if not, and how to deal with it enter image description here

Upvotes: 0

Views: 214

Answers (1)

mcagriaksoy
mcagriaksoy

Reputation: 90

You can use this solution: https://github.com/arsho/local_binary_patterns

def get_pixel(img, center, x, y):
    new_value = 0
    try:
        if img[x][y] >= center:
            new_value = 1
    except:
        pass
    return new_value

def lbp_calculated_pixel(img, x, y):  
    center = img[x][y]
    val_ar = []
    val_ar.append(get_pixel(img, center, x-1, y+1))     # top_right
    val_ar.append(get_pixel(img, center, x, y+1))       # right
    val_ar.append(get_pixel(img, center, x+1, y+1))     # bottom_right
    val_ar.append(get_pixel(img, center, x+1, y))       # bottom
    val_ar.append(get_pixel(img, center, x+1, y-1))     # bottom_left
    val_ar.append(get_pixel(img, center, x, y-1))       # left
    val_ar.append(get_pixel(img, center, x-1, y-1))     # top_left
    val_ar.append(get_pixel(img, center, x-1, y))       # top 
    power_val = [1, 2, 4, 8, 16, 32, 64, 128]
    val = 0
    for i in range(len(val_ar)):
        val += val_ar[i] * power_val[i]
    return val    

# Function to generate horizontal projection profile
def getHorizontalProjectionProfile(image):
    # Convert black spots to ones
    image[image == 0]   = 1
    # Convert white spots to zeros
    image[image == 255] = 0
    horizontal_projection = np.sum(image, axis = 1) 
    return horizontal_projection

# Function to generate vertical projection profile
def getVerticalProjectionProfile(image):
    # Convert black spots to ones 
    image[image == 0]   = 1
    # Convert white spots to zeros 
    image[image == 255] = 0
    vertical_projection = np.sum(image, axis = 0)
    return vertical_projection

lenx = x_train.shape[0]
x_train_lbp = np.zeros((lenx, 32,32))

for NUM in range(lenx):
  gray = rgb2gray(x_train[NUM])
  for i in range(0, 32):
    for j in range(0, 32):
      x_train_lbp[NUM][i][j] = lbp_calculated_pixel(gray, i, j)
        
    lenx = x_test.shape[0]
    x_test_lbp = np.zeros((lenx, 32,32))
    
    for NUM in range(lenx):
      gray = rgb2gray(x_test[NUM])
      for i in range(0, 32):
        for j in range(0, 32):
          x_test_lbp[NUM][i][j] = lbp_calculated_pixel(gray, i, j)

    from sklearn import preprocessing
    
    lenx = x_train.shape[0]
    x_train_lbp_vector = np.zeros((lenx, 64))
    
    for NUM in range(lenx):
      horizontal_projection = getHorizontalProjectionProfile(x_train_lbp[NUM])
      vertical_projection = getVerticalProjectionProfile(x_train_lbp[NUM])
      x_train_lbp_vector[NUM] = np.concatenate((horizontal_projection, vertical_projection), axis=0)
      x_train_lbp_vector[NUM] = normalize(x_train_lbp_vector[NUM].reshape(1, -1))
    
    lenx = x_test.shape[0]
    x_test_lbp_vector = np.zeros((lenx, 64))
    
    for NUM in range(lenx):
      horizontal_projection = getHorizontalProjectionProfile(x_test_lbp[NUM])
      vertical_projection = getVerticalProjectionProfile(x_test_lbp[NUM])
      x_test_lbp_vector[NUM] = np.concatenate((horizontal_projection, vertical_projection), axis=0)
      x_test_lbp_vector[NUM] = normalize(x_test_lbp_vector[NUM].reshape(1, -1))

Upvotes: 0

Related Questions