Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert between color models

I am very new to image processing. I have a PNG image (read using ImageIO.read()) that yields BufferedImage.TYPE_CUSTOM when I call getType() on it.

BufferedImage bi = ImageIO.read(new URL("file:/C:/samp1.png"));
int type =bi.getType(); //TYPE_CUSTOM for samp1.png

Now I would like to convert it to one of the following models:

  1. TYPE_USHORT_GRAY
  2. TYPE_3BYTE_BGR
  3. TYPE_BYTE_GRAY
  4. TYPE_INT_RGB
  5. TYPE_INT_ARGB

The above needs to be done to process the image further using a library that recognises only the above types.

How do I convert from TYPE_CUSTOM color model to other models?

Any help/pointers would be much appreciated. If there aren't any existing library to do this, any link/post to steps/algorithm would be great.

like image 536
Neville Avatar asked Jul 30 '11 06:07

Neville


People also ask

How do I convert exact colors from RGB to CMYK?

To create a new CMYK document in Photoshop, go to File > New. In the New Document window, simply switch the color mode to CMYK (Photoshop defaults to RGB). If you're wanting to convert an image from RGB to CMYK, then simply open the image in Photoshop. Then, navigate to Image > Mode > CMYK.

How do I convert RGB to XYZ?

Linear RGB to XYZ For example, if your values are in the range [0, 255], you must first divide each by 255.0. The output XYZ values are in the nominal range [0.0, 1.0]. The XYZ values will be relative to the same reference white as the RGB system.

What are different types of color models?

Three popular color models are: CMYK (Cyan, Magenta, Yellow, Black) RGB (Red, Green, Blue) Lab Color.

What are the two models of color?

There are several established color models used in computer graphics, but the two most common are the RGB model (Red-Green-Blue) for computer display and the CMYK model (Cyan-Magenta-Yellow-blacK) for printing.


1 Answers

Try this:

public static BufferedImage convert(BufferedImage src, int bufImgType) {
    BufferedImage img= new BufferedImage(src.getWidth(), src.getHeight(), bufImgType);
    Graphics2D g2d= img.createGraphics();
    g2d.drawImage(src, 0, 0, null);
    g2d.dispose();
    return img;
}
like image 68
eric2323223 Avatar answered Sep 21 '22 22:09

eric2323223