[Numpy-discussion] efficient 3d histogram creation

Chris Colbert sccolbert at gmail.com
Sun May 3 20:15:55 EDT 2009


in my endless pursuit of perfomance, i'm searching for a quick way to create
a 3d histogram from a 3d rgb image.

Here is what I have so far for a (16,16,16) 3d histogram:

def hist3d(imgarray):
    histarray = N.zeros((16, 16, 16))
    temp = imgarray.copy()
    (i, j) = imgarray.shape[0:2]
    temp = (temp - temp % 16) / 16
    for a in range(i):
        for b in range(j):
            (b1, b2, b3) = temp[a, b, :]
            histarray[b1, b2, b3] += 1
    return histarray

this works, but takes about 4 seconds for a 640x480 image.

I tried doing the inverse of my previous post, namely replacing the nested
for loop with:
histarray[temp[:,:,0], temp[:,:,1], temp[:,:,2]] += 1

but that doesn't work for whatever reason. It gives me number, but they're
incorrect.

Any ideas?

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


More information about the NumPy-Discussion mailing list