Skip to content

Instantly share code, notes, and snippets.

@kor01
Last active August 30, 2017 01:30
Show Gist options
  • Save kor01/b0a97909d7b305b5366c9b33266ef16f to your computer and use it in GitHub Desktop.
Save kor01/b0a97909d7b305b5366c9b33266ef16f to your computer and use it in GitHub Desktop.
[RoboND_recognition] #recognition #robotics
purpose:
to give each cluster in segmentation output a label
s.t. the grounding geometric specification of semantic command can be extracted
#hsv: hue saturation and vlue
#value stands for brightness
#color histogram:
#the bag of words representation of color
#divide each color channel into bins and quantize each pixel
#sum over all pixels to get BOW representation
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import numpy as np
image = mpimg.imread('can.jpeg')
plt.imshow(image)
# Take histograms in R, G, and B
r_hist = np.histogram(image[:,:,0], bins=32, range=(0, 256))
g_hist = np.histogram(image[:,:,1], bins=32, range=(0, 256))
b_hist = np.histogram(image[:,:,2], bins=32, range=(0, 256))
bin_edges = r_hist[1]
bin_centers = (bin_edges[1:] + bin_edges[0:len(bin_edges)-1])/2
# Plot a figure with all three bar charts
fig = plt.figure(figsize=(12,3))
plt.subplot(131)
plt.bar(bin_centers, r_hist[0])
plt.xlim(0, 256)
plt.title('R Histogram')
plt.subplot(132)
plt.bar(bin_centers, g_hist[0])
plt.xlim(0, 256)
plt.title('G Histogram')
plt.subplot(133)
plt.bar(bin_centers, b_hist[0])
plt.xlim(0, 256)
plt.title('B Histogram')
plt.show()
hist_features = np.concatenate((r_hist[0], g_hist[0], b_hist[0])).astype(np.float64)
import numpy as np
import cv2
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
def color_hist(img, nbins=32, bins_range=(0, 256)):
# Convert from RGB to HSV using cv2.cvtColor()
# Compute the histogram of the HSV channels separately
# Concatenate the histograms into a single feature vector
# Normalize the result
# Return the feature vector
image = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
r_hist = np.histogram(image[:,:,0], bins=nbins, range=bins_range)
g_hist = np.histogram(image[:,:,1], bins=nbins, range=bins_range)
b_hist = np.histogram(image[:,:,2], bins=nbins, range=bins_range)
hist_features = np.concatenate((r_hist[0], g_hist[0], b_hist[0])).astype(np.float64)
norm_features = hist_features / np.sum(hist_features)
return norm_features
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment