-
Notifications
You must be signed in to change notification settings - Fork 5
Introduces AbstractIntensityAdjustmentAlgorithm #50
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
Open
zygmuntszpak
wants to merge
1
commit into
master
Choose a base branch
from
adjust_intensity
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| # usage example for package developer: | ||
| # | ||
| # import ContrastAdjustmentAPI: AbstractIntensityAdjustmentAlgorithm, | ||
| # adjust_intensity, adjust_intensity! | ||
|
|
||
| """ | ||
| AbstractIntensityAdjustmentAlgorithm <: AbstractImageFilter | ||
|
|
||
| A root type for `ImageContrastAdjustment` package that relates to algorithms | ||
| that manipulate contrast without operating on intensity histograms. | ||
|
|
||
| Any concrete intensity adjustment algorithm shall subtype it to support | ||
| [`adjust_intensity`](@ref) and [`adjust_intensity!`](@ref) APIs. | ||
|
|
||
| # Examples | ||
|
|
||
| All intensity adjustment algorithms in ImageContrastAdjustment are called in the | ||
| following pattern: | ||
|
|
||
| ```julia | ||
| # first generate an algorithm instance | ||
| f = LinearStretching() | ||
|
|
||
| # then pass the algorithm to `adjust_intensity` | ||
| img_adjusted = adjust_intensity(img, f) | ||
|
|
||
| # or use in-place version `adjust_intensity!` | ||
| img_adjusted = similar(img) | ||
| adjust_intensity!(img_adjusted, img, f) | ||
| ``` | ||
|
|
||
| Some algorithms also receive additional information as an argument, | ||
| e.g., `gamma` of `GammaCorrection`. | ||
|
|
||
| ```julia | ||
| # you can explicit specify the parameters | ||
| f = GammaCorrection(gamma = 1.4) | ||
| ``` | ||
|
|
||
| For more examples, please check [`adjust_intensity`](@ref), | ||
| [`adjust_intensity!`](@ref) and concrete algorithms. | ||
| """ | ||
| abstract type AbstractIntensityAdjustmentAlgorithm <: AbstractImageFilter end | ||
|
|
||
| adjust_intensity!(out::Union{GenericGrayImage, AbstractArray{<:Color3}}, | ||
| img, | ||
| f::AbstractIntensityAdjustmentAlgorithm, | ||
| args...; kwargs...) = | ||
| f(out, img, args...; kwargs...) | ||
|
|
||
| # TODO: Relax this to all color types | ||
| function adjust_intensity!(img::Union{GenericGrayImage, AbstractArray{<:Color3}}, | ||
| f::AbstractIntensityAdjustmentAlgorithm, | ||
| args...; kwargs...) | ||
| tmp = copy(img) | ||
| f(img, tmp, args...; kwargs...) | ||
| return img | ||
| end | ||
|
|
||
| function adjust_intensity(::Type{T}, | ||
| img, | ||
| f::AbstractIntensityAdjustmentAlgorithm, | ||
| args...; kwargs...) where T | ||
| out = similar(Array{T}, axes(img)) | ||
| adjust_intensity!(out, img, f, args...; kwargs...) | ||
| return out | ||
| end | ||
|
|
||
| adjust_intensity(img::AbstractArray{T}, | ||
| f::AbstractIntensityAdjustmentAlgorithm, | ||
| args...; kwargs...) where T <: Colorant = | ||
| adjust_intensity(T, img, f, args...; kwargs...) | ||
|
|
||
| # Do not promote Number to Gray{<:Number} | ||
| adjust_intensity(img::AbstractArray{T}, | ||
| f::AbstractIntensityAdjustmentAlgorithm, | ||
| args...; kwargs...) where T <: Number = | ||
| adjust_intensity(T, img, f, args...; kwargs...) | ||
|
|
||
|
|
||
| # Handle instance where the input is a sequence of images. | ||
| adjust_intensity!(out_sequence::Vector{T}, | ||
| img_sequence, | ||
| f::AbstractIntensityAdjustmentAlgorithm, | ||
| args...; kwargs...) where T <: Union{GenericGrayImage, AbstractArray{<:Color3}} = | ||
| f(out_sequence, img_sequence, args...; kwargs...) | ||
|
|
||
| # TODO: Relax this to all color types | ||
| function adjust_intensity!(img_sequence::Vector{T}, | ||
| f::AbstractIntensityAdjustmentAlgorithm, | ||
| args...; kwargs...) where T <: Union{GenericGrayImage, AbstractArray{<:Color3}} | ||
| tmp = copy(img_sequence) | ||
| f(img_sequence, tmp, args...; kwargs...) | ||
| return img_sequence | ||
| end | ||
|
|
||
| function adjust_intensity(::Type{T}, | ||
| img_sequence::Vector{<:AbstractArray}, | ||
| f::AbstractIntensityAdjustmentAlgorithm, | ||
| args...; kwargs...) where T | ||
| N = length(img_sequence) | ||
| out_sequence = [similar(Array{T}, axes(img_sequence[n])) for n = 1:N] | ||
| adjust_intensity!(out_sequence, img_sequence, f, args...; kwargs...) | ||
| return out_sequence | ||
| end | ||
|
|
||
| adjust_intensity(img_sequence::Vector{<:AbstractArray{T}}, | ||
| f::AbstractIntensityAdjustmentAlgorithm, | ||
| args...; kwargs...) where T <: Colorant = | ||
| adjust_intensity(T, img_sequence, f, args...; kwargs...) | ||
|
|
||
| # Do not promote Number to Gray{<:Number} | ||
| adjust_intensity(img_sequence::Vector{<:AbstractArray{T}}, | ||
| f::AbstractIntensityAdjustmentAlgorithm, | ||
| args...; kwargs...) where T <: Number = | ||
| adjust_intensity(T, img_sequence, f, args...; kwargs...) | ||
|
|
||
| ### Docstrings | ||
|
|
||
| """ | ||
| adjust_intensity!([out,] img, f::AbstractIntensityAdjustmentAlgorithm, args...; kwargs...) | ||
|
|
||
| Adjust intensity of `img` using algorithm `f`. | ||
|
|
||
| # Output | ||
|
|
||
| If `out` is specified, it will be changed in place. Otherwise `img` will be changed in place. | ||
|
|
||
| # Examples | ||
|
|
||
| Just simply pass an algorithm to `adjust_intensity!`: | ||
|
|
||
| ```julia | ||
| img_adjusted = similar(img) | ||
| adjust_intensity!(img_adjusted, img, f) | ||
| ``` | ||
|
|
||
| For cases you just want to change `img` in place, you don't necessarily need to manually | ||
| allocate `img_adjusted`; just use the convenient method: | ||
|
|
||
| ```julia | ||
| adjust_intensity!(img, f) | ||
| ``` | ||
|
|
||
| See also: [`adjust_intensity`](@ref) | ||
| """ | ||
| adjust_intensity! | ||
|
|
||
| """ | ||
| adjust_intensity([T::Type,] img, f::AbstractIntensityAdjustmentAlgorithm, args...; kwargs...) | ||
|
|
||
| Adjust intensity of `img` using algorithm `f`. | ||
|
|
||
| # Output | ||
|
|
||
| The return image `img_adjusted` is an `Array{T}`. | ||
|
|
||
| If `T` is not specified, then it's inferred. | ||
| # Examples | ||
|
|
||
| Just simply pass the input image and algorithm to `adjust_intensity` | ||
|
|
||
| ```julia | ||
| img_adjusted = adjust_intensity(img, f) | ||
| ``` | ||
|
|
||
| This reads as "`adjust_intensity` of image `img` using algorithm `f`". | ||
|
|
||
| You can also explicitly specify the return type: | ||
|
|
||
| ```julia | ||
| img_adjusted_float32 = adjust_intensity(Gray{Float32}, img, f) | ||
| ``` | ||
|
|
||
| See also [`adjust_intensity!`](@ref) for in-place intensity adjustment. | ||
| """ | ||
| adjust_intensity | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Another choice is to make it
and so that we don't need to deprecate the old usages. We can just incrementally add new
adjust_intensitymethods with defaultalgto beLinearStretching. This means that bothadjust_histogramandadjust_intensitywould work forAbstractIntensityAdjustmentAlgorithmtypes.How do you think?
Uh oh!
There was an error while loading. Please reload this page.
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 that making
goes against the spirit of the issue #32 which seems to be specifically about the fact that operations that don't manipulate the histogram ought not to be conceptualised as histogram adjustment algorithms.
I reckon let's just go ahead and make the deprecations before we embed this all in the Images ecosystem.
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.
My argument is that not all histogram adjustment algorithm needs to explicitly construct a histogram. Directly adjust intensity can be considered as an implicit way to scale the x scale of the intensity histogram.
My personal understanding of #32 is that we just need a default algorithm for easier and more convenient usage to close #32. For generic histogram adjustment, there isn't a consensus default algorithm, but for intensity adjustment, we can default to linear stretching.
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.
That was also my argument and the reason why I implemented it the way I did in the first place :D, but @stillyslalom seems to disagree with our view here.
I'm torn here. If we go this route, then what's the benefit of introducing the
adjust_intensityfunction? Is it effectively not going to just be an alias foradjust_histogram?I guess one distinction is that
AbstractIntensityAdjustmentAlgorithmis typically embarrassingly parallelisable, whereas some of the histogram-based algorithms may not be.I was planning to introduce some convenience functions such as
span_contrastto address issues such as #27Uh oh!
There was an error while loading. Please reload this page.
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 all we need here is to introduce a convenient function
span_contrastbacked byLinearStretchingto save some typings and easier to remember without checking the documentation. Changing the type hierarchy doesn't make things easier.In the meantime, we can generalize
LinearStretchingand solves #33There 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 don't know if I agree with this argument. We don't call
fill!adjust_varianceeven though it does that.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.
Thanks for sharing your thoughts. What is your suggestion regarding the type hierarchy? Should AbstractIntensityAdjustmentAlgorithn subtype the AbstractHistogramAdjustmentAlgorithm type?
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.
Vice versa, I'd say. The second sounds way more specific, the first is much more vague.