Increasing masked areas in an array? | Newbie | | Join Date: Feb 2007
Posts: 15
| |
Hi guys,
I've asked for help here once before with great results, I thought I'd try it again.
(I'm on Ubuntu 6.06 with python 2.4. Xemacs as editor.)
I have an image as a scipy array and I've made a mask array for it, sources (stars/galaxies/etc.) are flagged with 1, sky is 0. Now I want to blow up the size of each flagged source by a factor of 2, or close to it. Meaning, if I originally have - array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
-
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
-
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
-
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
-
[ 0, 0, 1, 1, 1, 0, 0, 0, 0, 0],
-
[ 0, 0, 1, 1, 1, 0, 0, 0, 0, 0],
-
[ 0, 0, 1, 1, 1, 0, 0, 0, 0, 0],
-
[ 0, 0, 1, 1, 1, 0, 0, 0, 0, 0],
-
[ 0, 0, 1, 1, 1, 0, 0, 0, 0, 0],
-
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
-
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
-
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
-
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
-
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=int32)
I want to have -
array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
-
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
-
[ 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
-
[ 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
-
[ 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
-
[ 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
-
[ 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
-
[ 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
-
[ 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
-
[ 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
-
[ 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
-
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
-
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
-
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=int32)
Now, I thought wrong, and instead I wrote something that simply takes each pixel flagged with '1' and adds the indices of all its neighbouring pixels to the mask. But that only blows things up by one additional pixel at the borders of the source. Really not what I wanted. However, I could use the same technique if I knew the radius of each flagged "island" (at least approximately. and it will be different for every source). But it seems to me I'll have to got throught the image line by line to obtain the max and min indices of each group of neighbouring pixels. That would be incredibly slow, especially since I have over 3000 images (and many many things are being done to them, in addition to masking the sources).
I was wondering if you have some ideas. Maybe something like this already exists somewhere? Surely people have tried at some point to do exactly this, increase the masked area in an array by some factor??
Well, here's my code, it's looks really sad. -
import pyfits as PF
-
import scipy
-
import sets
-
-
def maskObjects(image):
#'image' is segmentation image from SExtractor -
fits=PF.open(image)
-
data=fits[0].data
-
hdr=fits[0].header
-
data=data.astype(float)
-
-
#for test purposes only
-
data=data[1000:1015,1000:1015]
-
-
#make mask, mark source with '1', sky with '0'
-
mask=scipy.where(data>0,1,0)
-
-
idx=[]
-
for i,value in enumerate(mask.flat):
-
if value: idx.append(i)
-
idx_array=scipy.array(idx)
-
width,height=idx_array.shape
-
-
-
-
left=sets.Set((idx_array-1).flat)
-
right=sets.Set((idx_array+1).flat)
-
low_left=sets.Set((idx_array-1+width).flat)
-
low_right=sets.Set((idx_array+1+width).flat)
-
up_left=sets.Set((idx_array-1-width).flat)
-
up_right=sets.Set((idx_array+1-width).flat)
-
orig_set=sets.Set(idx)
-
-
blownup=orig_set.update(left.update(right))
-
blownup.update(low_left.update(low_right))
-
blownup.update(up_left.update(up_right))
-
-
# remove indices out of bounds here. - Later.
-
#max_idx=width*height-1
-
-
| | Newbie | | Join Date: Feb 2007
Posts: 15
| | | re: Increasing masked areas in an array?
I see I made a typo in my code
width,height=idx_array.shape
should instead be
width,height=mask.shape
| | Newbie | | Join Date: Feb 2007
Posts: 15
| | | re: Increasing masked areas in an array?
well, damn it all to hell!! My code doesn't even do what I thought it did, it doesn't increase the size of the masked area, instead it shifts the source twice! I don't get it, surely if a pixel has index x, then the pixel right under it would have index x+image_width??
See the original 
this is the result of my "increased" masked area 
I need coffee.
| | Newbie | | Join Date: Feb 2007
Posts: 15
| | | re: Increasing masked areas in an array?
Well, in case anyone cares, the above can be done correctly by modifying this code I found in the SciPy Cookbook at scipy.org/Cookbook. Import scipy somewhere. -
def enlarge(a, x=2, y=None):
-
"""Enlarges 2D image array a using simple pixel repetition in both dimensions.
-
Enlarges by factor x horizontally and factor y vertically.
-
If y is left as None, uses factor x for both dimensions."""
-
a = asarray(a)
-
assert a.ndim == 2
-
if y == None: y = x
-
for factor in (x, y):
-
assert factor.__class__ == int
-
assert factor > 0
- return a.repeat(y, axis=0).repeat(x, axis=1)
|  | |