ColorConvertOp Filter

ColorConvertOp changes the colour model. It can be used to convert an image to black and white.

Using a ConvolveOp is similar the various other BufferedImageOp classes. In all cases, we can apply a filter to a source to generate a destination. The other BufferedImageOp classes are explained in further detail in these other sections of the notes (AffineTransformOp, ConvolveOp, LookupOp & RescaleOp).

ColorConvertDemo Example (Run Applet)

import java.awt.*;
import java.awt.color.ColorSpace;
import java.awt.image.*;
import javax.swing.*;

public class ColorConvertDemo extends JApplet
{
    @Override
    public void init()
    {
        this.setContentPane(new View());
    }

    public class View extends JPanel
    {
        private final Image image = new ImageIcon(getClass().getClassLoader().getResource("images/koala.jpg")).getImage();

        public View()
        {
            super();
        }

        @Override
        protected void paintComponent(Graphics g)
        {
            super.paintComponent(g);

            // place the original file image into a buffered image
            final BufferedImage srcBufferedImg = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
            final Graphics2D SrcG = srcBufferedImg.createGraphics();
            SrcG.drawImage(this.image, 0, 0, getWidth(), getHeight(), this);


            // Do the colorConvert
            final ColorSpace grayScale = ColorSpace.getInstance(ColorSpace.CS_GRAY);
            final ColorConvertOp colorConvert = new ColorConvertOp(grayScale, null);
            final BufferedImage destBufferedImg = colorConvert.filter(srcBufferedImg, null);


            // draw the destination buffered image onto the applet's panel
            g.drawImage(destBufferedImg, 0, 0, getWidth(), getHeight(), this);
        }
    }
}
 
<div align="center"><a href="../versionC/index.html" title="DKIT Lecture notes homepage for Derek O&#39; Reilly, Dundalk Institute of Technology (DKIT), Dundalk, County Louth, Ireland. Copyright Derek O&#39; Reilly, DKIT." target="_parent" style='font-size:0;color:white;background-color:white'>&nbsp;</a></div>