Login  Register

Re: Color Pixel Counter - plugin

Posted by Daniel Kalthoff on May 17, 2011; 5:17pm
URL: http://imagej.273.s1.nabble.com/Color-Pixel-Counter-plugin-tp3683486p3683492.html

Hi,

if you like to count only pixels of exactly defined colors, you could use my colorMask_.java plugin (code below).

It operates on RGB images and will simply turn the selected color (by default the current drawing color) into white, all other colors into black. It is helpful, i.e. if you have drawn a set of non-overlapping ROIs using different colors onto a background image and want to create masks from it. Be careful, it operates directly on the current image (duplicate first)!

Convert the resulting mask image into 8-bit, measure integrated density over the whole image and divide by 256 - you should get the number of pixels with your particular color by then.

Best regards,

Daniel

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

public class colorMask_ implements PlugInFilter {
        ImagePlus imp;
        Color maskCol;

        public int setup(String arg, ImagePlus imp) {
                this.imp = imp;
                maskCol = Toolbar.getForegroundColor();
                return DOES_RGB;
        }

        public void run(ImageProcessor ip) {
                GenericDialog gd = new GenericDialog("Choose Mask Color");
                        gd.addNumericField("R", maskCol.getRed(), 0);
                        gd.addNumericField("G", maskCol.getGreen(), 0);
                        gd.addNumericField("B", maskCol.getBlue(), 0);
                gd.showDialog();
                if (! gd.wasCanceled()) {
                        // retrieve color
                        maskCol = new Color( (int)gd.getNextNumber(), (int)gd.getNextNumber(), (int)gd.getNextNumber() );
                        for (int iSlice=1; iSlice <= imp.getNSlices(); iSlice++) {
                                int[] px = (int[]) imp.getStack().getProcessor(iSlice).getPixels();
                                for (int iPx = 0; iPx < ip.getWidth() * ip.getHeight(); iPx++) {
                                        if (px[iPx] == maskCol.getRGB()) px[iPx] = 0xffffff; else px[iPx] = 0x000000;
                                }
                        }
                        imp.updateAndDraw();
                }
        }
}


On 17.05.2011, at 09:01, Jacqui Ross wrote:

> Hi All,
>
> I have a person wanting to count the numbers of coloured pixels in her images. She has green, red and yellow and black. It's not colocalisation analysis that she is looking for.
>
> There seems to be a useful Plugin in development by Ben Pichette here: http://imagejdocu.tudor.lu/doku.php?id=plugin:color:color_pixel_counter:start but no plugin as such yet.
>
> Just wondering if Ben is able to let me know if this plugin is soon to be released or alternatively, whether there is another plugin that can do this? The other option I was looking at to assist her was Threshold Colour.
>
> Any assistance would be very welcome.
>
> Kind regards,
>
> Jacqui
>
> Jacqueline Ross
> Biomedical Imaging Microscopist
> Biomedical Imaging Research Unit
> School of Medical Sciences
> Faculty of Medical & Health Sciences
> The University of Auckland
> Private Bag 92019
> Auckland, NEW ZEALAND
>
> Tel: 64 9 373 7599 Ext 87438
> Fax: 64 9 373 7484
>
> http://www.fmhs.auckland.ac.nz/sms/biru/