Login  Register

Re: Driving me nuts

Posted by EHB2010 on Mar 23, 2010; 11:21am
URL: http://imagej.273.s1.nabble.com/Driving-me-nuts-tp3688830p3688840.html

you don't need a plug in for that, as you'll soon find out when you start learning how to write your own.

to do that with the current plugin all you need to do is but add three lines of code:

1) describe the red only pixel you'd like to replace the partially red pixels with:
        "private final static int[] REDPIXEL = {255, 0, 0};"
2) as well as counting the non-grey scale pixels also replace them with your red only pixel:
                "colours.putPixel(i, j, REDPIXEL);"
3) redraw the image:
                "imp.draw();"

which gives:

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";
        private final static int[] REDPIXEL = {255, 0, 0};
       
        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;
                                        colours.putPixel(i, j, REDPIXEL);
                                }
                        }
                }
                imp.draw();
                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");
        }
}


which when compiled gives http://dl.dropbox.com/u/2014897/RedAreaCalculator_.class

you might like to start with some on-line tutorials in the basics:

http://java.sun.com/docs/books/tutorial/

I don't think they are as well written as the book though.