[Numpy-discussion] efficient 3d histogram creation

David Huard david.huard@gmail....
Mon May 4 14:18:20 CDT 2009


On Mon, May 4, 2009 at 7:00 AM, <josef.pktd@gmail.com> wrote:

> On Mon, May 4, 2009 at 12:31 AM, Chris Colbert <sccolbert@gmail.com>
> wrote:
> > this actually sort of worked. Thanks for putting me on the right track.
> >
> > Here is what I ended up with.
> >
> > this is what I ended up with:
> >
> > def hist3d(imgarray):
> >     histarray = N.zeros((16, 16, 16))
> >     temp = imgarray.copy()
> >     bins = N.arange(0, 257, 16)
> >     histarray = N.histogramdd((temp[:,:,0].ravel(), temp[:,:,1].ravel(),
> > temp[:,:,2].ravel()), bins=(bins, bins, bins))[0]
> >     return histarray
> >
> > this creates a 3d histogram of rgb image values in the range 0,255 using
> 16
> > bins per component color.
> >
> > on a 640x480 image, it executes in 0.3 seconds vs 4.5 seconds for a for
> > loop.
> >
> > not quite framerate, but good enough for prototyping.
> >
>
> I don't think your copy to temp is necessary, and use reshape(-1,3) as
> in the example of Stefan, which will avoid copying the array 3 times.
>
> If you need to gain some more speed, then rewriting histogramdd and
> removing some of the unnecessary checks and calculations looks
> possible.


Indeed, the strategy used in the histogram function is faster than the one
used in the histogramdd case, so porting one to the other should speed
things up.

David


>
> Josef
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20090504/05885efc/attachment.html 


More information about the Numpy-discussion mailing list