astropy / imageutils

Image processing utilities for Astropy (No longer maintained)
https://imageutils.readthedocs.org/
9 stars 17 forks source link

Add crop = cutout function #4

Closed cdeil closed 9 years ago

cdeil commented 10 years ago

One of the basic image processing functions is crop, a.k.a. cutout.

API proposal:

def crop(data, wcs, bounding_box):
    data_new = # a view, not a copy
    wcs_new = # copy and modify the input wcs
    return data_new, wcs_new

Existing implementations (please add links if there is other code that might be useful):

To be discussed:

astrofrog commented 10 years ago

Well I can see different ways to specify the region to cutout. For starters, one might want to accept a SkyCoord and a radius. Or give a range of pixels in x and y. I guess there's not too many options. What API are you using in gammapy?

cdeil commented 10 years ago

My idea in Gammapy was to have the crop_image function only accept a BoundingBox object which simply represents a slice in pixel coordinates and then there could be various ways to create a BoundingBox.

I think a BoundingBox class would be a useful addition to imageutils (and then also be used in photutils or gammapy), because it's needed as input or internally for many methods. But I'm not 100% sure it's a good idea ... @astrofrog what do you think?

cdeil commented 10 years ago

PS: the Gammapy imageutils functions and classes are mostly crap and unfinished / untested, so don't look at them too closely ... the idea is to find a good API and then implement it well here in astropy.image and remove the versions from Gammapy.

crawfordsm commented 10 years ago

As another example, there's also the trim_image function in ccdproc: https://github.com/astropy/ccdproc/blob/master/ccdproc/core.py#L230

This doesn't have the WCS slicing and worries about FITS header format, which I am not sure if it is needed here, but it depends on how general the task should be.

cdeil commented 9 years ago

@larrybradley @astrofrog What do you think about introducing a ImageBoundingBox or ImageSlice or ImageCutout class in astropy.image and then using it in photutils, gammapy, ccdproc, ...

At the moment slices are mostly used internally and not part of the API and something definitely needs to be done about #40 ... we can't leave these slice helper functions as is.

larrybradley commented 9 years ago

@cdeil This could be useful if we can find a good API. Perhaps a simple ImageCutout class that takes data and x0, x1, y0, y1 as input and have a slice tuple, bounding box, and center pixel, etc. as object properties?

cdeil commented 9 years ago

@larrybradley – As I said ... I don't know what a good solution is here even after discussing this for an hour with @adonath ... I'd have to write down simple examples with the various use cases first to see what works best.

The smallest useful thing would be to have an NDSlice class that has a Python list of slice objects as a data member and provides convenience functions (e.g. create it from a center position) and attributes.

For other use cases it would be better if the class has a reference to the image and sometimes also a reference to a kernel ... probably these could be optional and one could have a single class that is useful to implement a lot of image processing stuff e.g. in modeling and photutils and gammapy where one operates on image cutouts for speed.

So +10 if you can make a concrete proposal ... I'm very interested and will try it out for my use cases.

keflavich commented 9 years ago

Over the past few days, I've had a need for this functionality on 3 separate occasions. So, big :+1:.

cdeil commented 9 years ago

@larrybradley has implemented this in https://github.com/astropy/astropy/pull/3823 . Closing this issue so that discussion can continue there ...