# Polygon masking possible?

Hi Søren,

I've put this back on the list in case it's useful to anyone else, or
if there are better suggestions or improvements around. Hope you don't
mind.

···

On 22/01/2008, Søren Nielsen <soren.skou.nielsen@...287...> wrote:

Yeah i'd like to see your code if I can..

import numpy as n

def get_poly_pts(x, y, shape):
"""Creates convex polygon mask from list of corners.

Parameters
----------
x : array_like
x co-ordinates of corners
y : array_like
y co-ordinates of corners, in order corresponding to x
shape : array_like
dimension sizes of result

Returns
-------
build : ndarray
2-D array of shape shape with values True inside polygon

Notes
-----
Code is constrained to convex polygons by "inside"
assessment criterion.

"""
x = n.asarray(x)
y = n.asarray(y)
shape = n.asarray(shape)
npts = x.size # should probably assert x.size == y.size
inds = n.indices( shape )
xs = inds[0]
ys = inds[1]
xav = n.round(x.mean()).astype(int)
yav = n.round(y.mean()).astype(int)
for i in xrange(npts): # iterate over pairs of co-ordinates
j = (i + 1) % npts
m = (y[j] - y[i])/(x[j] - x[i])
c = (x[j] * y[i] - x[i] * y[j])/(x[j] - x[i])
thisone = ( ys > m * xs + c )
if thisone[xav, yav] == False:
thisone = ~thisone
if i == 0:
build = thisone
else:
build &= thisone
return build

(released under BSD licence)

I just needed the push over the edge to know how to draw on the canvas,
mapping clicks etc. since i'm still fairly new to matplotlib, so I think

I hope so. As you can see this code doesn't do any of the drawing or
click collecting, but the cookbook page should be able to guide you
there. Ask again on the list if you have any further questions and
we'll see if we can help.

Also, the code assumes that the average co-ordinate is inside the
shape - that's true for convex polygons, but not necessarily for
arbitrary ones. I use if after taking a convex hull of a greater list
of points (using the delaunay module in scipy (now in scikits, I
hear)), which ensures convexity. You just need to be aware of that
limitation.

Cheers,

A.
--
AJC McMorland, PhD candidate
Physiology, University of Auckland

Hello,
I am also interested in masking polygons and defining the polygon by ‘clicking’ on the image… but I do not know anything about GUI… does anyone can help? Is there already something implemented?
Thanks!
Chiara

···

Date: Wed, 23 Jan 2008 13:50:15 +1300
From: amcmorl@…287…
To: matplotlib-users@lists.sourceforge.net
Subject: Re: [Matplotlib-users] Polygon masking possible?

Hi Søren,

I’ve put this back on the list in case it’s useful to anyone else, or
if there are better suggestions or improvements around. Hope you don’t
mind.

On 22/01/2008, Søren Nielsen <soren.skou.nielsen@…287…> wrote:

Yeah i’d like to see your code if I can…

import numpy as n

def get_poly_pts(x, y, shape):
“”"Creates convex polygon mask from list of corners.

## Parameters

x : array_like
x co-ordinates of corners
y : array_like
y co-ordinates of corners, in order corresponding to x
shape : array_like
dimension sizes of result

## Returns

build : ndarray
2-D array of shape shape with values True inside polygon

## Notes

Code is constrained to convex polygons by “inside”
assessment criterion.

“”"
x = n.asarray(x)
y = n.asarray(y)
shape = n.asarray(shape)
npts = x.size # should probably assert x.size == y.size
inds = n.indices( shape )
xs = inds[0]
ys = inds[1]
xav = n.round(x.mean()).astype(int)
yav = n.round(y.mean()).astype(int)
for i in xrange(npts): # iterate over pairs of co-ordinates
j = (i + 1) % npts
m = (y[j] - y[i])/(x[j] - x[i])
c = (x[j] * y[i] - x[i] * y[j])/(x[j] - x[i])
thisone = ( ys > m * xs + c )
if thisone[xav, yav] == False:
thisone = ~thisone
if i == 0:
build = thisone
else:
build &= thisone
return build

(released under BSD licence)

I just needed the push over the edge to know how to draw on the canvas,
mapping clicks etc. since i’m still fairly new to matplotlib, so I think

I hope so. As you can see this code doesn’t do any of the drawing or
click collecting, but the cookbook page should be able to guide you
there. Ask again on the list if you have any further questions and
we’ll see if we can help.

Also, the code assumes that the average co-ordinate is inside the
shape - that’s true for convex polygons, but not necessarily for
arbitrary ones. I use if after taking a convex hull of a greater list
of points (using the delaunay module in scipy (now in scikits, I
hear)), which ensures convexity. You just need to be aware of that
limitation.

Cheers,

## A.

AJC McMorland, PhD candidate
Physiology, University of Auckland

This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft® Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Express yourself instantly with MSN Messenger! MSN Messenger

Hi,

I often do this with ds9 and funtools.
ds9 is an astronomy-oriented image viewer (http://hea-www.harvard.edu/RD/ds9/)
but you can also use it with numpy array.
Within ds9, you can define regions (ellipse, polynomial, etc) easily
with a mouse.
After you define a region (and save it as a file), you can convert it
to a mask image
with funtools (funtools is a name of an astronomy-oriented image
utility pacakge).
funtools only support fits file (image format in astronomy) so this
can be a bit tricky, but if you're
interested i'll send my python wrapper code for it.

So, take a look at ds9 and see it fits your need.
To view numpy array in ds9,
*. From python, save the array as a file (tofile method, better use
"arr" as an extension)
* in ds9, file-> open others -> open array. You need to select
array dimension, type and endianness of the array.

Regards,

-JJ

···

On Sat, Mar 8, 2008 at 11:17 AM, Chiara Caronna <chiaracaronna@...32...> wrote:

Hello,
I am also interested in masking polygons and defining the polygon by
'clicking' on the image... but I do not know anything about GUI.... does
anyone can help? Is there already something implemented?
Thanks!
Chiara

> Date: Wed, 23 Jan 2008 13:50:15 +1300
> From: amcmorl@...287...
> To: matplotlib-users@lists.sourceforge.net
> Subject: Re: [Matplotlib-users] Polygon masking possible?

>
> Hi Søren,
>
> I've put this back on the list in case it's useful to anyone else, or
> if there are better suggestions or improvements around. Hope you don't
> mind.
>
> On 22/01/2008, Søren Nielsen <soren.skou.nielsen@...287...> wrote:
> > Yeah i'd like to see your code if I can..
>
> import numpy as n
>
> def get_poly_pts(x, y, shape):
> """Creates convex polygon mask from list of corners.
>
> Parameters
> ----------
> x : array_like
> x co-ordinates of corners
> y : array_like
> y co-ordinates of corners, in order corresponding to x
> shape : array_like
> dimension sizes of result
>
> Returns
> -------
> build : ndarray
> 2-D array of shape shape with values True inside polygon
>
> Notes
> -----
> Code is constrained to convex polygons by "inside"
> assessment criterion.
>
> """
> x = n.asarray(x)
> y = n.asarray(y)
> shape = n.asarray(shape)
> npts = x.size # should probably assert x.size == y.size
> inds = n.indices( shape )
> xs = inds[0]
> ys = inds[1]
> xav = n.round(x.mean()).astype(int)
> yav = n.round(y.mean()).astype(int)
> for i in xrange(npts): # iterate over pairs of co-ordinates
> j = (i + 1) % npts
> m = (y[j] - y[i])/(x[j] - x[i])
> c = (x[j] * y[i] - x[i] * y[j])/(x[j] - x[i])
> thisone = ( ys > m * xs + c )
> if thisone[xav, yav] == False:
> thisone = ~thisone
> if i == 0:
> build = thisone
> else:
> build &= thisone
> return build
>
> (released under BSD licence)
>
> > I just needed the push over the edge to know how to draw on the canvas,
> > mapping clicks etc. since i'm still fairly new to matplotlib, so I think
> > your code will be helpfull.
>
> I hope so. As you can see this code doesn't do any of the drawing or
> click collecting, but the cookbook page should be able to guide you
> there. Ask again on the list if you have any further questions and
> we'll see if we can help.
>
> Also, the code assumes that the average co-ordinate is inside the
> shape - that's true for convex polygons, but not necessarily for
> arbitrary ones. I use if after taking a convex hull of a greater list
> of points (using the delaunay module in scipy (now in scikits, I
> hear)), which ensures convexity. You just need to be aware of that
> limitation.
>
> Cheers,
>
> A.
> --
> AJC McMorland, PhD candidate
> Physiology, University of Auckland
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Microsoft
> Defy all challenges. Microsoft(R) Visual Studio 2008.
> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users

________________________________
Express yourself instantly with MSN Messenger! MSN Messenger
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Hi,
I tried ds9 and It looks like this is what I would like to do (though I couldn’t try funtools, but what you describe is good). DO you think it is possible to make something like this with matplotlib?
Thanks a lot for your help,
Chiara

···

Date: Wed, 12 Mar 2008 02:55:55 -0400
From: lee.j.joon@…287…
To: chiaracaronna@…32…
Subject: Re: [Matplotlib-users] Polygon masking possible?
CC: matplotlib-users@lists.sourceforge.net

Hi,

I often do this with ds9 and funtools.
ds9 is an astronomy-oriented image viewer (http://hea-www.harvard.edu/RD/ds9/)
but you can also use it with numpy array.
Within ds9, you can define regions (ellipse, polynomial, etc) easily
with a mouse.
After you define a region (and save it as a file), you can convert it
to a mask image
with funtools (funtools is a name of an astronomy-oriented image
utility pacakge).
funtools only support fits file (image format in astronomy) so this
can be a bit tricky, but if you’re
interested i’ll send my python wrapper code for it.

So, take a look at ds9 and see it fits your need.
To view numpy array in ds9,
*. From python, save the array as a file (tofile method, better use
“arr” as an extension)

• in ds9, file-> open others -> open array. You need to select
array dimension, type and endianness of the array.

Regards,

-JJ

On Sat, Mar 8, 2008 at 11:17 AM, Chiara Caronna > <chiaracaronna@…32…> wrote:

Hello,
I am also interested in masking polygons and defining the polygon by
‘clicking’ on the image… but I do not know anything about GUI… does
anyone can help? Is there already something implemented?
Thanks!
Chiara

Date: Wed, 23 Jan 2008 13:50:15 +1300
From: amcmorl@…287…
To: matplotlib-users@lists.sourceforge.net
Subject: Re: [Matplotlib-users] Polygon masking possible?

Hi Søren,

I’ve put this back on the list in case it’s useful to anyone else, or
if there are better suggestions or improvements around. Hope you don’t
mind.

On 22/01/2008, Søren Nielsen <soren.skou.nielsen@…287…> wrote:

Yeah i’d like to see your code if I can…

import numpy as n

def get_poly_pts(x, y, shape):
“”"Creates convex polygon mask from list of corners.

## Parameters

x : array_like
x co-ordinates of corners
y : array_like
y co-ordinates of corners, in order corresponding to x
shape : array_like
dimension sizes of result

## Returns

build : ndarray
2-D array of shape shape with values True inside polygon

## Notes

Code is constrained to convex polygons by “inside”
assessment criterion.

“”"
x = n.asarray(x)
y = n.asarray(y)
shape = n.asarray(shape)
npts = x.size # should probably assert x.size == y.size
inds = n.indices( shape )
xs = inds[0]
ys = inds[1]
xav = n.round(x.mean()).astype(int)
yav = n.round(y.mean()).astype(int)
for i in xrange(npts): # iterate over pairs of co-ordinates
j = (i + 1) % npts
m = (y[j] - y[i])/(x[j] - x[i])
c = (x[j] * y[i] - x[i] * y[j])/(x[j] - x[i])
thisone = ( ys > m * xs + c )
if thisone[xav, yav] == False:
thisone = ~thisone
if i == 0:
build = thisone
else:
build &= thisone
return build

(released under BSD licence)

I just needed the push over the edge to know how to draw on the canvas,
mapping clicks etc. since i’m still fairly new to matplotlib, so I think

I hope so. As you can see this code doesn’t do any of the drawing or
click collecting, but the cookbook page should be able to guide you
there. Ask again on the list if you have any further questions and
we’ll see if we can help.

Also, the code assumes that the average co-ordinate is inside the
shape - that’s true for convex polygons, but not necessarily for
arbitrary ones. I use if after taking a convex hull of a greater list
of points (using the delaunay module in scipy (now in scikits, I
hear)), which ensures convexity. You just need to be aware of that
limitation.

Cheers,

## A.

AJC McMorland, PhD candidate
Physiology, University of Auckland

This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft® Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

Matplotlib-users mailing list
Matplotlib-users@…1739…ge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

## Express yourself instantly with MSN Messenger! MSN Messenger

This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft® Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Express yourself instantly with MSN Messenger! MSN Messenger

The initiating thread (from January) had some suggestions. In particular, some
code that does this from Rob Hetland:
<http://www.nabble.com/Re%3A-Cross-hair-and-polygon-drawing-tools.-p14919733.html>

Hope that helps,
Jose

···

On Friday 14 March 2008 16:44:54 Chiara Caronna wrote:

I tried ds9 and It looks like this is what I would like to do (though I
couldn't try funtools, but what you describe is good). DO you think it is
possible to make something like this with matplotlib? Thanks a lot for your

Note that numdisplay can display numpy arrays directly to ds9 without saving to a file. (http://stsdas.stsci.edu/numdisplay).

Perry

···

On Mar 14, 2008, at 12:44 PM, Chiara Caronna wrote:

Hi,
I tried ds9 and It looks like this is what I would like to do (though I couldn't try funtools, but what you describe is good). DO you think it is possible to make something like this with matplotlib?
Thanks a lot for your help,
Chiara

> Date: Wed, 12 Mar 2008 02:55:55 -0400
> From: lee.j.joon@...287...
> To: chiaracaronna@...32...
> Subject: Re: [Matplotlib-users] Polygon masking possible?
> CC: matplotlib-users@lists.sourceforge.net
>
> Hi,
>
> I often do this with ds9 and funtools.
> ds9 is an astronomy-oriented image viewer (http://hea-www.harvard.edu/RD/ds9/)
> but you can also use it with numpy array.
> Within ds9, you can define regions (ellipse, polynomial, etc) easily
> with a mouse.
> After you define a region (and save it as a file), you can convert it
> to a mask image
> with funtools (funtools is a name of an astronomy-oriented image
> utility pacakge).
> funtools only support fits file (image format in astronomy) so this
> can be a bit tricky, but if you're
> interested i'll send my python wrapper code for it.
>
> So, take a look at ds9 and see it fits your need.
> To view numpy array in ds9,
> *. From python, save the array as a file (tofile method, better use
> "arr" as an extension)
> * in ds9, file-> open others -> open array. You need to select
> array dimension, type and endianness of the array.
>
> Regards,
>
> -JJ
>
> On Sat, Mar 8, 2008 at 11:17 AM, Chiara Caronna > > <chiaracaronna@...32...> wrote:
> >
> > Hello,
> > I am also interested in masking polygons and defining the polygon by
> > 'clicking' on the image... but I do not know anything about GUI.... does
> > anyone can help? Is there already something implemented?
> > Thanks!
> > Chiara
> >
> > > Date: Wed, 23 Jan 2008 13:50:15 +1300
> > > From: amcmorl@...287...
> > > To: matplotlib-users@lists.sourceforge.net
> > > Subject: Re: [Matplotlib-users] Polygon masking possible?
> >
> > >
> > > Hi Søren,
> > >
> > > I've put this back on the list in case it's useful to anyone else, or
> > > if there are better suggestions or improvements around. Hope you don't
> > > mind.
> > >
> > > On 22/01/2008, Søren Nielsen <soren.skou.nielsen@...287...> > wrote:
> > > > Yeah i'd like to see your code if I can..
> > >
> > > import numpy as n
> > >
> > > def get_poly_pts(x, y, shape):
> > > """Creates convex polygon mask from list of corners.
> > >
> > > Parameters
> > > ----------
> > > x : array_like
> > > x co-ordinates of corners
> > > y : array_like
> > > y co-ordinates of corners, in order corresponding to x
> > > shape : array_like
> > > dimension sizes of result
> > >
> > > Returns
> > > -------
> > > build : ndarray
> > > 2-D array of shape shape with values True inside polygon
> > >
> > > Notes
> > > -----
> > > Code is constrained to convex polygons by "inside"
> > > assessment criterion.
> > >
> > > """
> > > x = n.asarray(x)
> > > y = n.asarray(y)
> > > shape = n.asarray(shape)
> > > npts = x.size # should probably assert x.size == y.size
> > > inds = n.indices( shape )
> > > xs = inds[0]
> > > ys = inds[1]
> > > xav = n.round(x.mean()).astype(int)
> > > yav = n.round(y.mean()).astype(int)
> > > for i in xrange(npts): # iterate over pairs of co-ordinates
> > > j = (i + 1) % npts
> > > m = (y[j] - y[i])/(x[j] - x[i])
> > > c = (x[j] * y[i] - x[i] * y[j])/(x[j] - x[i])
> > > thisone = ( ys > m * xs + c )
> > > if thisone[xav, yav] == False:
> > > thisone = ~thisone
> > > if i == 0:
> > > build = thisone
> > > else:
> > > build &= thisone
> > > return build
> > >
> > > (released under BSD licence)
> > >
> > > > I just needed the push over the edge to know how to draw on the canvas,
> > > > mapping clicks etc. since i'm still fairly new to matplotlib, so I think
> > > > your code will be helpfull.
> > >
> > > I hope so. As you can see this code doesn't do any of the drawing or
> > > click collecting, but the cookbook page should be able to guide you
> > > there. Ask again on the list if you have any further questions and
> > > we'll see if we can help.
> > >
> > > Also, the code assumes that the average co-ordinate is inside the
> > > shape - that's true for convex polygons, but not necessarily for
> > > arbitrary ones. I use if after taking a convex hull of a greater list
> > > of points (using the delaunay module in scipy (now in scikits, I
> > > hear)), which ensures convexity. You just need to be aware of that
> > > limitation.
> > >
> > > Cheers,
> > >
> > > A.
> > > --
> > > AJC McMorland, PhD candidate
> > > Physiology, University of Auckland
> > >
> > > -------------------------------------------------------------------------
> > > This SF.net email is sponsored by: Microsoft
> > > Defy all challenges. Microsoft(R) Visual Studio 2008.
> > > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
> > > _______________________________________________
> > > Matplotlib-users mailing list
> > > Matplotlib-users@lists.sourceforge.net
> > > https://lists.sourceforge.net/lists/listinfo/matplotlib-users
> >
> > ________________________________
> > Express yourself instantly with MSN Messenger! MSN Messenger
> > -------------------------------------------------------------------------
> > This SF.net email is sponsored by: Microsoft
> > Defy all challenges. Microsoft(R) Visual Studio 2008.
> > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
> > _______________________________________________
> > Matplotlib-users mailing list
> > Matplotlib-users@lists.sourceforge.net
> > https://lists.sourceforge.net/lists/listinfo/matplotlib-users
> >

Express yourself instantly with MSN Messenger! MSN Messenger
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

I am not sure how should I use it.... any hints?

···

----------------------------------------

From: jgomezdans@...287...
To: matplotlib-users@lists.sourceforge.net
Subject: Re: [Matplotlib-users] Polygon masking possible?
Date: Fri, 14 Mar 2008 18:03:13 +0000
CC: chiaracaronna@...32...

On Friday 14 March 2008 16:44:54 Chiara Caronna wrote:

I tried ds9 and It looks like this is what I would like to do (though I
couldn't try funtools, but what you describe is good). DO you think it is
possible to make something like this with matplotlib? Thanks a lot for your

The initiating thread (from January) had some suggestions. In particular, some
code that does this from Rob Hetland:

Hope that helps,
Jose

_________________________________________________________________
Discover the new Windows Vista
http://search.msn.com/results.aspx?q=windows+vista&mkt=en-US&form=QBRE