-
Notifications
You must be signed in to change notification settings - Fork 143
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
Adds Gamma Correction Function #492
Conversation
|
||
""" | ||
|
||
RET_TYPE = Dict("8bit" => Gray{U8}, "16bit" => Gray{FixedPointNumbers.UFixed{UInt16,16}}) |
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.
Any time you pass type information via a string, you're going to have type-instability. I let this slide on the histogram equalization PR, but we should get away from that pattern ASAP. It's much better to pass the type as a type.
what are we going to do with |
6f3100e
to
8ec10eb
Compare
@timholy I have made the function pixel-wise and the code has reduced by a lot (just a line for color images, the chain of functions is a bit long though). Like |
8ec10eb
to
044ec95
Compare
function adjust_gamma{T<:FixedPointNumbers.UFixed, D<:Union{U8, U16}}(img::AbstractArray{Gray{T}}, gamma::Number, dtype::Type{D} = U8) | ||
raw_type = FixedPointNumbers.rawtype(dtype) | ||
gamma_inv = 1.0 / gamma | ||
table = [dtype((i / typemax(raw_type)) ^ gamma_inv) for i in 0:typemax(raw_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.
This line is one source of type instability. It can be replicated with
The type instability can be replicated with
```jl
function prep_table{D}(::Type{D}, gamma_inv)
raw_type = FixedPointNumbers.rawtype(D)
[D((i / typemax(raw_type)) ^ gamma_inv) for i in zero(raw_type):typemax(raw_type)]
end
But put a D
in front of the comprehension, and suddenly inference is happier. Probably JuliaLang/julia#15276.
If you're interested in pursuing this further, it would be good to address @bjarthur's question too. |
@bjarthur We could have a function |
044ec95
to
0a78d8a
Compare
Made a lot of changes, simplified |
There is still some type instability issue with julia> function foo(i)
i*i
end
foo (generic function with 1 method)
julia> img = reshape(1:1:100, 10, 10)
10×10 Base.ReshapedArray{Int64,2,StepRange{Int64,Int64},Tuple{}}:
1 11 21 31 41 51 61 71 81 91
2 12 22 32 42 52 62 72 82 92
3 13 23 33 43 53 63 73 83 93
4 14 24 34 44 54 64 74 84 94
5 15 25 35 45 55 65 75 85 95
6 16 26 36 46 56 66 76 86 96
7 17 27 37 47 57 67 77 87 97
8 18 28 38 48 58 68 78 88 98
9 19 29 39 49 59 69 79 89 99
10 20 30 40 50 60 70 80 90 100
julia> @code_warntype map(foo, img)
Variables:
#self#::Base.#map
f::#foo
A::Base.ReshapedArray{Int64,2,StepRange{Int64,Int64},Tuple{}}
Body:
begin # abstractarray.jl, line 1478:
return $(Expr(:invoke, _collect(::Base.ReshapedArray{Int64,2,StepRange{Int64,Int64},Tuple{}}, ::Base.Generator{Base.ReshapedArray{Int64,2,StepRange{Int64,Int64},Tuple{}},#foo}, ::Base.EltypeUnknown, ::Base.HasShape), :(Base._collect), :(A), :($(Expr(:new, Base.Generator{Base.ReshapedArray{Int64,2,StepRange{Int64,Int64},Tuple{}},#foo}, :(f), :(A)))), :($(Expr(:new, :(Base.EltypeUnknown)))), :($(Expr(:new, :(Base.HasShape))))))
end::Union{Array{Int64,2},Array{Union{},2}}
The |
That's been one of the most long-standing problems in julia: the possibility that the input would be an empty array made type-stable Having been frustrated by this issue for at least 2 years now (e.g., JuliaLang/julia#8027), I'm very pleased to report that...if you update your julia, you'll see this has (very recently) been fixed! There were some deep-guts improvements that made it finally possible to use type-inference to reliably infer the output type of |
on the input type. If the input is an `Image` then the resulting image is of the same type | ||
and has the same properties. | ||
|
||
For coloured images, the input is converted to YCbCr type and the Y channel is gamma corrected. |
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.
YIQ
Aside from this tiny edit, LGTM. Seems to need a rebase before merging, however. |
0a78d8a
to
18b47aa
Compare
Done :) Tests fail due to |
No description provided.