Hey, I’m trying a histogram equalization. I’m getting the following error. Can someone please help me with this?
Thanks in advance!
Please make sure to post actual code rather than screenshots or literal photos of your screen, as it makes reading your question & helping you a lot more cumbersome!
That said the error you’re seeing is telling you that an index you are using is not an integer, but a floating point (decimal) number. This is likely in your call to cdf[image[i, j] + 1]
, where image[i, j] + 1
might be an integer?
Are you attempting to call the cdf
function here? If so, use round brackets cdf(image[i, j] + 1)
- in general, square brackets []
are for indexing, round brackets ()
are for calling.
Hey sorry for the picture.
Here is my code:
I have a histogram of the image,
hist = myHis(image)
cum = cumsum (hist)
cdf = cum ./ sum(cum) #type Array{Float64,1}
#for histogram equalization
m, n = size(image)
for I in 1: m-1
for j in 1: n-1
map[UInt8( [i,j] * 255)] = cdf(image[i,j] + 1)
end
end
I changed the square brackets to (). Now the error is “MethodError: objects of type Array {Float64, 1} are not callable. Use square brackets for indexing an array”
When I’m using square brackets, the error is “ArgumentError: invalid index: 1.3019608f0 of type Float32”
Thanks!
use triple backticks to surround your code…
in this code isn’t the outer loop variable an “ell” ( l ) not an “eye” ( i )
that’s your problem. you’re indexing with i
Oops! Sorry It’s a typo. It’s actually “i”, not ell.
This line doesn’t make sense to me. I’m not even able to figure out what you’re trying to do here. Can you describe in words what you’re trying to do?
I’m mapping the pixel in the image to the new one.
(i,j) is the intensity of the particular rows and column. I’m casting it as an UInt8 using UInt8(). though it says UInt() I’m pretty sure the values are floating points (it was previously divided by 255 whatever) so I multiply it by 255 and then I cast it as UInt8.
Perhaps you meant map[i,j] =Uint8(cdf(image[i,j] +1)*255)
Isn’t that same? But I tried out and getting the same error “ArgumentError: invalid index: 1.3019608f0 of type Float32”
So cdf
is apparently an array, and so is map
? It’s easy to get confused, because those two are also the names of functions. In particular, I think you should change the name of the map
array to make things less confusing.
Maybe you can try
map[i, j] = cdf[round(UInt8, image[i, j]) + 1]
So here, cdf is not an in-built function. Cdf is actually from my code as follows:
cum = cumsum(histogram)
cdf = cum ./ sum(cum)
Oh right! If I change my code like,
m, n = size(image)
for i in 1-m
for j in 1-n
out[i,j] = cdf [round(UInt8, image[i,j]) + 1]
end
end
So image[i,j] is Already a type of UInt8. I don’t think it’s okay to use round(UInt8, image[i,j) because it will throw an TypeError.
If image[i, j]
is already of type UInt8
then it’s not necessary to round or convert it, but it should work. But why are you trying to change things into UInt8
, then? Just try
map[i, j] = cdf[image[i, j] + 1]
I tried that out. It throws an error, “ArgumentError: invalid index: 1.3019608f0 of type Float32”
So I thought about converting it. So i casted UInt8, multiplying it by 255. Unfortunately, it throws the same error.
But then image
isn’t a UInt8
array after all, but an array of Float32
. Why can’t you use round
like I suggested, then?
Typeof(image) is Array{Gray{Normed{UInt8,8}}, 2}
This is what I’m confused about.
Type of my histogram and cdf both are, Array{Float64, 1}
So I tried using round.() to my cdf, to convert it into UInt8. The plot is more like step side function, not a cumulative histogram.
So, the thing is, there’s apparently a lot of important information that we don’t have access to. In order to help, we need to know what all the different variables are, and what they mean. Perhaps you can read this Please read: make it easier to help you , before going further? Just the first post, not the whole thread.
well then,
julia> using Images, ImageView, Plots, ImageCore
#Histogram function
julia> function myHis(image)
m, n = size(image)
frequency = zeros(256)
for i in 1:m, j in 1:n
frequency[UInt8(image[i,j] * 255)] += 1
end
return frequency ./ sizeof(image)
end
julia> image = load("bookstore_dark.tif")
hist = myHis(image)
cum = cumsum(hist)
cdf = cum ./ sum(cum)
#Histogram Equalization
julia> m, n = size(image)
for i in 1:m-1
for j in 1:n-1
out[UInt8([i,j] * 255)]= cdf[image[i,j] + 1]
end
end
Error:
ArgumentError: invalid index: 1.3019608f0 of type Float32
Stacktrace:
[1] to_index(::Float32) at .\indices.jl:297
[2] to_index(::Array{Float64,1}, ::Float32) at .\indices.jl:274
[3] to_indices at .\indices.jl:325 [inlined]
[4] to_indices at .\indices.jl:322 [inlined]
[5] getindex(::Array{Float64,1}, ::Float32) at .\abstractarray.jl:980
[6] top-level scope at .\In[102]:4
Note that this code depends on a file local to your computer (bookstore_dark.tif
), so it’s not possible for others to run.
That said, the problem still seems to be the one initially conjectured: your image
object likely contains Float32
element, so when you do cdf[image[i, j] + 1]
you are essentially trying to get the 1.3rd element of the cdf
vector, which is not an operation that makes sense.
This isn’t the only issue in that loop though, as:
julia> UInt8([1,1]*255)
ERROR: MethodError: no method matching UInt8(::Array{Int64,1})
Closest candidates are:
UInt8(::Union{Bool, Int32, Int64, UInt32, UInt64, UInt8, Int128, Int16, Int8, UInt128, UInt16}) at boot.jl:709
UInt8(::Float32) at float.jl:686
UInt8(::Float64) at float.jl:686
You can’t create an 8-bit unsigned integer from a vector. You might be trying to convert the vector into UInt8
s, in which case you want to broadcast UInt8.([1,1]*255)
. Note however that idexing with UInt8
doesn’t work:
julia> rand(10, 10)[UInt8.([1,1]*255)]
ERROR: BoundsError: attempt to access 10×10 Array{Float64,2} at index [UInt8[0xff, 0xff]]
Stacktrace:
[1] throw_boundserror(::Array{Float64,2}, ::Tuple{Array{UInt8,1}}) at ./abstractarray.jl:537
[2] checkbounds at ./abstractarray.jl:502 [inlined]
[3] _getindex at ./multidimensional.jl:726 [inlined]
[4] getindex(::Array{Float64,2}, ::Array{UInt8,1}) at ./abstractarray.jl:980
[5] top-level scope at REPL[19]:1
So given that you’re just using the result of this UInt8
construction to index your out
array, it’s probably best to forget about this altogether.
Where does out
come from? It has to be initialized somewhere.
So your problem is indexing. We don’t know what your image is, so that makes it harder, but at least we can point out things that do not make sense:
If image
is an array of floats, then UInt8(image[i,j] * 255)
is unlikely to work, since UInt8
can only convert integer values:
julia> UInt8(1.0) # this works
0x01
julia> UInt8(1.6) # this doesn't works
ERROR: InexactError: UInt8(1.6)
Therefore I think you should round or truncate instead of convert:
julia> round(UInt8, 1.0) # works
0x01
julia> round(UInt8, 1.6) # works
0x02
julia> trunc(UInt8, 1.0) # works
0x01
julia> trunc(UInt8, 1.6) # works
0x01
In fact, when indexing you should normally just use Int
s instead of UInt8
.
So you can try
frequency[trunc(UInt8, image[i,j] * 255)] += 1
but, I got the impression that image[i,j]
isn’t a normal float, but a RGB
type, and I don’t know how to handle them.
Here you are trying to index into cdf
using the contents of image[i,j]
, and since that is not an integer, you cannot use it for indexing.
And then there’s this:
Here you are trying to convert the vector [i, j]
of Int
s to UInt8
. I don’t understand why you are trying to do that, since you can (and should) use ordinary Int
s for indexing, and also because UInt8([i,j]*255)
could never work anyway. [i,j]*255
is a vector, so then you would have to use UInt8.([i,j]*255)
, but even that wouldn’t work, because i*255
and j*255
is bigger than 255 except when i
and j
are 0 or 1.
I understand now how it works. Thanks!
i just found i missed something in my histogram function.
julia> function myHis(image)
m, n = size(image)
frequency = zeros(256)
for i in 1:m, j in 1:n
frequency[UInt8(image[i,j] * 255)+1 ] += 1
end
return frequency ./ sizeof(image)
end