FluxML / DataAugmentation.jl

Flexible data augmentation library for machine and deep learning
https://fluxml.ai/DataAugmentation.jl/dev/
MIT License
41 stars 17 forks source link

AdjustBrightness and AdjustContrast without clamp01 #76

Closed itan1 closed 1 year ago

itan1 commented 1 year ago

Currently the output images are clamped with clamp01 in AdjustBrightness (here) and AdjustContrast (here).

I am working with medical data with a different grayscale range than [0,1] so I would like to use the augmentation methods but without clamping. Was there a specific reason to use the clamping? Can I add an optional clamp flag like below?

function adjustcontrast!(dst::AbstractArray{U}, img::AbstractArray{T}, factor; clamp=true) where {T, U}
    map!(dst, img) do x
        convert(U, clamp ? clamp01(x * factor) : x * factor)
    end
end
lorenzoh commented 1 year ago

The reason for the default clamping is that the default low-memory color type is RGB{N0f8} which is represented under the hood as UInt8s. When trying to convert a number outside the inveral [0, 1] back to that representation, an error is thrown.

Happy to accept a PR adding the clamp = true flag for AdjustBrightness and AdjustContrast :)