[Numpy-discussion] 2-D Histogram

David Reed david.reed.c at gmail.com
Tue Sep 10 09:52:04 EDT 2013

Hi there,

Is there a faster way to perform a 2D Histogram from a 2D matrix than what
I have below:

def spatial_histogram(frame, n_bins):
shape = frame.shape

h_len = shape[0]/n_bins
w_len = shape[1]/n_bins

h_ind = range(0, shape[0], h_len)
 w_ind = range(0, shape[1], w_len)

max_val = 255*h_len*w_len

out = np.empty((n_bins, n_bins), np.uint8)

for ii in range(n_bins):
for jj in range(n_bins):
out[ii, jj] = np.sum(frame[h_ind[ii]:h_ind[ii]+h_len,

return out

Should I try implementing this in Cython, or is there something I can do in

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20130910/a05b53df/attachment.html>

More information about the NumPy-Discussion mailing list