The Wayback Machine - https://web.archive.org/web/20220925152436/https://github.com/scikit-image/scikit-image/issues/5428
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utility function for cropping images from a bounding box #5428

Open
sofroniewn opened this issue Jun 16, 2021 · 4 comments
Open

Utility function for cropping images from a bounding box #5428

sofroniewn opened this issue Jun 16, 2021 · 4 comments

Comments

@sofroniewn
Copy link

sofroniewn commented Jun 16, 2021

Feature Request

This is a feature request for a utility function for cropping images from a bounding box.

Note this need comes up a lot, particularly for 2D images - see this discussion on image.sc for an example, including prototype code. It would be nice to have a full nD solution though. Possibly something that did broadcasting appropriately too (i.e. cropping a 3D image with a 2D rectangle broadcasts across the "missing" dimension). @jni suggested I open an issue here to track.

I'm thinking something sort of like the following but for the full nD case.

def create_box(data):
    """Create integer box

    Parameters
    ----------
    data : (N, 2) array
        Points around which the box is created.

    Returns
    -------
    box : (2, 2) array
        Integrer extrema of the box.
    """
    min_val = data.min(axis=0)
    max_val = data.max(axis=0)
    tl = np.array([min_val[0], min_val[1]])
    br = np.array([max_val[0], max_val[1]])
    box = np.round(np.array([tl, br])).astype(int)
    return box


def bounding_box_crop(image, rectangle):
    """Create integer box

    Parameters
    ----------
    image : (N, M) array
        2D image.
    rectangle : (4, 2) array or (2, 2) array
        Rectangle for cropping.

    Returns
    -------
    cropped_image : (U, V) array
        Cropped 2D image.
    """
    min_val, max_val = create_box(rectangle)
    return image[min_val[0]: max_val[0], min_val[1]: max_val[1]]

Note this is different from the existing https://scikit-image.org/docs/dev/api/skimage.util.html#skimage.util.crop which has a slightly different API.

A quick google reveals this is an often requested utility function so many people might benefit from it

@Alfa-Shashank
Copy link

Alfa-Shashank commented Aug 23, 2021

Hello guys, I would like to work on this, is this issue still open?

@tarun-bisht
Copy link

tarun-bisht commented Aug 23, 2021

Hi, I am intrested in solving this issue. Is this still open?

@grlee77
Copy link
Contributor

grlee77 commented Aug 23, 2021

@Alfa-Shashank , @tarun-bisht. Thank you for the interest, but there is a PR for this in progress at #5499.

In general, if you go to the "Pull requests" tab there is a search box where you can try to determine if a particular feature already has a pull request open. Since #5499 references this issue, you can also see it listed here (just before @Alfa-Shashank's comment).

@tarun-bisht
Copy link

tarun-bisht commented Aug 23, 2021

@grlee77 thanks for clarifying

@scikit-image scikit-image locked and limited conversation to collaborators Oct 18, 2021
@grlee77 grlee77 reopened this Feb 20, 2022
@scikit-image scikit-image unlocked this conversation Feb 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants