Created
March 7, 2017 15:25
-
-
Save naxty/e99c67526f224cea510f0c8834951623 to your computer and use it in GitHub Desktop.
Histogram of Oriented Gradients
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.image as mpimg | |
import matplotlib.pyplot as plt | |
import numpy as np | |
import cv2 | |
import glob | |
from skimage.feature import hog | |
# Read in our vehicles and non-vehicles | |
images = glob.glob('*.jpeg') | |
cars = [] | |
notcars = [] | |
for image in images: | |
if 'image' in image or 'extra' in image: | |
notcars.append(image) | |
else: | |
cars.append(image) | |
# Define a function to return HOG features and visualization | |
def get_hog_features(img, orient, pix_per_cell, cell_per_block, vis=False, feature_vec=True): | |
if vis == True: | |
# Use skimage.hog() to get both features and a visualization | |
features, hog_image = hog(img, orientations=orient, pixels_per_cell=(pix_per_cell, pix_per_cell), | |
cells_per_block=(cell_per_block, cell_per_block), visualise=vis, feature_vector=False) | |
return features, hog_image | |
else: | |
# Use skimage.hog() to get features only | |
features = [] # Remove this line | |
features = hog(img, orientations=orient, pixels_per_cell=(pix_per_cell, pix_per_cell), | |
cells_per_block=(cell_per_block, cell_per_block), visualise=vis, feature_vector=feature_vec) | |
return features | |
# Generate a random index to look at a car image | |
ind = np.random.randint(0, len(cars)) | |
# Read in the image | |
image = mpimg.imread(cars[ind]) | |
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) | |
# Define HOG parameters | |
orient = 9 | |
pix_per_cell = 8 | |
cell_per_block = 2 | |
# Call our function with vis=True to see an image output | |
features, hog_image = get_hog_features(gray, orient, | |
pix_per_cell, cell_per_block, | |
vis=True, feature_vec=False) | |
# Plot the examples | |
fig = plt.figure() | |
plt.subplot(121) | |
plt.imshow(image, cmap='gray') | |
plt.title('Example Car Image') | |
plt.subplot(122) | |
plt.imshow(hog_image, cmap='gray') | |
plt.title('HOG Visualization') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment