Conversion to Gray{Bool}

I wish to convert of type(which i loaded as a bitmap)(I tried other types before)
Array{ColorTypes.RGB{FixedPointNumbers.Normed{UInt8,8}},2} to an object of type ColorTypes.Gray{Bool}
I wanted to apply convex hull algorithm but when I load images it gave me various type of errors:

MethodError: no method matching convexhull(::Array{ColorTypes.RGB{FixedPointNumbers.Normed{UInt8,8}},2})
Closest candidates are:

convexhull(!Matched::AbstractArray{T,2}) where T<:Union{ColorTypes.Gray{Bool}, Bool} at /home/ashwani/.julia/packages/ImageMorphology/BZPyk/src/convexhull.jl:7

I assume you mean you want to convert the elements of the array from RGB{N0f8} to Gray{Bool}, rather than convert the entire array to a single Gray(true)/Gray(false) value.

You’ll need to decide how to convert to a binary image; there’s no unique answer. The most common way to convert from RGB to grayscale is based on perceptual intensity; we have a demo of that in https://juliaimages.org/latest/democards/examples/color_channels/rgb_grayscale/#RGB-to-GrayScale. But of course you could do that based on, say, just the red channel, or the distance from a given pixel to the nearest blue-dominated pixel, etc. The number of ways is literally infinite depending on your goal.

Gray.(img) would convert to a Gray{N0f8}, which still isn’t binary. Continuing the theme of using the intensity, here’s a method based on thresholding:

julia> using Images, TestImages

julia> img = testimage("lighthouse");

julia> imgg = Gray.(img);

julia> thresh = otsu_threshold(imgg)
Gray{N0f8}(0.408)

julia> imgt = imgg .> thresh;

julia> summary(imgt)
"512×768 BitMatrix"

But again, the number of ways you could binarize is infinite, so you’ll have to decide what you actually want.

4 Likes