Skip to content

Commit e91d010

Browse files
authored
Merge pull request #80 from GenericMappingTools/let-cheat-RGB-indices
Let users redefine the RGB color order for the RGB indices.
2 parents 218420c + 4595422 commit e91d010

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/spectral_indices.jl

+22-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const generic_docs = "
22
- The first form accepts inputs as matrices, or file names of the data bands.
3-
- The second form is more versatile but also more complex to describe.
3+
- The last form is more versatile but also more complex to describe.
44
- `cube`: Is the file name of a 'cube', a multi-layered file normally created with the [`cutcube`](@ref) function.
55
If this file was created with band descriptions one can use the `bands` or the `bandnames` options.
66
- `bands`: _cubes_ created with [`cutcube`](@ref) assign descriptions starting with \"Band1 ...\" an so on
@@ -12,13 +12,18 @@ const generic_docs = "
1212
- `bandnames`: When we know the common designation of a band, for example \"Green\", or any part of a band
1313
description, for example \"NIR\", we can use that info to create a `bandnames` string vector that will be
1414
matched against the cube's bands descriptions.
15-
- `kwargs`:
15+
16+
### Kwargs
1617
- `threshold`: When a threshold is provided we return a GMTgrid where `vals[ij] < threshold = NaN`
1718
- `classes`: is a vector with up to 3 elements (class separators) and we return a UInt8 GMTimage with the
1819
indices categorized into vals[ij] > classes[1] = 1; vals[ij] > classes[2] = 2; vals[ij] > classes[3] = 3 and 0 otherwise.
1920
- `mask`: Used together with `threshold` outputs a UInt8 GMTimage mask with `vals[ij] >= threshold = 255` and 0 otherwise
2021
If `mask=-1` (or any other negative number) we compute instead a mask where `vals[ij] < threshold = 255` and 0 otherwise
2122
- `save`: Use `save=\"file_name.ext\"` to save the result in a disk file. File format is picked from file extension.
23+
- `order` | `bands_order` | `rgb`: For the ``GLI``, ``TGI`` and ``VARI`` (RGB) indices, we allow to reorder the bands
24+
and change the expected RGB order. Pass in a string, or symbol, with the color order. For example, `order=:rbg`
25+
will swap the green and blue components making the result index identify the _reds_ instead of the _greens_.
26+
Not good for vegetation indices, but potentially useful for other purposes.
2227
2328
If none of `bands`, `layers` or `bandnames` is provided, we use the default band names shown in the first form.
2429
@@ -477,6 +482,9 @@ function helper_sp_indices(kwargs...)
477482
(haskey(dd, :mask)) && delete!(dd, :mask)
478483
(haskey(dd, :classes)) && delete!(dd, :classes)
479484
(haskey(dd, :threshold)) && delete!(dd, :threshold)
485+
(haskey(dd, :rgb)) && delete!(dd, :rgb)
486+
(haskey(dd, :order)) && delete!(dd, :order)
487+
(haskey(dd, :bands_order)) && delete!(dd, :bands_order)
480488
(length(d) > 0) && println("Warning: the following options were not consumed in sp_indices => ", keys(dd))
481489
return mask, rev_mask, classes, threshold, save_name, dbg
482490
end
@@ -498,11 +506,21 @@ function sp_indices(bnd1::String, bnd2::String, bnd3::String=""; index::String="
498506
end
499507

500508
# ----------------------------------------------------------------------------------------------------------
501-
function sp_indices(rgb::GMT.GMTimage{UInt8, 3}; index::String="", kwargs...)
509+
function sp_indices(rgb::GMT.GMTimage{UInt8, 3}; index::String="", kw...)
502510
# This method applyies only in the case of the RGB vegetation indices (GLI, TGI, VARI)
503511
(index != "GLI" && index != "TGI" && index != "VARI") && error("With RGB images input, only `GLI`, `TGI` and `VARI` indices are supported, not $index")
504512
(rgb.layout[3] != 'B') && error("For now, only band interleavedRGB composition is supported and not $(rgb.layout)")
505-
img = sp_indices(view(rgb, :, :, 1), view(rgb, :, :, 2), view(rgb, :, :, 3); index=index, kwargs...)
513+
# Here we are allowing cheating the indices by altering the bands order. These indices expect (were deffined)
514+
# the bands in RGB order but nothing stops us to to change that and convert a green index into a blue index.
515+
# For that pass string with the R,G,B in the wished order to the 'order' option. E.g. 'order="rbg"'
516+
bds::Vector{Int} = [1,2,3] # The default RGB order
517+
if ((val = find_in_kwargs(kw, [:order :bands_order :rgb])[1]) !== nothing)
518+
o = lowercase(string(val))
519+
bds[1] = (o[1] == 'r') ? 1 : (o[1] == 'g') ? 2 : (o[1] == 'b') ? 3 : error("Non 'r', 'g' or 'b' in the 'order' option")
520+
bds[2] = (o[2] == 'r') ? 1 : (o[2] == 'g') ? 2 : (o[2] == 'b') ? 3 : error("Non 'r', 'g' or 'b' in the 'order' option")
521+
bds[3] = (o[3] == 'r') ? 1 : (o[3] == 'g') ? 2 : (o[3] == 'b') ? 3 : error("Non 'r', 'g' or 'b' in the 'order' option")
522+
end
523+
img = sp_indices(view(rgb, :, :, bds[1]), view(rgb, :, :, bds[2]), view(rgb, :, :, bds[3]); index=index, kw...)
506524
return mat2grid(img, rgb)
507525
end
508526

test/runtests.jl

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ G2 = gli("LC08_cube.tiff"); println(" gli 2")
6666
G3 = gli(cube); println(" gli 3")
6767
@test G1.range[5:6] == G2.range[5:6]
6868
@test G1.range[5:6] == G3.range[5:6]
69+
G1 = gli(R,G,B, order="rbg"); println(" gli 4")
6970

7071
G1 = tgi(R,G,B); println(" tgi 1")
7172
G2 = tgi("LC08_cube.tiff"); println(" tgi 2")

0 commit comments

Comments
 (0)