Open lastlegion opened 10 years ago
Thanks Louis for your inputs. I have updated the code to improve the performance.
@lastlegion I did a bit of digging around codereview.stackexchange.com and got some help vectorizing your function. This new version clocks in at 49.5 ms total execution time, or about 88 times faster than the original iterative version. You can find stack exchange post here.
A few notes about the changes (mostly copied, pasted, & adapted from Gareth Rees' answer):
% (2 * pi)
to get the angles in the range [0, 2π)
. An alternative that's more like your code would be angles[angles < 0] += 2 * pi
but using the modulus is shorter and, I think, clearer.n_HOG
is much less than img_area
and so it's cheaper to do the division later when there are fewer items. (This means that the results differ very slightly from your code, so bear that in mind when checking.)angles[...,0]
and magnit[...,0]
here in order to drop the third axis. But I think it would make more sense if you dropped this axis earlier, before computing angles
and magnit
, by writing Ix = Ix[...,0]
or just Ix = Ix.reshape((height, width))
if you know that the last axis has length 1.Anyway, the code can be found in this gist.
If you don't feel like amending your pull request, I'll be happy to file one on my end. Thanks again for your very helpful contributions! I hope your request will be merged soon!
HOG(Histogram of Oriented Gradients) features are widely used in various Computer Vision and image processing for object detection.
Usage
For sample image:
It would return the hog descriptor: