I have this short code img_lab = rand(Lab, 10, 10)
where I just have created an image of random colored pixels. Probably this question is trivial but how do I convert the output of this (which is Matrix{Lab{Float64}} (alias for Array{Lab{Float64}, 2}
) to an organized list of coordinates.
For example [[0,0,0.44,0.33,0.45],[0,1, ...] , ... , [10,10,0.54,0.32,0.87]]
where the first two entries are the coordinates and the last three values for RGB.
CodePudding user response:
First, please make sure that you have good reasons to do this. Converting to such a format makes it harder to use the rest of Colors
functionality, could have a (potentially large) performance cost, and is usually unnecessary.
Now, assuming it's necessary, here's one way to do it:
julia> using Random; Random.seed!(42)
TaskLocalRNG()
julia> img_lab = rand(Lab, 10, 10)
10×10 Matrix{Lab{Float64}}:
Lab(17.3575, -26.8628, -55.5385) Lab(9.1585, 86.3359, 71.5105) … Lab(14.4465, 87.7468, 12.1242)
Lab(16.6439, 11.0078, -10.1264) Lab(54.2929, -24.2044, -55.6044) Lab(5.5968, 79.2197, -6.69094)
Lab(39.0663, 61.8604, 90.5844) Lab(50.2836, -78.3482, -26.4757) Lab(6.6672, 21.7418, -100.172)
.
.
.
julia> map(keys(img_lab)) do cindex
rgbval = RGB(img_lab[cindex])
[cindex[1], cindex[2], red(rgbval), green(rgbval), blue(rgbval)]
end
10×10 Matrix{Vector{Float64}}:
[1.0, 1.0, 0.0, 0.223779, 0.488699] [1.0, 2.0, 0.468242, 0.0, 0.0] … [1.0, 10.0, 0.513226, 0.0, 0.101153]
[2.0, 1.0, 0.196137, 0.141577, 0.218233] [2.0, 2.0, 0.0, 0.573977, 0.885473] [2.0, 10.0, 0.366805, 0.0, 0.118275]
[3.0, 1.0, 0.737804, 0.0333098, 0.0] [3.0, 2.0, 0.0, 0.583897, 0.639801] [3.0, 10.0, 0.0, 0.136071, 0.65593]
[4.0, 1.0, 0.117582, 0.0803492, 0.209829] [4.0, 2.0, 0.833069, 0.433512, 1.0] [4.0, 10.0, 0.818553, 0.673722, 0.202751]
[5.0, 1.0, 0.0, 0.267613, 0.416236] [5.0, 2.0, 0.0, 1.0, 1.0] [5.0, 10.0, 0.26368, 0.0, 0.0]
[6.0, 1.0, 0.0, 0.703393, 0.0] [6.0, 2.0, 0.89057, 0.980158, 1.0] … [6.0, 10.0, 0.491448, 0.47962, 0.891735]
[7.0, 1.0, 0.0, 1.0, 0.819822] [7.0, 2.0, 1.0, 0.773939, 1.0] [7.0, 10.0, 0.0, 1.0, 1.0]
[8.0, 1.0, 1.0, 0.707005, 0.402694] [8.0, 2.0, 0.0, 0.820631, 1.0] [8.0, 10.0, 0.0, 0.3219, 0.91962]
[9.0, 1.0, 1.0, 0.830982, 0.928663] [9.0, 2.0, 0.0, 0.279664, 0.0] [9.0, 10.0, 0.937227, 0.650945, 0.442197]
[10.0, 1.0, 0.291409, 0.0, 0.28529] [10.0, 2.0, 1.0, 0.682411, 1.0] [10.0, 10.0, 0.0, 0.475448, 0.941526]
(From my limited understanding, Lab-to-RGB conversion is a lossy one - and the number of 0.0s and 1.0s in the color values above seems to indicate that too, values outside the RGB gamut getting clamped to be within its limits.)