Skip to content

Instantly share code, notes, and snippets.

@louist87
Created February 27, 2014 12:13
Show Gist options
  • Save louist87/9248952 to your computer and use it in GitHub Desktop.
Save louist87/9248952 to your computer and use it in GitHub Desktop.
Vectorized HOG feature detector for SimpleCV images
def findHOGFeatures(self, n_divs=3, n_bins=6):
"""
**SUMMARY**
Get HOG(Histogram of Oriented Gradients) features from the image.
**PARAMETERS**
* *n_divs* - the number of divisions(cells).
* *n_divs* - the number of orientation bins.
**RETURNS**
Returns the HOG vector in a numpy array
"""
n_HOG = n_divs * n_divs * n_bins # Size of HOG vector
HOG = np.zeros((n_HOG, 1)) # Initialize output HOG vector
# Apply sobel on image to find x and y orientations of the image
Icv = self.getNumpyCv2()
Ix = cv2.Sobel(Icv, ddepth=cv.CV_32F, dx=1, dy=0, ksize=3)
Iy = cv2.Sobel(Icv, ddepth=cv.CV_32F, dx=0, dy=1, ksize=3)
Ix = Ix.transpose(1, 0, 2)
Iy = Iy.transpose(1, 0, 2)
cellx = self.width / n_divs # width of each cell(division)
celly = self.height / n_divs # height of each cell(division)
# Area of image
img_area = self.height * self.width
#Range of each bin
BIN_RANGE = (2 * pi) / n_bins
angles = np.arctan2(Iy, Ix)
magnit = ((Ix ** 2) + (Iy ** 2)) ** 0.5
height, width = self.height, self.width
bins = (angles[...,0] % (2 * pi) / BIN_RANGE).astype(int)
x, y = np.mgrid[:width, :height]
x = x * n_divs // width
y = y * n_divs // height
labels = (x * n_divs + y) * n_bins + bins
index = np.arange(n_HOG)
HOG = ndi_sum(magnit[..., 0], labels, index)
return HOG / self_area
@carlhung
Copy link

i want to get return a featureset like the other function in the imageclass that i can draw() or crop().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment