Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
get_coords now optionally returns mask or vector of points
  • Loading branch information
d-v-b committed Jan 27, 2015
commit ddf70c19e0f2fc47bc0f11a50c630cf94d098958
31 changes: 25 additions & 6 deletions lightning/types/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from lightning.types.base import Base
from lightning.types.decorators import viztype
from lightning.types.utils import array_to_im, get_points_from_polygon
from lightning.types.utils import array_to_im, mask_to_points, polygon_to_mask


@viztype
Expand All @@ -28,14 +28,33 @@ def clean(imagedata):

return {'images': outdict}

def get_coords(self, as_points=False):
def get_coords(self, return_type='bounds', dims=None):
"""
Get data from polygons drawn on image.

:param return_type:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow arg formatting of other functions, it should look like:

Parameters
----------
return_type : str
    <Explanation of parameter and its options>
dims : tuple, list, or array, optional, default=None
    <Explanation of parameters>

:return:
"""

user_data = self.get_user_data()['settings']
if 'coords' in user_data.keys():
if 'coords' in user_data.keys():
coords = user_data['coords']
if as_points:
return [get_points_from_polygon(x) for x in coords]

if return_type == 'bounds':
return coords

elif return_type == 'mask':
if not dims:
raise Exception('Must provide image dimensions to return mask')
return [polygon_to_mask(x, dims) for x in coords]

elif return_type == 'points':
if not dims:
raise Exception('Must provide image dimensions to return inner points')
return [mask_to_points(polygon_to_mask(x, dims)) for x in coords]

else:
return coords
raise Exception('Option %s is not supported' % return_type)
else:
return []

Expand Down
36 changes: 22 additions & 14 deletions lightning/types/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,25 +208,33 @@ def list_to_regions(reg):
raise Exception("All region names must be two letters (for US) or three letters (for world)")
return reg

def get_points_from_polygon(coords):

def polygon_to_mask(coords, dims):

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove line break

"""
Given a list of pairs of points which define a polygon, return a list of points interior to the polygon
Given a list of pairs of points which define a polygon, return a binary
mask covering the interior of the polygon
"""

bounds = array(coords).astype('int')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace array with asarray

path = Path(bounds)

bMax = bounds.max(0)
bMin = bounds.min(0)
grid = meshgrid(range(dims[1]), range(dims[0]))
grid_flat = zip(grid[0].ravel(), grid[1].ravel())

path = Path(bounds)
mask = path.contains_points(grid_flat).reshape(dims[0:2]).astype('int')

grid = meshgrid(range(bMin[0],bMax[0]+1),range(bMin[1],bMax[1]+1))
return mask

gridFlat = zip(grid[0].ravel(),grid[1].ravel())

inside = path.contains_points(gridFlat).reshape(grid[0].shape).astype('int')
inside = where(inside)
inside = vstack([inside[0],inside[1]]).T + bMin[-1::-1]
inside.tolist()

return inside
def mask_to_points(mask):

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove line break

"""
Convert binary mask to a list of (y,x) points for each point where the mask is 1
"""

points = where(mask)
points = vstack([points[0], points[1]]).T
points = points.tolist()

return points