Module ij
Package ij.process

Class ColorProcessor

  • All Implemented Interfaces:
    java.lang.Cloneable
    Direct Known Subclasses:
    IntProcessor

    public class ColorProcessor
    extends ImageProcessor
    This is an 32-bit RGB image and methods that operate on that image.. Based on the ImageProcessor class from "KickAss Java Programming" by Tonny Espeset (1996).
    • Constructor Detail

      • ColorProcessor

        public ColorProcessor​(java.awt.Image img)
        Creates a ColorProcessor from an AWT Image or BufferedImage.
      • ColorProcessor

        public ColorProcessor​(int width,
                              int height)
        Creates a blank ColorProcessor of the specified dimensions.
      • ColorProcessor

        public ColorProcessor​(int width,
                              int height,
                              int[] pixels)
        Creates a ColorProcessor from a pixel array.
    • Method Detail

      • createImage

        public java.awt.Image createImage()
        Description copied from class: ImageProcessor
        Returns a copy of this image is the form of an AWT Image.
        Specified by:
        createImage in class ImageProcessor
      • setColorModel

        public void setColorModel​(java.awt.image.ColorModel cm)
        Description copied from class: ImageProcessor
        Sets the color model. Must be an IndexColorModel (aka LUT) for all processors except the ColorProcessor.
        Overrides:
        setColorModel in class ImageProcessor
      • getColor

        public java.awt.Color getColor​(int x,
                                       int y)
      • setColor

        public void setColor​(java.awt.Color color)
        Sets the foreground color.
        Specified by:
        setColor in class ImageProcessor
      • setBackgroundColor

        public void setBackgroundColor​(java.awt.Color color)
        Sets the background fill/draw color.
        Overrides:
        setBackgroundColor in class ImageProcessor
      • setColor

        public void setColor​(int color)
        Sets the fill/draw color, where color is an RGB int.
        Overrides:
        setColor in class ImageProcessor
      • setValue

        public void setValue​(double value)
        Sets the default fill/draw value, where value is interpreted as an RGB int.
        Specified by:
        setValue in class ImageProcessor
      • setBackgroundValue

        public void setBackgroundValue​(double value)
        Sets the background fill value, where value is interpreted as an RGB int.
        Specified by:
        setBackgroundValue in class ImageProcessor
      • getMin

        public double getMin()
        Returns the smallest displayed pixel value.
        Specified by:
        getMin in class ImageProcessor
      • getMax

        public double getMax()
        Returns the largest displayed pixel value.
        Specified by:
        getMax in class ImageProcessor
      • setMinAndMax

        public void setMinAndMax​(double min,
                                 double max,
                                 int channels)
      • reset

        public void reset()
        Description copied from class: ImageProcessor
        Restores the pixel data from the snapshot (undo) buffer.
        Specified by:
        reset in class ImageProcessor
      • reset

        public void reset​(ImageProcessor mask)
        Description copied from class: ImageProcessor
        Restores pixels from the snapshot buffer that are within the rectangular roi but not part of the mask.
        Specified by:
        reset in class ImageProcessor
      • caSnapshot

        public void caSnapshot​(boolean set)
        Used by the ContrastAdjuster
      • caSnapshot

        public boolean caSnapshot()
        Used by the ContrastAdjuster
      • swapPixelArrays

        public void swapPixelArrays()
        Swaps the pixel and snapshot (undo) arrays.
        Specified by:
        swapPixelArrays in class ImageProcessor
      • setSnapshotPixels

        public void setSnapshotPixels​(java.lang.Object pixels)
        Description copied from class: ImageProcessor
        Sets a new pixel array for the snapshot (undo) buffer.
        Specified by:
        setSnapshotPixels in class ImageProcessor
      • getSnapshotPixels

        public java.lang.Object getSnapshotPixels()
        Returns a reference to the snapshot pixel array. Used by the ContrastAdjuster.
        Specified by:
        getSnapshotPixels in class ImageProcessor
      • getPixel

        public int getPixel​(int x,
                            int y)
        Description copied from class: ImageProcessor
        Returns the value of the pixel at (x,y). For RGB images, the argb values are packed in an int. For float images, the the value must be converted using Float.intBitsToFloat(). Returns zero if either the x or y coodinate is out of range. Use getValue(x,y) to get calibrated values from 8-bit and 16-bit images, to get intensity values from RGB images and to get float values from 32-bit images.
        Specified by:
        getPixel in class ImageProcessor
        See Also:
        ImageProcessor.getValue(int, int)
      • get

        public final int get​(int x,
                             int y)
        Description copied from class: ImageProcessor
        This is a faster version of getPixel() that does not do bounds checking.
        Specified by:
        get in class ImageProcessor
      • set

        public final void set​(int x,
                              int y,
                              int value)
        Description copied from class: ImageProcessor
        This is a faster version of putPixel() that does not clip out of range values and does not do bounds checking.
        Specified by:
        set in class ImageProcessor
      • get

        public final int get​(int index)
        Specified by:
        get in class ImageProcessor
      • set

        public final void set​(int index,
                              int value)
        Specified by:
        set in class ImageProcessor
      • getf

        public final float getf​(int x,
                                int y)
        Description copied from class: ImageProcessor
        Returns the value of the pixel at (x,y) as a float. Faster than getPixelValue() but does no bounds checking and does not return calibrated values.
        Specified by:
        getf in class ImageProcessor
      • setf

        public final void setf​(int x,
                               int y,
                               float value)
        Description copied from class: ImageProcessor
        Sets the value of the pixel at (x,y) to 'value'. Does no bounds checking or clamping, making it faster than putPixel(). Due to the lack of bounds checking, (x,y) coordinates outside the image may cause an exception. Due to the lack of clamping, values outside the 0-255 range (for byte) or 0-65535 range (for short) are not handled correctly.
        Specified by:
        setf in class ImageProcessor
      • getf

        public final float getf​(int index)
        Specified by:
        getf in class ImageProcessor
      • setf

        public final void setf​(int index,
                               float value)
        Specified by:
        setf in class ImageProcessor
      • getPixel

        public int[] getPixel​(int x,
                              int y,
                              int[] iArray)
        Returns the 3 samples for the pixel at (x,y) in an array of int. Returns zeros if the the coordinates are not in bounds. iArray is an optional preallocated array.
        Overrides:
        getPixel in class ImageProcessor
      • putPixel

        public final void putPixel​(int x,
                                   int y,
                                   int[] iArray)
        Sets a pixel in the image using a 3 element (R, G and B) int array of samples.
        Overrides:
        putPixel in class ImageProcessor
      • getInterpolatedPixel

        public double getInterpolatedPixel​(double x,
                                           double y)
        Calls getPixelValue(x,y).
        Specified by:
        getInterpolatedPixel in class ImageProcessor
      • getPixelInterpolated

        public final int getPixelInterpolated​(double x,
                                              double y)
        Description copied from class: ImageProcessor
        Uses the current interpolation method to find the pixel value at real coordinates (x,y). For RGB images, the argb values are packed in an int. For float images, the value must be converted using Float.intBitsToFloat(). Returns zero if the (x, y) is not inside the image.
        Specified by:
        getPixelInterpolated in class ImageProcessor
      • putPixel

        public final void putPixel​(int x,
                                   int y,
                                   int value)
        Stores the specified value at (x,y).
        Specified by:
        putPixel in class ImageProcessor
      • putPixelValue

        public void putPixelValue​(int x,
                                  int y,
                                  double value)
        Stores the specified real grayscale value at (x,y). Does nothing if (x,y) is outside the image boundary. The value is clamped to be in the range 0-255.
        Specified by:
        putPixelValue in class ImageProcessor
      • drawPixel

        public void drawPixel​(int x,
                              int y)
        Draws a pixel in the current foreground color.
        Specified by:
        drawPixel in class ImageProcessor
      • getPixels

        public java.lang.Object getPixels()
        Returns a reference to the int array containing this image's pixel data.
        Specified by:
        getPixels in class ImageProcessor
      • setPixels

        public void setPixels​(java.lang.Object pixels)
        Description copied from class: ImageProcessor
        Sets a new pixel array for the image. The length of the array must be equal to width*height. Use setSnapshotPixels(null) to clear the snapshot buffer.
        Specified by:
        setPixels in class ImageProcessor
      • getHSB

        public void getHSB​(byte[] H,
                           byte[] S,
                           byte[] B)
        Returns hue, saturation and brightness in 3 byte arrays.
      • getHSB

        public void getHSB​(float[] H,
                           float[] S,
                           float[] B)
        Returns hue, saturation and brightness in 3 float arrays.
      • getHSBStack

        public ImageStack getHSBStack()
        Returns an ImageStack with three 8-bit slices, representing hue, saturation and brightness
      • getHSB32Stack

        public ImageStack getHSB32Stack()
        Returns an ImageStack with three 32-bit slices, representing hue, saturation and brightness
      • getBrightness

        public FloatProcessor getBrightness()
        Returns brightness as a FloatProcessor.
      • getRGB

        public void getRGB​(byte[] R,
                           byte[] G,
                           byte[] B)
        Returns the red, green and blue planes as 3 byte arrays.
      • getChannel

        public byte[] getChannel​(int channel)
        Returns the specified plane (1=red, 2=green, 3=blue, 4=alpha) as a byte array.
      • getChannel

        public ByteProcessor getChannel​(int channel,
                                        ByteProcessor bp)
        Returns the specified plane (1=red, 2=green, 3=blue, 4=alpha) as a ByteProcessor.
      • setChannel

        public void setChannel​(int channel,
                               ByteProcessor bp)
        Sets the pixels of one color channel from a ByteProcessor.
        Parameters:
        channel - Determines the color channel, 1=red, 2=green, 3=blue, 4=alpha
        bp - The ByteProcessor where the image data are read from.
      • setRGB

        public void setRGB​(byte[] R,
                           byte[] G,
                           byte[] B)
        Sets the current pixels from 3 byte arrays (reg, green, blue).
      • setHSB

        public void setHSB​(byte[] H,
                           byte[] S,
                           byte[] B)
        Sets the current pixels from 3 byte arrays (hue, saturation and brightness).
      • setHSB

        public void setHSB​(float[] H,
                           float[] S,
                           float[] B)
        Sets the current pixels from 3 float arrays (hue, saturation and brightness).
      • setBrightness

        public void setBrightness​(FloatProcessor fp)
        Updates the brightness using the pixels in the specified FloatProcessor).
      • copyBits

        public void copyBits​(ImageProcessor ip,
                             int xloc,
                             int yloc,
                             int mode)
        Copies the image contained in 'ip' to (xloc, yloc) using one of the transfer modes defined in the Blitter interface.
        Specified by:
        copyBits in class ImageProcessor
      • applyTable

        public void applyTable​(int[] lut)
        Description copied from class: ImageProcessor
        Transforms the image or ROI using a lookup table. The length of the table must be 256 for byte images and 65536 for short images. RGB and float images are not supported.
        Specified by:
        applyTable in class ImageProcessor
      • applyTable

        public void applyTable​(int[] lut,
                               int channels)
      • filterRGB

        public void filterRGB​(int type,
                              double arg)
        Performs the specified filter on the red, green and blue planes of this image.
      • noise

        public void noise​(double range)
        Description copied from class: ImageProcessor
        Adds pseudorandom, Gaussian ("normally") distributed values, with mean 0.0 and the specified standard deviation, to this image or ROI.
        Specified by:
        noise in class ImageProcessor
      • findEdges

        public void findEdges()
        Description copied from class: ImageProcessor
        Finds edges in the image or ROI using a Sobel operator.
        Overrides:
        findEdges in class ImageProcessor
      • erode

        public void erode()
        Description copied from class: ImageProcessor
        Erodes the image or ROI using a 3x3 maximum filter. Requires 8-bit or RGB image.
        Specified by:
        erode in class ImageProcessor
      • dilate

        public void dilate()
        Description copied from class: ImageProcessor
        Dilates the image or ROI using a 3x3 minimum filter. Requires 8-bit or RGB image.
        Specified by:
        dilate in class ImageProcessor
      • autoThreshold

        public void autoThreshold()
        Description copied from class: ImageProcessor
        Converts the image to binary using an automatically determined threshold. For byte and short images, converts to binary using an automatically determined threshold. For RGB images, converts each channel to binary. For float images, does nothing.
        Overrides:
        autoThreshold in class ImageProcessor
      • getInterpolatedRGBPixel

        public int getInterpolatedRGBPixel​(double x,
                                           double y)
        Uses bilinear interpolation to find the pixel value at real coordinates (x,y).
      • makeThumbnail

        public ImageProcessor makeThumbnail​(int width2,
                                            int height2,
                                            double smoothFactor)
        Uses averaging to creates a new ColorProcessor containing a downsized copy of this image or selection.
      • convolve3x3

        public void convolve3x3​(int[] kernel)
        3x3 convolution contributed by Glynne Casteel.
        Specified by:
        convolve3x3 in class ImageProcessor
      • filter

        public void filter​(int type)
        A 3x3 filter operation, where the argument (ImageProcessor.BLUR_MORE, FIND_EDGES, MEDIAN_FILTER, MIN or MAX) determines the filter type.
        Specified by:
        filter in class ImageProcessor
      • getHistogram

        public int[] getHistogram()
        Description copied from class: ImageProcessor
        Returns the histogram of the image or ROI. Returns a luminosity histogram for RGB images and null for float images.

        For 8-bit and 16-bit images, returns an array with one entry for each possible value that a pixel can have, from 0 to 255 (8-bit image) or 0-65535 (16-bit image). Thus, the array size is 256 or 65536, and the bin width in uncalibrated units is 1.

        For RGB images, the brightness is evaluated using the color weights (which would result in a float value) and rounded to an int. This gives 256 bins. FloatProcessor.getHistogram is not implemented (returns null).

        Specified by:
        getHistogram in class ImageProcessor
      • weightedHistogram

        public boolean weightedHistogram()
      • convolve

        public void convolve​(float[] kernel,
                             int kernelWidth,
                             int kernelHeight)
        Performs a convolution operation using the specified kernel.
        Specified by:
        convolve in class ImageProcessor
      • setWeightingFactors

        public static void setWeightingFactors​(double rFactor,
                                               double gFactor,
                                               double bFactor)
        Sets the weighting factors used by getPixelValue(), getHistogram() and convertToByte() to do color conversions. The default values are 1/3, 1/3 and 1/3. Check "Weighted RGB Conversions" in Edit/Options/Conversions to use 0.299, 0.587 and 0.114.
        See Also:
        getWeightingFactors(), setRGBWeights(double, double, double)
      • setRGBWeights

        public void setRGBWeights​(double rweight,
                                  double gweight,
                                  double bweight)
        This is a thread-safe (non-static) version of setWeightingFactors().
      • setRGBWeights

        public void setRGBWeights​(double[] weights)
        This is a thread-safe (non-static) version of setWeightingFactors().
      • getRGBWeights

        public double[] getRGBWeights()
        Returns the values set by setRGBWeights(), or null if setRGBWeights() has not been called.
      • isInvertedLut

        public boolean isInvertedLut()
        Always returns false since RGB images do not use LUTs.
        Overrides:
        isInvertedLut in class ImageProcessor
      • isGrayscale

        public final boolean isGrayscale()
        Returns 'true' if this is a grayscale image.
        Overrides:
        isGrayscale in class ImageProcessor
      • getBestIndex

        public int getBestIndex​(java.awt.Color c)
        Always returns 0 since RGB images do not use LUTs.
        Overrides:
        getBestIndex in class ImageProcessor
      • invertLut

        public void invertLut()
        Does nothing since RGB images do not use LUTs.
        Overrides:
        invertLut in class ImageProcessor
      • updateComposite

        public void updateComposite​(int[] rgbPixels,
                                    int channel)
        Description copied from class: ImageProcessor
        This method is used by CompositeImage.updateImage() to create RGB images (for display) of a multi-channel composite images.
        Overrides:
        updateComposite in class ImageProcessor
      • threshold

        public void threshold​(int level)
        Not implemented.
        Specified by:
        threshold in class ImageProcessor
      • getNChannels

        public int getNChannels()
        Returns the number of color channels (3).
        Overrides:
        getNChannels in class ImageProcessor
        Returns:
        1 for grayscale images, 3 for RGB images
      • toFloat

        public FloatProcessor toFloat​(int channelNumber,
                                      FloatProcessor fp)
        Returns a FloatProcessor with one color channel of the image. The roi and mask are also set for the FloatProcessor.
        Specified by:
        toFloat in class ImageProcessor
        Parameters:
        channelNumber - Determines the color channel, 0=red, 1=green, 2=blue
        fp - Here a FloatProcessor can be supplied, or null. The FloatProcessor is overwritten by this method (re-using its pixels array improves performance).
        Returns:
        A FloatProcessor with the converted image data of the color channel selected
      • setPixels

        public void setPixels​(int channelNumber,
                              FloatProcessor fp)
        Sets the pixels of one color channel from a FloatProcessor.
        Specified by:
        setPixels in class ImageProcessor
        Parameters:
        channelNumber - Determines the color channel, 0=red, 1=green, 2=blue
        fp - The FloatProcessor where the image data are read from.
      • getBitDepth

        public int getBitDepth()
        Description copied from class: ImageProcessor
        Returns the bit depth, 8, 16, 24 (RGB) or 32. RGB images actually use 32 bits per pixel.
        Overrides:
        getBitDepth in class ImageProcessor