Wrong brightness converting image to grayscale in Java - java

I'm converting a image to gray scale in Java with the following code:
BufferedImage originalImage = ImageIO.read(new File("/home/david/input.bmp"));
BufferedImage grayImage = new BufferedImage(originalImage.getWidth()
, originalImage.getHeight()
, BufferedImage.TYPE_BYTE_GRAY);
ColorSpace gray = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOp colorConvert = new ColorConvertOp(gray, null);
colorConvert.filter(originalImage, grayImage);
ImageIO.write(grayImage, "bmp", new File("/home/david/output_java.bmp"));
That seems to work, but the problem is that the output image is very different from the gray scale image generated by gimp (see examples below).
Can I control someway how is the image generated?
How I can make the result more similar to the gimp result?
Original image:
Gray scale image generated in Java:
Gray scale image generated in Gimp (Image -> Mode -> Grayscale):
BTW: I have a bunch of images coming from ffmpeg (with gray option) and they are like Gimp images so because of that I want my image in that way.

Finally I've wrote GrayscaleFilter class implementing BufferedImageOp interface.
I've followed this really good guide about Java image processing.
This is the relevant code fragment:
public class GrayscaleFilter extends AbstractFilter
{
public final static double[] METHOD_AVERAGE = {1.0/3.0, 1.0/3.0, 1.0/3.0};
public final static double[] METHOD_GIMP_LUMINOSITY = {0.21, 0.71, 0.07};
public GrayscaleFilter(final double[] rgb)
{
this(rgb[0], rgb[1], rgb[2]);
}
public BufferedImage filter(BufferedImage src, BufferedImage dest)
{
if (src.getType() == BufferedImage.TYPE_BYTE_GRAY)
{
dest = src;
return dest;
}
if (dest == null)
dest = createCompatibleDestImage(src, null);
final int width = src.getWidth();
final int height = src.getHeight();
int[] inPixels = new int[width * height];
GraphicsUtilities.getPixels(src, 0, 0, width, height, inPixels);
byte[] outPixels = doFilter(inPixels);
GraphicsUtilities.setPixels(dest, 0, 0, width, height, outPixels);
return dest;
}
private byte[] doFilter(int[] inputPixels)
{
int red, green, blue;
int i = 0;
byte[] outPixels = new byte[inputPixels.length];
for(int pixel : inputPixels)
{
// Obtengo valores originales
red = (pixel >> 16) & 0xFF;
green = (pixel >> 8) & 0xFF;
blue = pixel & 0xFF;
// Calculo valores nuevos
outPixels[i++] = (byte)(
red * red_part +
green * green_part +
blue * blue_part
);
}
return outPixels;
}
public BufferedImage createCompatibleDestImage(BufferedImage src, ColorModel destCM)
{
return new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
}
}

Find out the conversion formula used by Gimp. It probably takes some human color perception into account, while the Java implementation is mathematical (R+G+B)/ 3.

Related

Attempting to set alpha value of color changes color

I created this code in order to remove all semi-transparent colors from an image and make them fully opaque. For some reason, the colors of the image change drastically, even though im only changing the alpha. Attached is the code and an example of what happens to the image.
Before:
After:
public class Main {
public static void main(String args[]) throws IOException
{
File file = new File("karambitlore.png");
FileInputStream fis = new FileInputStream(file);
BufferedImage image = ImageIO.read(fis);
image = convertToType(image, BufferedImage.TYPE_INT_ARGB);
BufferedImage image2 = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
for (int width = 0; width < image.getWidth(); width++)
{
for (int height = 0; height < image.getHeight(); height++)
{
int rgb = image.getRGB(width, height);
boolean transparent = (rgb & 0xFF000000) == 0x0;
boolean opaque = (rgb & 0xFF000000) == 0xFF000000;
if (!transparent && !opaque)
{
rgb = rgb | 0xFF000000;
image2.setRGB(width, height, rgb);
} else
{
image2.setRGB(width, height, image.getRGB(width, height));
}
}
}
fis.close();
ImageIO.write(image2, "png", file);
System.out.println(image.getType());
}
public static BufferedImage convertToType(BufferedImage image, int type) {
BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), type);
Graphics2D graphics = newImage.createGraphics();
graphics.drawImage(image, 0, 0, null);
graphics.dispose();
return newImage;
}
}
The main problem with your input image is that it has a nearly-transparent "halo" around the fully transparent pixels, kind of like a "dithered" transparency. When you turn these pixels fully opaque, they a) create opaque pixels where you don't want them, and b) make these pixels way too saturated as the pixels are not premultiplied with alpha (you can google "premultiplied alpha" if you are interested in alpha compositing/blending). Note that the pixels don't really "change color", it's just that they normally don't contribute much to the end result as they are almost fully transparent.
The easy fix, that works ok regardless of background color, is just to use a threshold value to decide whether the pixel should be transparent.
If you know the background color (either solid or some "average") you can blend the pixel color with the background color, using the alpha value. This will create smoother edges, but ONLY against this background. You may use a lower threshold in this case.
Here's a modified version of your code that produces better results for your input:
public static void main(String args[]) throws IOException {
File file = new File(args[0]);
BufferedImage image = convertToType(ImageIO.read(file), BufferedImage.TYPE_INT_ARGB);
Color bg = Color.ORANGE; // Change to the color of your background, if you want to blend
int bgR = bg.getRed();
int bgG = bg.getGreen();
int bgB = bg.getBlue();
for (int width = 0; width < image.getWidth(); width++) {
for (int height = 0; height < image.getHeight(); height++) {
int rgb = image.getRGB(width, height);
int opaqueness = (rgb >>> 24) & 0xFF;
if (opaqueness > 100) { // The threshold 100 works fine for your current input, tune to suit other needs
// Fully opaque
image.setRGB(width, height, rgb | 0xFF000000);
// Or if you prefer blending the background:
/*
// Multiply alpha
int r = ((rgb >> 16 & 0xFF) * opaqueness) + (bgR * (0xFF - opaqueness)) >> 8;
int g = ((rgb >> 8 & 0xFF) * opaqueness) + (bgG * (0xFF - opaqueness)) >> 8;
int b = ((rgb & 0xFF) * opaqueness) + (bgB * (0xFF - opaqueness)) >> 8;
image.setRGB(width, height, r << 16 | g << 8 | b | 0xFF000000);
*/
} else {
// Fully transparent
image.setRGB(width, height, rgb & 0xFFFFFF);
}
}
}
ImageIO.write(image, "png", new File(file.getParentFile(), file.getName() + "_copy_bg_mult.png"));
}
public static BufferedImage convertToType(BufferedImage image, int type) {
BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), type);
Graphics2D graphics = newImage.createGraphics();
graphics.setComposite(AlphaComposite.Src);
graphics.drawImage(image, 0, 0, null);
graphics.dispose();
return newImage;
}
The code as-is will create an image like this, and it will have slightly jagged edges, but the "halo" is removed:
Or, if you comment out the "Fully opaque" version, and comment in the "Multiply alpha" section, you can get an image like this (which will obviously only look good against a yellow background):

Java recoloring BufferedImage not working with an image of a larger height

I have a program that is supposed to take the RGB values of an image and then multiply them by some constants, and then draw the new image on a JPanel. The problem is that if my image is over a certain height, specifically over 187 pixels, the new colored image is different than an image with a height of less than 187px.
The JPanel shows this: example.
Notice how the longer recolored image is different than the shorter one. I'm sure that the shorter image's colors are correct, and I have no idea how it's getting messed up.
public class RecolorImage extends JPanel {
public static int scale = 3;
public static BufferedImage walk, walkRecolored;
public static BufferedImage shortWalk, shortWalkRecolored;
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(200*scale, 400*scale);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new RecolorImage());
walk = ImageLoader.loadImage("/playerWalk.png");
walkRecolored = recolor(walk);
shortWalk = ImageLoader.loadImage("/playerWalkShort.png");
shortWalkRecolored = recolor(shortWalk);
frame.setVisible(true);
}
#Override
public void paint(Graphics graphics) {
Graphics2D g = (Graphics2D) graphics;
g.scale(scale, scale);
g.drawImage(walk, 10, 10, null);
g.drawImage(walkRecolored, 40, 10, null);
g.drawImage(shortWalk, 70, 10, null);
g.drawImage(shortWalkRecolored, 100, 10, null);
}
The recolor method:
public static BufferedImage recolor(BufferedImage image) {
BufferedImage outputImage = deepCopy(image);
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
int rgb = image.getRGB(x, y);
Color c = new Color(rgb);
int r = c.getRed();
int g = c.getGreen();
int b = c.getBlue();
r *= 0.791;
g *= 0.590;
b *= 0.513;
int newRGB = (rgb & 0xff000000) | (r << 16) | (g << 8) | b;
outputImage.setRGB(x, y, newRGB);
}
}
return outputImage;
}
How I load the images and make deep copies:
public static BufferedImage loadImage(String path) {
try {
return ImageIO.read(ImageLoader.class.getResource(path));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static BufferedImage deepCopy(BufferedImage image) {
ColorModel colorModel = image.getColorModel();
boolean isAlphaPremultiplied = colorModel.isAlphaPremultiplied();
WritableRaster raster = image.copyData(null);
return new BufferedImage(colorModel, raster, isAlphaPremultiplied, null);
}
My original images: the tall image and short image. Thanks for any help!
Your source images have different color models:
the short image uses 4 bytes per pixel (RGB and alpha)
the tall image uses 1 byte per pixel (index into a palette of 256 colors)
Your recolored images use the same color model as the source images (thanks to the deepCopy method), therefore the recolored image for the tall image also uses the same color palette as the source image, meaning that it cannot contain all the colors you want.
Since your recoloring code overwrites each pixel of the output image anyway the deep copy operation is unnecessary. Instead you would better create a full color image as target image like this:
public static BufferedImage recolor(BufferedImage image) {
BufferedImage outputImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
//... code as before
}

How to make the white of a PNG transparent in Java?

I would like to know how to implement the following makeWhiteTransparent() below so that it takes a file and only makes the white pixels transparent in my existing PNG. That is ONLY the perfectly white pixels (#FFFFFF).
public static void main(String[] args)
{
File pngFile = new File(pathToPngFile);
File outputPngFile = new File(pathToOutputPngFile);
makeWhiteTransparent(pngFile, outputPngFile);
}
I even looked for open source libraries in addition to finding a bunch of responses here on SO but nothing seemed to work. That or the code was complex and unless you know what you're doing it was hard to understand (for example thresholds, etc.). I basically just want all #FFFFFF pixels in my PNG to be transparent.
It should be as simple as setting the Alpha channel's value to 0, if the rest of the channels are at 255
private static void makeWhiteTransparent(File in, File out)throws IOException{
BufferedImage bi = ImageIO.read(in);
int[] pixels = bi.getRGB(0, 0, bi.getWidth(), bi.getHeight(), null, 0, bi.getWidth());
for(int i=0;i<pixels.length;i++){
int color = pixels[i];
int a = (color>>24)&255;
int r = (color>>16)&255;
int g = (color>>8)&255;
int b = (color)&255;
if(r == 255 && g == 255 && b == 255){
a = 0;
}
pixels[i] = (a<<24) | (r<<16) | (g<<8) | (b);
}
BufferedImage biOut = new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_INT_ARGB);
biOut.setRGB(0, 0, bi.getWidth(), bi.getHeight(), pixels, 0, bi.getWidth());
ImageIO.write(biOut, "png", out);
}

Java Convert PPM Byte array to JPG image

Currently working on Image manipulation in Java
I have the byte array(PPM) of size 921600 (640*480*3)
byte[] image; // PPM
BufferedImage image = ImageIO.read(new ByteArrayInputStream(image));
image is null.
Tried with ImageMagic and JAI libraries. But it does not help me.
Is it possible to get the RGB components from byte array and convert it to JPG file.
Any help is appreciated.
Below is the Code (which will convert PPM(byte array to Buffered image and you can save buffered image to the file)
// Method Call
BufferedImage image = ppm(width, height, 255, byte[]);
//Method Definition
static public BufferedImage ppm(int width, int height, int maxcolval, byte[] data){
if(maxcolval<256){
BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
int r,g,b,k=0,pixel;
if(maxcolval==255){ // don't scale
for(int y=0;y<height;y++){
for(int x=0;(x<width)&&((k+3)<data.length);x++){
r=data[k++] & 0xFF;
g=data[k++] & 0xFF;
b=data[k++] & 0xFF;
pixel=0xFF000000+(r<<16)+(g<<8)+b;
image.setRGB(x,y,pixel);
}
}
}
else{
for(int y=0;y<height;y++){
for(int x=0;(x<width)&&((k+3)<data.length);x++){
r=data[k++] & 0xFF;r=((r*255)+(maxcolval>>1))/maxcolval; // scale to 0..255 range
g=data[k++] & 0xFF;g=((g*255)+(maxcolval>>1))/maxcolval;
b=data[k++] & 0xFF;b=((b*255)+(maxcolval>>1))/maxcolval;
pixel=0xFF000000+(r<<16)+(g<<8)+b;
image.setRGB(x,y,pixel);
}
}
}
return image;
}
else{
BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
int r,g,b,k=0,pixel;
for(int y=0;y<height;y++){
for(int x=0;(x<width)&&((k+6)<data.length);x++){
r=(data[k++] & 0xFF)|((data[k++] & 0xFF)<<8);r=((r*255)+(maxcolval>>1))/maxcolval; // scale to 0..255 range
g=(data[k++] & 0xFF)|((data[k++] & 0xFF)<<8);g=((g*255)+(maxcolval>>1))/maxcolval;
b=(data[k++] & 0xFF)|((data[k++] & 0xFF)<<8);b=((b*255)+(maxcolval>>1))/maxcolval;
pixel=0xFF000000+(r<<16)+(g<<8)+b;
image.setRGB(x,y,pixel);
}
}
return image;
}
}
You can use a WritableRaster to set the pixels in the image for you:
For a grayscale image, you will have a byte array like this:
byte[] arr = new byte[width * height];
To make an image, use:
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
img.getRaster().setDataElements(0, 0, width, height, arr);
For a color image, you will have an array like this:
byte[] arr = new byte[width * height * 3];
So, to make an image, use:
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
img.getRaster().setDataElements(0, 0, width, height, arr);
You may need to mess this the TYPE in the first line. See here to choose which type your image is.

How to make a color transparent in a BufferedImage and save as PNG

I have been searching the web for this, but I havent found any decent help.
I have a BufferedImage, which I have read in with ImageIO. Now I would like to make a certain color in that image to transparent, and save the image as PNG.
I know I cannot just "paint" the transparent color for obvious reasons, so I am guessing I need some kind of a filter.
Anyone got some sample code for this?
I did that recently, to answer a question of my project manager.
The function transforming gray to transparency is:
private Image TransformGrayToTransparency(BufferedImage image)
{
ImageFilter filter = new RGBImageFilter()
{
public final int filterRGB(int x, int y, int rgb)
{
return (rgb << 8) & 0xFF000000;
}
};
ImageProducer ip = new FilteredImageSource(image.getSource(), filter);
return Toolkit.getDefaultToolkit().createImage(ip);
}
Actually, it acts on a gray-level image, so I just copy a RGB component (the R one) to alpha, discarding the others which are identical in my case.
You can adapt it to filter a specific color, eg. with a test of equality or range, etc.
Of course, the BufferedImage must be of BufferedImage.TYPE_INT_ARGB type.
I don't address the question of saving, as it is pretty trivial, but I can add this code page too.
[EDIT] To convert Image to BufferedImage:
BufferedImage dest = new BufferedImage(
imageWidth, imageHeight,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = dest.createGraphics();
g2.drawImage(image, 0, 0, null);
g2.dispose();
[EDIT 2] I come after Christoffer posted his complete solution, but here is mine, I show how to make a range of colors transparent. Can be improved, eg. using HSB components instead.
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;
import java.io.*;
import javax.imageio.ImageIO;
public class AddTransparency
{
AddTransparency() throws IOException
{
String imagePath = "E:/Documents/images/";
File inFile = new File(imagePath, "map.png");
BufferedImage image = ImageIO.read(inFile);
Image transpImg1 = TransformGrayToTransparency(image);
BufferedImage resultImage1 = ImageToBufferedImage(transpImg1, image.getWidth(), image.getHeight());
File outFile1 = new File(imagePath, "map_with_transparency1.png");
ImageIO.write(resultImage1, "PNG", outFile1);
Image transpImg2 = TransformColorToTransparency(image, new Color(0, 50, 77), new Color(200, 200, 255));
BufferedImage resultImage2 = ImageToBufferedImage(transpImg2, image.getWidth(), image.getHeight());
File outFile2 = new File(imagePath, "map_with_transparency2.png");
ImageIO.write(resultImage2, "PNG", outFile2);
}
private Image TransformGrayToTransparency(BufferedImage image)
{
ImageFilter filter = new RGBImageFilter()
{
public final int filterRGB(int x, int y, int rgb)
{
return (rgb << 8) & 0xFF000000;
}
};
ImageProducer ip = new FilteredImageSource(image.getSource(), filter);
return Toolkit.getDefaultToolkit().createImage(ip);
}
private Image TransformColorToTransparency(BufferedImage image, Color c1, Color c2)
{
// Primitive test, just an example
final int r1 = c1.getRed();
final int g1 = c1.getGreen();
final int b1 = c1.getBlue();
final int r2 = c2.getRed();
final int g2 = c2.getGreen();
final int b2 = c2.getBlue();
ImageFilter filter = new RGBImageFilter()
{
public final int filterRGB(int x, int y, int rgb)
{
int r = (rgb & 0xFF0000) >> 16;
int g = (rgb & 0xFF00) >> 8;
int b = rgb & 0xFF;
if (r >= r1 && r <= r2 &&
g >= g1 && g <= g2 &&
b >= b1 && b <= b2)
{
// Set fully transparent but keep color
return rgb & 0xFFFFFF;
}
return rgb;
}
};
ImageProducer ip = new FilteredImageSource(image.getSource(), filter);
return Toolkit.getDefaultToolkit().createImage(ip);
}
private BufferedImage ImageToBufferedImage(Image image, int width, int height)
{
BufferedImage dest = new BufferedImage(
width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = dest.createGraphics();
g2.drawImage(image, 0, 0, null);
g2.dispose();
return dest;
}
public static void main(String[] args) throws IOException
{
AddTransparency at = new AddTransparency();
}
}
Thanks to PhilLo here is a complete solution of my demo application.
public static void main(String[] args) throws Exception {
File in = new File("C:\\Users\\Christoffer\\Desktop\\christoffer.jpg");
BufferedImage source = ImageIO.read(in);
int color = source.getRGB(0, 0);
Image image = makeColorTransparent(source, new Color(color));
BufferedImage transparent = imageToBufferedImage(image);
File out = new File("C:\\Users\\Christoffer\\Desktop\\trans.PNG");
ImageIO.write(transparent, "PNG", out);
}
private static BufferedImage imageToBufferedImage(Image image) {
BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = bufferedImage.createGraphics();
g2.drawImage(image, 0, 0, null);
g2.dispose();
return bufferedImage;
}
public static Image makeColorTransparent(BufferedImage im, final Color color) {
ImageFilter filter = new RGBImageFilter() {
// the color we are looking for... Alpha bits are set to opaque
public int markerRGB = color.getRGB() | 0xFF000000;
public final int filterRGB(int x, int y, int rgb) {
if ((rgb | 0xFF000000) == markerRGB) {
// Mark the alpha bits as zero - transparent
return 0x00FFFFFF & rgb;
} else {
// nothing to do
return rgb;
}
}
};
ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
return Toolkit.getDefaultToolkit().createImage(ip);
}

Categories

Resources