Transform image based on vegetation colour index equation

Hi folks

I am using Image J to threshold green crop canopy pixels from background soil pixels. I would like to first transform my image using a colour index based on RGB channels, the Excess Green Index:

ExGreen = 2 * (G/R+G+B) - (R/R+G+B) - (B/B+G+R)

This should create a tonal image with the green channel enhanced so that it becomes easier to threshold.

How do I tell ImageJ to create a new image by using the above equation to calculate new RGB values? Do I need to write a macro?

Thanks!

You don’t need to write a macro – you could use Process > Image Calculator. But it supports only 2 input images at a time. So it would take a lot of steps.

I think it would be easier to do in a simple Jython script, based on this example – showing how to create a new pixel array based on an existing image.

        # Obtain current image and its pixels
        imp = IJ.getImage()
        pix = imp.getProcessor().convertToFloat().getPixels()
         
        # find out the minimal pixel value
        min = reduce(Math.min, pix)
         
        # create a new pixel array with the minimal value subtracted
        pix2 = map(lambda x: x - min, pix)
       
        ImagePlus("min subtracted", FloatProcessor(imp.width, imp.height, pix2, None)).show()

You would just have to adapt it for your 3 channels and your particular formula. I’ve only played around a little bit with Jython, but the wiki page I linked to, by @albertcardona, has a nice tutorial and sample code.

Hope this helps.

2 Likes

Thanks very much for the suggestion @tswayne

Unfortunately it seems to be a very tricky problem for someone new to Jython - I have spent nearly the whole day trying to figure this out, and I just cannot get my code to run!

This morning I succeeded in working out how to do this in R with the EBImage package, then was curious to see if I could do it in Jython but apparently not - I guess Jython is just a level too tricky for me. I also struggled to find applicable examples and tutorials for Jython but I suppose I am trying to do something a bit obscure in it!

(if anyone else trying to use the excess green index sees this thread I am happy to provide the R code)

I also tried the Image Calculator but couldn’t get that to work either (it doesn’t seem to accept decimal values for pixels …?)

Cheers anyhow :slight_smile:

Instead of the Image Calculator, you can use the Image Expression Parser in Fiji. This is how it looks after loading the clown.jpg sample image (File > Open Samples > Clown (14K)), splitting the channels (Image > Color > Split Channels) and running Process > Image Expression Parser:


If you managed understanding the R syntax, I’m sure you’ll master Python in a very short time. Just give it a try.

4 Likes

Hi Chloe,
This is how I usually compute the excessive green image:

run("RGB Stack");
run("32-bit");
run("Multiply...", "value=-1 stack");
setSlice(2);
run("Multiply...", "value=-2 slice");
run("Z Project...", "projection=[Sum Slices]");

Note, however, that in this case the formula is just ExG = 2G-R-B, and this usually allows easy thresholding of plant tissue.

Jerome

1 Like

Hi,

Nice topic you have started. I have the same problem. Is it possible for you to send me the R code?

Hi all, if anyone sent or received the R code, I would love to know. We are trying to do the same process.

Here’s the R code to compute the excessive green image using the R imager package.

Hello All,
In Photosynthesis a large range of spectrum are used to determine different results. From a healthy plant compared to a weak plant, or one species from another, or even determine which plants are changing seasonally. The process used is determined by the question asked. The best formula I have found using RGB is: ((r-b)/(r+b)) + 0.5*g which I place in the grey channel in the Combine Channels and Auto Brightness/Contrast. This is for overall health of the plants visualization. Other combinations work for other questions.
Unfortunately we normal people do not have access to 220 band channel cameras, only 3 band.
Bob