Skip to content

Conversation

d-v-b
Copy link
Contributor

@d-v-b d-v-b commented Jan 27, 2015

This addition adds the ability to get the set of (x,y) points inside a polygon drawn in an instance of lightning.image

To test this new functionality, draw a region on an image and call the following:

# get set of pixels from image
coords = viz.get_coords(as_points=True)[0]
# build an array with the same size in (x,y) as the image data
tmp = np.zeros(dims[0:2])

# for each pixel, color the image at that point.
for pix in coords:
        tmp[pix[0],pix[1]] = 1

# the resulting binary image should match the shape and position of the drawn polygon
plt.imshow(tmp)

Copy link
Member

Choose a reason for hiding this comment

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

tolist() is not in place, make sure to call it on the object that will be returned.

@freeman-lab
Copy link
Member

@d-v-b this looks great! I think it would be nice to add one more output type, a mask for each region, of 0s and 1s with 1s where the region was defined. As we'll then have three options, I would suggest the following change to the name / function definition:

def get_regions(self, return_type='bounds', dims=None):

Where the three options are bounds, points, and mask. Then the core of the method would look like:

    coords = user_data['coords']
    if return_type == 'bounds':
        return coords
    if return_type == 'points':
        return [get_points_from_polygon(x) for x in coords]
    else if return_type == 'mask':
        if not dims:
            raise Exception('Must provide dimensions to return masks')
        # call function to generate a mask on each region
    else:
        raise Exception('Option %s not supported' % return_type)

Want to give this a try?

@d-v-b
Copy link
Contributor Author

d-v-b commented Jan 27, 2015

@freeman-lab yep, I can work those suggestions in. Expect something along those lines in a few hours

@d-v-b
Copy link
Contributor Author

d-v-b commented Jan 27, 2015

@freeman-lab:

A list of boundary points for each polygon are returned when get_coords is called like this:
bounds = viz.get_coords()

Given dims, the dimensions of the image containing the polygons, a list of binary masks formed by each polygon is returned when get_coords is called like this:
mask = viz.get_coords(return_type='mask',dims=dims)

A list of lists of points internal to each polygon is returned when get_coords is called like this:
pts = viz.get_coords(return_type='points',dims=dims)

The argument dims is required for both inner points and masks because the masks are generated by calling the function bounds_to_mask(bounds,mask) on the bounds, and the points are subsequently generated by calling 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

@freeman-lab
Copy link
Member

@d-v-b looking very close! Made a couple formatting suggestions, only other thing is that I'd rather use the get_points_from_polygon from before instead of mask_to_points(polygon_to_mask()), becuase the earlier one didn't require knowing the dims. Normally I'd prefer the modularity and consistency of your approach, but I think the ability to get points without providing the dimensions is actually nicer to have, and you need an extra function in either case.

@d-v-b
Copy link
Contributor Author

d-v-b commented Jan 27, 2015

@freeman-lab now get_coords(return_type='points') does not require dims as an argument, thanks to the addition of a function polygon_to_points

for consistency in naming, I also re-named the mask-generating function to polygon_to_mask.

@freeman-lab
Copy link
Member

@d-v-b Terrific, LGTM, really nice work! Merging it in.

freeman-lab added a commit that referenced this pull request Jan 27, 2015
Add addition output options for getting coordinates
@freeman-lab freeman-lab merged commit a446bf9 into lightning-viz:master Jan 27, 2015
@d-v-b d-v-b deleted the coords-opts branch January 28, 2015 00:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants