473,748 Members | 2,887 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Increasing masked areas in an array?

15 New Member
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

Expand|Select|Wrap|Line Numbers
  1. array([[  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
  2.        [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
  3.        [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
  4.        [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
  5.        [  0,   0,   1,   1,   1,   0,   0,   0,   0,   0],
  6.        [  0,   0,   1,   1,   1,   0,   0,   0,   0,   0],
  7.        [  0,   0,   1,   1,   1,   0,   0,   0,   0,   0],
  8.        [  0,   0,   1,   1,   1,   0,   0,   0,   0,   0],
  9.        [  0,   0,   1,   1,   1,   0,   0,   0,   0,   0],
  10.        [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
  11.        [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
  12.        [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
  13.        [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
  14.        [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0]], dtype=int32)
I want to have
Expand|Select|Wrap|Line Numbers
  1. array([[  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
  2.        [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
  3.        [  1,   1,   1,   1,   1,   1,   1,   1,   0,   0],
  4.        [  1,   1,   1,   1,   1,   1,   1,   1,   0,   0],
  5.        [  1,   1,   1,   1,   1,   1,   1,   1,   0,   0],
  6.        [  1,   1,   1,   1,   1,   1,   1,   1,   0,   0],
  7.        [  1,   1,   1,   1,   1,   1,   1,   1,   0,   0],
  8.        [  1,   1,   1,   1,   1,   1,   1,   1,   0,   0],
  9.        [  1,   1,   1,   1,   1,   1,   1,   1,   0,   0],
  10.        [  1,   1,   1,   1,   1,   1,   1,   1,   0,   0],
  11.        [  1,   1,   1,   1,   1,   1,   1,   1,   0,   0],
  12.        [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
  13.        [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
  14.        [  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.




Expand|Select|Wrap|Line Numbers
  1. import pyfits as PF
  2. import scipy
  3. import sets
  4.  
  5. def maskObjects(image):
       #'image' is segmentation image from SExtractor
  6.     fits=PF.open(image)
  7.     data=fits[0].data
  8.     hdr=fits[0].header
  9.     data=data.astype(float)
  10.  
  11.      #for test purposes only
  12.     data=data[1000:1015,1000:1015]
  13.  
  14.     #make mask, mark source with '1', sky with '0'
  15.     mask=scipy.where(data>0,1,0)
  16.  
  17.     idx=[]
  18.     for i,value in enumerate(mask.flat):
  19.         if value:  idx.append(i)
  20.      idx_array=scipy.array(idx)
  21.     width,height=idx_array.shape
  22.  
  23.  
  24.  
  25.     left=sets.Set((idx_array-1).flat)
  26.     right=sets.Set((idx_array+1).flat)    
  27.     low_left=sets.Set((idx_array-1+width).flat)
  28.     low_right=sets.Set((idx_array+1+width).flat)
  29.     up_left=sets.Set((idx_array-1-width).flat)
  30.     up_right=sets.Set((idx_array+1-width).flat)
  31.     orig_set=sets.Set(idx)
  32.  
  33.    blownup=orig_set.update(left.update(right))
  34.    blownup.update(low_left.update(low_right))
  35.    blownup.update(up_left.update(up_right))
  36.  
  37.    # remove indices out of bounds here. - Later.
  38.    #max_idx=width*height-1
  39.  
  40.         
Dec 11 '07 #1
3 2182
eso40043
15 New Member
I see I made a typo in my code



width,height=id x_array.shape

should instead be

width,height=ma sk.shape
Dec 11 '07 #2
eso40043
15 New Member
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.
Dec 11 '07 #3
eso40043
15 New Member
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.

Expand|Select|Wrap|Line Numbers
  1. def enlarge(a, x=2, y=None):
  2.     """Enlarges 2D image array a using simple pixel repetition in both dimensions.
  3.     Enlarges by factor x horizontally and factor y vertically.
  4.     If y is left as None, uses factor x for both dimensions."""
  5.     a = asarray(a)
  6.     assert a.ndim == 2
  7.     if y == None:    y = x
  8.     for factor in (x, y):
  9.         assert factor.__class__ == int
  10.         assert factor > 0
  11.     return a.repeat(y, axis=0).repeat(x, axis=1)
Dec 17 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1954
by: matt | last post by:
Hi, I have two pairs of image. In each pair there is one image that is masked by the other. I have then attempted to overlay them. However, the image that is being masked has white corners. How can I make these corners transparent. An example is shown at http://portcanal.co.uk/filter.html What I would like is for the background to the yellow triangle pair to be transparent. How do I do this? Thanks.
3
9511
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At the end of this message is the inflate method this is where I get stuck I know that I need a byte array but because I am decompressing a string I have no idea of how big the byte array will need to be in the end (the inflate and deflate methods...
2
13180
by: orekinbck | last post by:
Hi There I have spent alot of time trying to get a masked text box to validate e-mails, but with no success. Mainly because I can't figure out how to account for the wide variety of different e-mail addresses, for example: billwallis@hta.com bill.wallis@hta.com billwallis@hta.com.au bill.wallis@hta.com.au
1
3478
by: Kostis | last post by:
Hello there! I have created a windows application in VB.NET and I want to retrieve data from an access database. I create the OleDbConnection, an OleDbAdapter, a dataset and finally a Data Form using the Data Form Wizard. Everything works fine. The problem is that I want to use the masked edit control to manage the way some fields appear, and are being stored to the database. I insert the "Microsoft Masked Edit Control, version 6.0" (Path...
3
2610
by: Panos | last post by:
Hi all, I can't clear a Masked edit control. I use it to input dates, so I have defined the mask as "##/##/####". I use the the well known meb.mask="" meb.text="" , but nothing happens. Does anybody have a clue, why these lines don' t work? I use vb.net 2002 Thank u
7
8698
by: DazedAndConfused | last post by:
Curently I manualy code keypress edits. i.e. allow only 3 digits before decimal and two digits after decimal in .NET. Is there an easy solution to mask text boxes? The MSMASK32.OCX from vb6 does not seem to be appropriate for a desktop application with textboxes not bound to a dataset, am I wrong? -- How time fly's when you don't know what you are doing!
0
1027
by: jean-michel bain-cornu | last post by:
Hi all, I'm trying to get the whole field selected when the caret arrives into a wx masked control. This is perfectly done with the program below. But if I comment out the line "r= dlg.ShowModal()", I get something different : the data within the field is no more correctly selected. Why ? And is there any solution ? If somebody have got an idea, I'd be delighted.
3
9137
by: dmbuso | last post by:
I have a money field defined in a SQL Server 05 database with a value of 49.50. Also, it displays in SQL Server as 49.5000. I have a form in VB.NET 2005 and I'm using the new MaskedTextBox control in Visual Studio. I set the Mask property for the text box to "$999.00". The problem is when I run the form the text box displays "$495.00" and not "$49.50" as one would expect. It it shifting the values left here. How do I fix this? Thanks...
0
1754
by: michels287 | last post by:
I have a masked textbox on my form. I have an onscreen keyboard with numbers only. The masked format for the masked textbox is: (###) ### - #### I want to make sure they enter the phone numbers properly, hence the mask. What I cannot seem to figure out is why can I not send the button captions (captions are the value of the number buttons, 1 through 9) into the masked box without an error? I get: Run Time Error '380'. Invalid...
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8830
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9247
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8243
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2783
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.