def extract_lbp_features(X, P=8, R=5): F = [] # Features are stored here N = X.shape[0] for k in range(N): print("Processing image {}/{}".format(k + 1, N)) image = X[k, ...] lbp = local_binary_pattern(image, P, R) hist = np.histogram(lbp, bins=range(257))[0] F.append(hist) return np.array(F)
def extract_lbp_features(X, P=8, R=5): """ Extract LBP features from all input samples. - R is radius parameter - P is the number of angles for LBP """ F = [] # Features are stored here N = X.shape[0] for k in range(N): print("Processing image {}/{}".format(k + 1, N)) image = X[k, ...] lbp = local_binary_pattern(image, P, R) hist = np.histogram(lbp, bins=range(257))[0] F.append(hist) return np.array(F)
class_1_path = './GTSRB_subset/class1/' class_2_path = './GTSRB_subset/class2/' # Load the images for filepath in os.listdir(class_1_path): images.append(mpimg.imread(class_1_path + filepath)) labels.append(0) for filepath in os.listdir(class_1_path): images.append(mpimg.imread(class_1_path + filepath)) labels.append(1) # Extract features for image in images: features.append(local_binary_pattern(image)) features = np.array(features) features = features.reshape(features.shape[0], features.shape[1] * features.shape[2]) X_train, X_test, y_train, y_test = train_test_split(features, labels) ## Training with different classifiers # Nearest neighbor neigh = KNeighborsClassifier(n_neighbors=3) scores_neigh = cross_val_score(neigh, features, labels, cv=5) print("Neigh score:") print(scores_neigh) neigh.fit(X_train, y_train) neigh_score = accuracy_score(y_test, neigh.predict(X_test))
R = 5 # histograms and corresponding classes X = [] y = [] # class folders class_folders = sorted(glob.glob('GTSRB_subset/*')) for i,folder in enumerate(class_folders): # images in class folder name_list = glob.glob(folder+'/*') for name in name_list: image = plt.imread(name) # histogram of lbp lbp = local_binary_pattern(image, P, R) hist = np.histogram(lbp, bins=range(257))[0] X.append(hist) # corresponding class y.append(i) # convert to numpy X = np.array(X) print(X.shape) y = np.array(y) ''' Question 5 ''' # split train and test
def extract_features(img): lbp1 = local_binary_pattern(img, 8, 5) feature = np.histogram(lbp1, bins=range(257))[0] feature = np.reshape(feature, [1, 256]) return feature