ROI Extract Javascript Rewrite

javascript
imagej
roi

#1

Hi all,

I found the script below on the old imagej.1557.x6.nabble forum, and it does almost exactly what I need it to, but works on RGB images rather than 8-bit images. For my project I have to extract data from large no.s of ROIs and I hit limitations in data handling capacity, so switching from RGB to 8-bit images would reduce the amount of data by 66% and solve my problem as well as make the process faster. Would someone be able to rewrite this code to do so? I hope this is a fairly simple request, I have nigh on 0 knowledge in coding and my simplistic attempts to rewrite were quite futile.

Cheers,
John

Link to original code

import ij.*; 
import ij.process.*; 
import ij.gui.*; 
import java.awt.*; 
import ij.plugin.*; 
import ij.plugin.frame.*; 

public class MultiROIhist_ implements PlugIn { 

        public void run(String arg) { 
                ImagePlus imp = IJ.getImage(); 
                IJ.run(imp, "Invert", ""); 
                IJ.wait(1000); 
                IJ.run(imp, "Invert", ""); 
        } 

imp = IJ.getImage(); 
  ip = imp.getProcessor(); 
  rm = RoiManager.getInstance(); 
  rois = rm.getRoisAsArray(); 
  rt = new ResultsTable(); 
  for (i=0; i<rois.length; i++) { 
     ip.setRoi(rois[i]); 
     ip.setRGBWeights(1, 0, 0); 
     histogram = ip.getHistogram(); 
     rt.incrementCounter(); 
     rt.addValue("ROI", "R"+i); 
     for (j=0; j<256; j++) 
        rt.addValue("C"+j, histogram[j]); 
     ip.setRGBWeights(0, 1, 0); 
     histogram = ip.getHistogram(); 
     rt.incrementCounter(); 
     rt.addValue("ROI", "G"+i); 
     for (j=0; j<256; j++) 
        rt.addValue("C"+j, histogram[j]); 
     ip.setRGBWeights(0, 0, 1); 
     histogram = ip.getHistogram(); 
     rt.incrementCounter(); 
     rt.addValue("ROI", "B"+i); 
     for (j=0; j<256; j++) 
        rt.addValue("C"+j, histogram[j]); 
  } 
  rt.showRowNumbers(false); 
  rt.show("Results"); 

}

#2

Dear @WreckJohnB,

were you able to solve the issue in the meantime? If not, could you please explain in a bit more detail what you are trying to achieve?

Best,
Stefan


#3

Hello Stefan,

Nope, no progress yet, thanks for picking this up.

I need to extract individual histograms from large 8-bit images for lots of ROIs (>500,000). Normally Iā€™d just convert 8-bit to RGB but because of the number of individual particles I am analysing, I hit a limitation that Excel can only deal with approx 1m rows of data. Currently the javascript extracts a red, green and blue value but because I am working on greyscale images they are all the same and I get masses of redundant data (i.e. 1.5m data rows for 500k ROIs). So simply by rewriting the javascript to deal with 8-bit images rather than RGB the amount of data to 1/3 of what I am currently facing, which makes both running the javascript, as well as post-processing massively faster/easier.

So to sum things up, is it possible to rewrite the javascript to extract histograms for greyscale rather than RGB data? I have 0 programming knowledge, hence why I turned to this forum.

I hope this makes sense!
John


#4

Hi @WreckJohnB,

the code you posted above is Java code (which is unnecessarily complicated for your task), so I first simplified it a bit to have a Groovy script that you can run from the script editor (after selecting Language > Groovy):

#@ ImagePlus imp
#@ RoiManager rm

import ij.IJ
import ij.measure.ResultsTable

ip = imp.getProcessor()
rois = rm.getRoisAsArray()
rt = new ResultsTable()
for (i=0; i<rois.length; i++) { 
	ip.setRoi(rois[i])
	ip.setRGBWeights(1, 0, 0)
	histogram = ip.getHistogram()
	rt.incrementCounter()
	rt.addValue("ROI", "R"+i)
	for (j=0; j<256; j++)
		rt.addValue("C"+j, histogram[j])
	ip.setRGBWeights(0, 1, 0)
	histogram = ip.getHistogram()
	rt.incrementCounter()
	rt.addValue("ROI", "G"+i)
	for (j=0; j<256; j++)
		rt.addValue("C"+j, histogram[j])
	ip.setRGBWeights(0, 0, 1)
	histogram = ip.getHistogram()
	rt.incrementCounter()
	rt.addValue("ROI", "B"+i)
	for (j=0; j<256; j++)
		rt.addValue("C"+j, histogram[j])
} 
rt.showRowNumbers(false)
rt.show("Results")

Now, to make it work on 8-bit images instead of RGB, you can just leave away the ip.setRGBWeights(0, 1, 0) calls and only call the histogram once instead of three times, which yields this simplified script:

#@ ImagePlus imp
#@ RoiManager rm

import ij.IJ
import ij.measure.ResultsTable

ip = imp.getProcessor()
rois = rm.getRoisAsArray()
rt = new ResultsTable()
for (i=0; i<rois.length; i++) { 
	ip.setRoi(rois[i])
	histogram = ip.getHistogram()
	rt.incrementCounter()
	rt.addValue("ROI", ""+i)
	for (j=0; j<256; j++)
		rt.addValue("C"+j, histogram[j])
} 
rt.showRowNumbers(false)
rt.show("Results")

#5

Amazing, all working. Thanks very much for your help!!