Login  Register

Re: Filtering certain color range

Posted by Sami Badawi-2 on May 13, 2008; 1:18pm
URL: http://imagej.273.s1.nabble.com/Filtering-certain-color-range-tp3696226p3696231.html

Hi Rasheed,

It should be pretty straightforward to write this filter as an ImageJ
PlugInFilter:

1: Find the color that you want to filter. Just put the mouse over it
in ImageJ and it will give you 3 coordinates for the RGB components.
2: Select a max distance of what colors will be considered the same as
the filter color you chose
3: Run through all the pixels in the image and see if the color on a
given pixel has a smaller distance to your filter color, if so replace
that pixel with black.

This seem like it could be useful, I will put a filter like this in
the next version of my little plugin ShapeLogic.

Here is a little code that might be useful for your to write it yourself filter:

public class ColorUtil {

        public static final int BLUE_MASK = 0xff;
        public static final int GREEN_MASK = 0xff00;
        public static final int RED_MASK = 0xff0000;

        //Saving in this sequence gives the option of saving alpha too
        public static final int RED_POS = 2;
        public static final int GREEN_POS = 1;
        public static final int BLUE_POS = 0;

        public static final int GREEN_OFFSET = 8;
        public static final int RED_OFFSET = 16;

        /** Split color coded as int into 3 int. */
        static public int[] splitColor(int colorIn) {
                int[] iArray = new int[3];
                iArray[RED_POS] = (colorIn&RED_MASK)>>RED_OFFSET; //red
                iArray[GREEN_POS] = (colorIn&GREEN_MASK)>>GREEN_OFFSET; //green
                iArray[BLUE_POS] = colorIn&BLUE_MASK; //blue
                return iArray;
        }

        /** Split color coded as int into 3 int. */
        static public int[] splitColor(int colorIn, int[] iArray) {
                if (iArray == null)
                        iArray = new int[3];
                iArray[RED_POS] = (colorIn&RED_MASK)>>RED_OFFSET; //red
                iArray[GREEN_POS] = (colorIn&GREEN_MASK)>>GREEN_OFFSET; //green
                iArray[BLUE_POS] = colorIn&BLUE_MASK; //blue
                return iArray;
        }

        /** Distance between 2 colors split into arrays of int. */
        static public double distance(int[] color1, int[] color2) {
                int minLength = Math.min(color1.length, color2.length);
                if (minLength == 0)
                        return 0.;
                double distanceResult = 0.;
                for (int i = 0; i < minLength; i++) {
                        distanceResult += Math.abs(color1[i] - color2[i]);
                }
                return distanceResult / minLength;
        }

-Sami Badawi
http://www.shapelogic.org