[SciPy-user] stupid array tricks
Mon Feb 9 11:03:45 CST 2009
Thanks Stefan ! The index meister comes through again. I was sort of thinking along those lines but couldn't quite take the final step of understanding how to get the row coordinates for arbitrary filter locations. BTW, I should send you the latest version of my modification of glcom; the "profiling" that showed this part of my code to be the current bottleneck was the result of the generalized glcom (arbitrary number of co-registered images and arbitrary templates) being so fast at generating the co-occurrence matrices (for others on the list, Stefan wrote a very nice ctypes module, glcom, that generates co-occurrence matrices from an image for doing texture analysis).
Center for Imaging of Neurodegenerative Disease, UCSF
VA Medical Center, MRS Unit (114M)
Phone: (415) 221-4810 x3114
FAX: (415) 668-2864
Email: karl young at ucsf edu
From: firstname.lastname@example.org on behalf of Stéfan van der Walt
Sent: Sat 2/7/2009 9:36 AM
To: SciPy Users List
Subject: Re: [SciPy-user] stupid array tricks
2009/2/7 Karl Young <email@example.com>:
> I have three objects, 1) an array containing an "image" (could be any
> dimension), 2) a mask for the image (of the same dimensions as the
> image), and 3) a "template" which is just a list of offset coordinates
> from any point in the image.
You can create a strided view of the image, so that the values around
each position where the filter can be applied becomes a row.
Thereafter, using the indexing tricks shown at
index the view to produce the templated values at each position.
Say your template has length n, then you'd have:
template of shape (1, n)
rows = np.arange(m)[:, None] with shape (m, 1)
When using template and rows in a fancy indexing operating, you should
get an output of shape (m, n).
Here is a simplified example:
# Strided view of your image
In : data
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
In : rows
In : rows.shape
Out: (4, 1)
In : template
Out: array([[0, 2]])
In : template.shape
Out: (1, 2)
In : data[rows, template]
array([[ 0, 2],
[ 3, 5],
[ 6, 8],
[ 9, 11]])
Hope that helps!
SciPy-user mailing list
More information about the SciPy-user