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

_stretch_im not working as intended #743

Open
nkorinek opened this issue Nov 3, 2021 · 4 comments
Open

_stretch_im not working as intended #743

nkorinek opened this issue Nov 3, 2021 · 4 comments
Labels
enhancement New feature or request

Comments

@nkorinek
Copy link
Member

nkorinek commented Nov 3, 2021

When using the stretch parameter of earthpy's plot_rgb function, the resulting image is not what is expected. It's supposed to be the original array with the values stretched to show contrast better, but instead it's an array from 0-1 of very odd values. I think the function is returning the scalar (I'm pretty sure) and we need to instead multiply the return of that function by the original array. Going from this:

    s_min = str_clip
    s_max = 100 - str_clip
    arr_rescaled = np.zeros_like(arr)
    for ii, band in enumerate(arr):
        lower, upper = np.nanpercentile(band, (s_min, s_max))
        arr_rescaled[ii] = exposure.rescale_intensity(
            band, in_range=(lower, upper)
        )
    return arr_rescaled.copy()

to this:

    s_min = str_clip
    s_max = 100 - str_clip
    arr_rescaled = np.zeros_like(arr)
    for ii, band in enumerate(arr):
        lower, upper = np.nanpercentile(band, (s_min, s_max))
        arr_rescaled[ii] = exposure.rescale_intensity(
            band, in_range=(lower, upper)
        )
    return arr*arr_rescaled.copy()

Testing seems to show this working

@nkorinek
Copy link
Member Author

nkorinek commented Nov 3, 2021

Also in trouble shooting realizing our tests involving the stretch image function need to be updated! They don't test for what the parameter actually does, unless I'm misreading them. I think this is why the faulty stretch never tripped any errors.

@nkorinek
Copy link
Member Author

nkorinek commented Nov 3, 2021

https://gist.github.com/nkorinek/e566788685c453174d6920829d4b5829 gist demonstrating the issue better then I described it @lwasser

@lwasser
Copy link

lwasser commented Nov 4, 2021

just a note on this from the matplotlib docs:. Note below that MPL accepts either 0-255 as an int format or 0-1 as a float. this is fairly standard. Often images are stored as int and the data are scaled. this is because storing ints is less space (smaller file size) vs storing floats. so either is acceptable. You get into trouble when you try to give mpl floats that have values beyond 0-1 or 0-255 as it struggles to map the values the way you'd expect to a colormap object. More below. So i wouldn't worry about the 0-1 values as much as the image stretch looking icky.

We should consider adding (in the future as an enhancement) different types of stretch that may better account for issues in image intensity across bands. linear will not fix all images visually the way we want it to.

i do see some other issues with plot_rgb that are bugs (adam's) that we may want to prioritize before this one UNLESS you do really see a bug in the code. i do like the ideas we discussed of updating the lesson a bit to explain why that image looks so funky and perhaps adding an image that stretches a bit more nicely could be good too (or link to the NAIP lesson that shows this nicely there).

Xarray-like or PIL image

    The image data. Supported array shapes are:

        (M, N): an image with scalar data. The values are mapped to colors using normalization and a colormap. See parameters norm, cmap, vmin, vmax.
        (M, N, 3): an image with RGB values (0-1 float or 0-255 int).
        (M, N, 4): an image with RGBA values (0-1 float or 0-255 int), i.e. including transparency.

    The first two dimensions (M, N) define the rows and columns of the image.

    Out-of-range RGB(A) values are clipped.

@lwasser lwasser added the enhancement New feature or request label Nov 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants