[SciPy-User] Most efficient way to eliminate or keep specified features from labeled image

Jason Roberts jason.roberts at duke.edu
Wed Apr 6 20:23:01 EDT 2011


Zach,

Nicely done! I obviously did not think if doing it that way, but now that
I've seen what you did, I understand how it would work.

Jason 

-----Original Message-----
From: scipy-user-bounces at scipy.org [mailto:scipy-user-bounces at scipy.org] On
Behalf Of Zachary Pincus
Sent: Wednesday, April 06, 2011 7:55 PM
To: SciPy Users List
Subject: Re: [SciPy-User] Most efficient way to eliminate or keep specified
features from labeled image

>> Basically, as input to choose, you pass an array that's as long as  
>> the
>> maximum label, and has ones at the indices of the labels you want to
>> keep, and zeros elsewhwere.
>
> Zach,
>
> Thanks for your suggestions! I did not realize that numpy.choose  
> could be
> used that way, and that approach works when there are 32 or less  
> choices
> (i.e. features to keep). Unfortunately numpy.choose is limited to 32  
> or less
> and I often have hundreds.

Ugh! Not documented and I sadly did not test the upper limits of this  
case.

However, this reminded me that in simple cases, fancy indexing works  
just as choose does. So another second's puzzling gives this solution,  
which I think is pretty clever:

In [93]: a = numpy.arange(400).reshape((20,20))

In [94]: keep = numpy.zeros(400, dtype=int)

In [95]: keep[128+numpy.arange(64)] = 1

In [96]: mask = keep[a]

Fancy indexing never ceases to amaze. Perfect for this sort of look-up  
table task.

_______________________________________________
SciPy-User mailing list
SciPy-User at scipy.org
http://mail.scipy.org/mailman/listinfo/scipy-user




More information about the SciPy-User mailing list