Login  Register

Re: Driving me nuts

Posted by EHB2010 on Mar 21, 2010; 4:09pm
URL: http://imagej.273.s1.nabble.com/Driving-me-nuts-tp3688830p3688833.html

oh well if the camera is doing it digitally from the original image data I would imagine the R&G&B of the grey areas really are equal. That makes things very simple- something like this should do it (untested):

import ij.plugin.PlugIn;
import ij.ImagePlus;
import ij.IJ;
import ij.WindowManager;
import ij.process.*;
import ij.measure.*;
 
public final class RedAreaCalculator_ implements PlugIn {
        private ImagePlus imp;
        private final static String COLUMN_HEADING = "percentage red";
       
        public void run(String arg) {
                imp = WindowManager.getCurrentImage();
                        int bitDepth = imp.getBitDepth();
                        if (bitDepth ==24) {
                                CalculatePercentageRed();
                        } else {
                                IJ.error("RGB image required");
                        }
               
        }
       
        private void CalculatePercentageRed(){
                ImageProcessor colours = imp.getProcessor();
                int[] RGB  = new int[3];
                int ImageWidth = colours.getWidth();
                int ImageHeight = colours.getHeight();
                final int totalPixelNumber = ImageWidth*ImageHeight;
                int sumOfRedPixels = 0;
                for (int i = 0 ; i < ImageWidth ; ++i) {
                        for (int j = 0 ; j < ImageHeight ; ++j) {
                                RGB = colours.getPixel(i, j, RGB);
                                if (RGB[0] != RGB[1]) {
                                        sumOfRedPixels = sumOfRedPixels +1;
                                }
                        }
                }
                double percentage = (sumOfRedPixels*100.00)/totalPixelNumber;
                ResultsTable rt = ResultsTable.getResultsTable();
                int column = rt.getColumnIndex(COLUMN_HEADING);
                if (column == -1) {
                        rt.addColumns();
                        column = rt.getFreeColumn(COLUMN_HEADING);
                }
                rt.incrementCounter();
                rt.addValue(column, percentage);
                rt.show("Results");
        }
}