|
| 1 | +#!/usr/bin/python |
| 2 | +# OpenCV bindings |
| 3 | +import cv2 |
| 4 | +import os |
| 5 | +# Local Binary Pattern function |
| 6 | +from skimage.feature import local_binary_pattern |
| 7 | +# To calculate a normalized histogram |
| 8 | +from scipy.stats import itemfreq |
| 9 | +from sklearn.preprocessing import normalize |
| 10 | +# For plotting |
| 11 | +import matplotlib.pyplot as plt |
| 12 | +# For array manipulations |
| 13 | +import numpy as np |
| 14 | +# For saving histogram values |
| 15 | +from sklearn.externals import joblib |
| 16 | +# For command line input |
| 17 | +import argparse as ap |
| 18 | +# Utility Package |
| 19 | +import cvutils |
| 20 | + |
| 21 | +# Get the path of the input image |
| 22 | +parser = ap.ArgumentParser() |
| 23 | +parser.add_argument("-i", "--inputimage", help="Path to Input Image folder", required="True") |
| 24 | +args = vars(parser.parse_args()) |
| 25 | + |
| 26 | +train_images = cvutils.imlist(args["inputimage"]) |
| 27 | +X_name= [] |
| 28 | +X_hist=[] |
| 29 | + |
| 30 | +for train_image in train_images: |
| 31 | + # Read the image |
| 32 | + im = cv2.imread(train_image) |
| 33 | + # Convert to grayscale as LBP works on grayscale image |
| 34 | + im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) |
| 35 | + cv2.imshow('',im_gray) |
| 36 | + radius = 3 |
| 37 | + # Number of points to be considered as neighbourers |
| 38 | + no_points = 8 * radius |
| 39 | + # Uniform LBP is used |
| 40 | + lbp = local_binary_pattern(im_gray, no_points, radius, method='uniform') |
| 41 | + # Calculate the histogram |
| 42 | + x = itemfreq(lbp.ravel()) |
| 43 | + # Normalize the histogram |
| 44 | + hist = x[:, 1]/sum(x[:, 1]) |
| 45 | + # print(X_hist) |
| 46 | + print(hist) |
| 47 | + plt.hist(hist, density=True, bins=30) |
| 48 | + plt.show() |
| 49 | + plt.clf() |
| 50 | + |
0 commit comments