Vegitation Index from RGB

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
ITWarrior
Posts: 3
Joined: 2019-05-18T01:03:33-07:00
Authentication code: 1152

Vegitation Index from RGB

Post by ITWarrior »

I'm trying to process some photos into green -> red gradients to highlight plant crop health where healthy regions (greens) show as bright green, while unhealthy regions (brown) show as red, with a gradient in between.

There's a couple of examples on the sort of output I'm trying to achieve here:
https://blog.dronedeploy.com/identifyin ... c380381a33

I've found some mathematics for this here (e.g. VARI is "(green-red)/(green+red-blue)"):
https://rdrr.io/cran/uavRst/man/rgb_indices.html

I've been trying out VARI and NGRDI conversions but without success. I've been trying it like this:

convert test.png -channel RGB -fx "(g-r)/(g+r-b)" -normalize out.png
and this:
convert test.png -channel RGB -fx "(u.g-u.r)/(u.g+u.r-u.b)" -normalize out.png
and this:
convert test.png -channel RGB -fx "(green-red)/(green+red-blue)" -normalize out.png

But I either get divide by zero errors, a full green output, or the output is identical to the input. I'm obviously doing something wrong - I assumed that I'd be able to drop the calculations straight in but this does not seem to be the case. The reason for posting in this forum is that I found a post here which gives me the type of output that I'm looking for, except that this one seems to be turning whiter areas into red and darker areas blue/green:
viewtopic.php?t=24682

I've tried several variations but I've not even come close. Is there anybody that can help with being able to apply the mathematical conversions in that way to images?

Thank you very much for your assistance.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Vegitation Index from RGB

Post by snibgo »

What version of IM, on what platform?

For "-fx", see http://www.imagemagick.org/script/fx.php . Use single letters "r", "g" or "b" for those channels. Eg:

Code: Select all

magick in.png -fx "(g-r)/(g+r-b)" out.png
At pixels where g+r-b=0, you will get a divide by zero, so the output pixel will be white.

It might help if you linked to an input image, and ideally the required outputs for whatever formulae you want.

EDIT: I should add that "r" in the formula is the red channel on a scale of 0.0 to 1.0. So beware of formulae like...

Code: Select all

(1-(r-30)/(r+30))*(1-(g-50)/(g+50))*(1-(b-1)/(b+1))
... which probably assume a scale of 0 to 255, or 0 to 100, or something else.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Vegitation Index from RGB

Post by fmw42 »

I have moved this post to the User's forum, since it does not seem to have anything to do with my scripts.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Vegitation Index from RGB

Post by fmw42 »

Since your computation has differences in channels, the typical thing to do if not using HDRI is to add 0.5 to those differences.


Input
Image

Code: Select all

convert pivot-irrigation-rgb.png -fx "g-r+0.5/(g+r-b+0.5)" -auto-level pivot-irrigation-rgb-var1.png
Image

Or apply some colormap to colorize the gray levels with a rainbow type colormap

Code: Select all

convert xc:red xc:orange xc:yellow xc:green1 xc:cyan xc:blue xc:blueviolet +append -flop -filter Cubic -resize 600x30! colormap.png
Image

Code: Select all

convert pivot-irrigation-rgb.png -fx "g-r+0.5/(g+r-b+0.5)" -auto-level colormap.png -clut  pivot-irrigation-rgb-var2.png
Image

Perhaps a better method would be to add a small amount to the denominator and add 0.5 to result of the division.

Code: Select all

convert pivot-irrigation-rgb.png -fx "g-r/(g+r-b+quantumscale)+0.5" -auto-level pivot-irrigation-rgb-var3.png
Image
Post Reply