Skip to content

Comments

fix show function#3171

Merged
sgillies merged 7 commits intorasterio:mainfrom
xinluo2018:main
Mar 20, 2025
Merged

fix show function#3171
sgillies merged 7 commits intorasterio:mainfrom
xinluo2018:main

Conversation

@xinluo2018
Copy link
Contributor

we found the show function has some problems in ploting the rgb image, thus we fixed it.

@sgillies
Copy link
Member

sgillies commented Sep 9, 2024

@xinluo2018 can you explain what the problems are? Otherwise, I don't know if this work solves them.

@xinluo2018
Copy link
Contributor Author

ok, i found the function show() couldn't plot rgb image if the color interpretation information is missing in the image, then we fixed it that is, when the color information is missing for multi-bands image, the first three bands are regarded the RGB bands, like the qgis software.
moreover, we also added a new feature of histogram stretching for better image visualization, the previous feature of adjust_band() is included in our new function hist_strech(), therefore we removed the function of adjust_band() in the script.

rasterio/plot.py Outdated

except KeyError:
arr = source.read(1, masked=True)
arr = source.read((3,2,1), masked=True)
Copy link
Member

Choose a reason for hiding this comment

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

why do you assume that bands are store as BGR ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is from our experience in remote sensing application, that is, if the remote sensing image contains multiple bands, the bands are usually stored by ascending order of the wavelength of bands. The BGR wavelengths are: ~460nm, ~530nm, ~580nm, therefore for a multibands image we set the default index (3,2,1) for the color composition, thus mostly the color composition is the true color composition rather than by setting the default index of (1,2,3).
moreover, if the image bands are not stored as BGR, our revision also at least show the multibands image as a color composite image, while the previous script only show the multibands image as a grey image.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we add a new feature for image histogram stretching, this feature is used by an optional parameter for users, which will make the image visualization better. i frequently use it for image visualization in my experiment.

Copy link
Member

Choose a reason for hiding this comment

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

This is from our experience in remote sensing application, that is, if the remote sensing image contains multiple bands, the bands are usually stored by ascending order of the wavelength of bands. The BGR wavelengths are: ~460nm, ~530nm, ~580nm, therefore for a multibands image we set the default index (3,2,1) for the color composition, thus mostly the color composition is the true color composition rather than by setting the default index of (1,2,3).

This is a pretty heavy assumption... which after 15years working with Remote Sensing data, I would not make myself ;-)

if the image bands are not stored as BGR, our revision also at least show the multibands image as a color composite image, while the previous script only show the multibands image as a grey image.

Which is why I think adding an indexes= options might be a better solution

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that is also ok, we add a new 'indexes=' options, and the user can specify the bands for color composite when visualize the multibands image. Hope you can check my code again, thanks.

@vincentsarago
Copy link
Member

I feel that this PR complexity the show function and make assumption about the data. Maybe adding an indexes option could solve your issues

@sgillies
Copy link
Member

Have you tried this out, @vincentsarago? Does it do what it advertises?

@xinluo2018 I will try this out, but first I need to make a 1.4.2 release.

@vincentsarago
Copy link
Member

@sgillies I did not

I'll try tomorrow

@xinluo2018
Copy link
Contributor Author

hi, how about the modified code, could you merge our modifications, that the new function could be used in the new release version of rasterio, thanks.

@sgillies
Copy link
Member

@xinluo2018 I am sorry for the delay and I apologize. I am in favor of this and have several suggestions to make.

Copy link
Member

@sgillies sgillies left a comment

Choose a reason for hiding this comment

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

@xinluo2018 thank you for the contribution! I have a couple of requests.

"""
arr_hist = np.nanpercentile(np.array(arr), (clip_percent, 100 - clip_percent))
arr = (arr - arr_hist[0]) / (arr_hist[1] - arr_hist[0])
arr = np.clip(arr, 0, 1)
Copy link
Member

Choose a reason for hiding this comment

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

Is it necessary to clip the array? Is it not ensured by the math on the line above?

Copy link
Contributor Author

@xinluo2018 xinluo2018 Mar 20, 2025

Choose a reason for hiding this comment

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

For rgb image show, it is not necessary, while for the single-band image show, clip the array is necessary for better visualization. the test sample can be found at: https://github.com/xinluo2018/rasterio/blob/test/test.ipynb.
We have made the related modifications by following your other suggestions.

Copy link
Member

@sgillies sgillies left a comment

Choose a reason for hiding this comment

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

Thank you @xinluo2018 !

@sgillies sgillies merged commit b593731 into rasterio:main Mar 20, 2025
21 of 22 checks passed
@sgillies sgillies added this to the 1.5.0 milestone Mar 20, 2025
@xinluo2018
Copy link
Contributor Author

happy to be 1 of 158 developers ^_^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants