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:
- TYPE_USHORT_GRAY
- TYPE_3BYTE_BGR
- TYPE_BYTE_GRAY
- TYPE_INT_RGB
- 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.
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;
}
Have you tried this?
BufferedImage rgbImg = new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_INT_RGB);
精彩评论