-
Notifications
You must be signed in to change notification settings - Fork 77
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
Optional gamma correction parameter #350
base: main
Are you sure you want to change the base?
Conversation
Thanks for the implementation. Can you say a bit more about the use case for this? Any reason why this can't be done offline (during preprocessing) or using |
We use terracotta to display images customers purchase at SkyFi. For true colour 3-band composites, rendering the data linearly does not provide a representation that is similar to what the human eye would perceive. Applying a gamma correction between 1.6 and 2.2, on an otherwise linear colour space makes for something that is a lot closer to a “natural colour composite”. We also want the preview on the map to be as close as possible to the png previews we send customers along with the raster, to which gamma correction is applied. Regarding the option of doing it offline, we would like to offer users the ability to adjust min/max and gamma factor on the fly. The I also think gamma correction would be a good addition to the capabilities of terracotta since it’s a standard and common processing technique, and its implementation doesn’t really affect any other functionality. |
FYI in rio-tiler/titiler we use https://github.com/vincentsarago/color-operations (fork of mapbox/rio-color), Just sharing if you want to support more |
Thanks @vincentsarago! I like this a lot, and a feature like this has been on the roadmap for a long time: #23. I'd strongly prefer an implementation that doesn't assume a specific color transformation like gamma, and instead implements support for a range of transforms (like the ones provided via |
@vincentsarago @dionhaefner To be honest, I also used However, I added Some notes:
|
Thanks @biserhong ! I'm hesitant to introduce features that complicate the API for a singular use case like gamma correction. I can see how this is probably fine for the few most important use cases, but inevitably leads to a kitchen sink of parameters over time that becomes a nightmare to clean up / refactor later on. A for me much more sustainable design would be to introduce a parameter like |
It would work for me. I'll have to check what each of the color transforms does exactly and it might turn out that for some transformation we might not be able to use the precomputed percentiles (it might also be ok, I just don't know). Are you ok with that? @vincentsarago do you have any input on this? |
Percentiles are stable under any monotonous map, so we should be fine for all operations that are applied band-wise. In other cases, I think it's okay to be slightly off – the practical impact of that should be small (of course it should be documented explicitly that the given percentiles relate to untransformed data). |
Sorry this took so long, I had to focus on some other work for a bit. @dionhaefner Can you take a look at the latest commit and let me know if you agree with the approach? I'll clean up the rest of the PR and tidy up the code and variables if we agree to continue this way. |
for func in parse_operations(color_transform): | ||
arr = func(arr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this assume a particular scaling already?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, inputs have to be in the [0,1] range: https://github.com/vincentsarago/color-operations/?tab=readme-ov-file#color_operationsoperations
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. It may be easier to apply stretching before color transform then. I.e., normalize to [0,1]
using stretch_range
and clamp spillover to 0
/ 1
, then apply color transform, then convert to uint8
. That way we don't rely on a transformation of the stretch range?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it may not be a good idea since it would change the result.
The color stretch fills the [0, 1] range whereas the normalization to_math_type
does is relative to the dtype range. So when you apply the color transform I think that would shift where values fall along the curve.
This is what chatgpt said in the case of gamma correction:
Gamma correction is typically applied before color stretching, particularly when working with image data in scientific imaging, photography, or graphic design.
Here's why:
Gamma Correction First: Gamma correction adjusts the image data to a linear color space, which compensates for the nonlinear response of human vision and many display systems. This linearization step ensures that subsequent adjustments, like color stretching, are applied to data that more accurately represents real-world light intensities.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, but who says that the dtype range is appropriate? The user-provided stretch range is telling us how to map the values in the raster to a linear scale.
Summoning @vincentsarago in case you want to weigh in on the appropriate order of linear scaling (and clamping out of range values) vs. color correction :)
Awesome @biserhong! I like this a lot, especially how we specify stretch ranges in untransformed space which should work even with multi-band transforms. I reckon there will be quite a bit of testing required to make sure scaling works as expected but from a UX perspective I really like it, so should be good to port to other endpoints as well. |
Thanks for the quick feedback @dionhaefner! I'll try to get something more complete this week. |
@dionhaefner I removed all references to the gamma factor and replaced it with the color transform. I excluded the saturation option for now because it's only valid for rgb and it's not monotonic. What do you think? I can proceed to tests if you agree with this approach. |
@@ -88,7 +90,8 @@ def get_band_future(band_key: str) -> Future: | |||
keys = (*some_keys, band_key) | |||
metadata = driver.get_metadata(keys) | |||
|
|||
band_stretch_range = list(metadata["range"]) | |||
band_range = list(metadata["range"]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why introduce this variable? Looks unused.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh now I see, you're using this further below. But beware, since you're setting this in a loop only the value for the last band will be used in the color transform!
missing=None, | ||
example="gamma 1 1.5, sigmoidal 1 15 0.5", | ||
description="Color transform DSL string from color-operations." | ||
"All color operations for singleband should specify band 1.", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what this means. Could you elaborate?
This PR adds gamma correction functionality to the
/singleband
and/rgb
endpoints. It is requested by adding an optionalgamma_factor
parameter.An important observation about enabling this functionality is that the exponential function preserves the relative percentile ranges in the [0, 1] interval. So the computed percentiles in the metadata can be carried through after gamma correcting a tile.