Home > Blockchain >  How to convert a Float64 Matrix into a RGB channel Matrix in Julia?
How to convert a Float64 Matrix into a RGB channel Matrix in Julia?

Time:12-09

Suppose I have the following Matrix:

img = [
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.5  1.0  1.0  1.0  1.0  1.0  1.0  0.5  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
]

I want to convert it to a Matrix in which each element represent the value as an RGB channel. I guess a somewhat translation should happen there. E.g., what is the equivalent RGB channel for representing 0.0? You might say this can't happen or isn't possible. But I can convert the same Matrix into a black-and-white image:

julia> using Images
julia> Gray.(img)

And this shows an image as the output. So if it can translate the 0.0 into black and 1.0 into white by the Gray channel, Then I can expect existing the equivalent as an RGB channel.

Q1: What is my expected output?
Ans: A Matrix like this (here, with arbitrary numbers):

10×12 Array{RGB{Float64},2} with eltype RGB{Float64}:
 RGB{Float64}(0.412225,0.412225,0.412225)     …  RGB{Float64}(0.755344,0.755344,0.755344)
 RGB{Float64}(0.0368978,0.0368978,0.0368978)     RGB{Float64}(0.198098,0.198098,0.198098)

Q2: Why do I need this?
Ans: The whole idea is to be able to change the color of some pixels. So first, I have a Matrix with the Float64 element type. Then I want to achieve the equivalent Matrix with the RGB{Float64} element type to change the value of some of them into something else (to show a pixel in a green color or anything.) and then see the picture to check the final image.

Q3: What I've tried?
Ans: I tried a straightforward approach, but I failed:

julia> RGB.(img)

julia> typeof(RGB.(img))
Matrix{RGB{N0f8}} (alias for Array{RGB{Normed{UInt8, 8}}, 2})

This command renders the image. It doesn't give me the expected Matrix. Then I tried this:

julia> channelview(RGB.(img))
3×10×12 reinterpret(reshape, N0f8, ::Array{RGB{N0f8},2}) with eltype N0f8:
[:, :, 1] =
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
;;; …

[:, :, 12] =
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0

But it's not what I expect.

Update: Now tried another way:

julia> convert(Array{RGB, 2}, img)
# Just renders the result and doesn't show the matrix

julia> typeof(convert(Array{RGB, 2}, img))
Matrix{RGB} (alias for Array{RGB, 2})

Also, I tried collecting it, but it led to the same result.

P.S. I need it to work in the VScode, not the REPL.

CodePudding user response:

Issue solved by disabling the plot pane: enter image description here

CodePudding user response:

To understand what you're seeing, realize that Julia calls display(obj) on the value obj returned from any command you execute. So when you do RGB.(img) (which is indeed the answer to your original question), that value gets returned---you could assign it to a variable and manipulate it.

But since you're working in VSCode, the default is to display anything that is a matrix of Colorant as an image, rather than a standard matrix. If you want to see it as a numeric matrix, you need to do a bit more work to specify that. Here's a demo in VSCode's Julia REPL:

image display

Just as you found, after the imgrgb = RGB.(img) line, the plot pane showed a representation of this image. But as the later lines show, you can still manipulate and display imgrgb as a regular matrix. The MIME("text/plain") forces it to display in text-mode rather than image-mode.

CodePudding user response:

Unable to understand why RGB.(img) is not the expected result? Looks fine to me.

| This command renders the image. It doesn't give me the expected Matrix.

julia> using Images
[ Info: Precompiling Images [916415d5-f1e6-5110-898d-aaa5f9f070e0]

julia> img = [
        0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
        0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
        0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
        0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
        0.0  0.5  1.0  1.0  1.0  1.0  1.0  1.0  0.5  0.0  0.0  0.0
        0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
        0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
        0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
        0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
        0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.5
       ]
10×12 Matrix{Float64}:
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.5  1.0  1.0  1.0  1.0  1.0  1.0  0.5  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.5

julia> RGB.(img)
10×12 Array{RGB{Float64},2} with eltype RGB{Float64}:
 RGB{Float64}(0.0,0.0,0.0)  …  RGB{Float64}(0.0,0.0,0.0)
 RGB{Float64}(0.0,0.0,0.0)     RGB{Float64}(0.0,0.0,0.0)
 RGB{Float64}(0.0,0.0,0.0)     RGB{Float64}(0.0,0.0,0.0)
 RGB{Float64}(0.0,0.0,0.0)     RGB{Float64}(0.0,0.0,0.0)
 RGB{Float64}(0.0,0.0,0.0)     RGB{Float64}(0.0,0.0,0.0)
 RGB{Float64}(0.0,0.0,0.0)  …  RGB{Float64}(0.0,0.0,0.0)
 RGB{Float64}(0.0,0.0,0.0)     RGB{Float64}(0.0,0.0,0.0)
 RGB{Float64}(0.0,0.0,0.0)     RGB{Float64}(0.0,0.0,0.0)
 RGB{Float64}(0.0,0.0,0.0)     RGB{Float64}(0.0,0.0,0.0)
 RGB{Float64}(0.0,0.0,0.0)     RGB{Float64}(0.5,0.5,0.5)
  • Related