Java BufferedImage get single pixel brightness - java

I want to convert coloured image to a monochrome, i thought to loop all pixel, but I don't know how to test if they are bright or dark.
for(int y=0;y<image.getHeight();y++){
for(int x=0;x<image.getWidth();x++){
int color=image.getRGB(x, y);
// ???how to test if its is bright or dark?
}
}

int color = image.getRGB(x, y);
// extract each color component
int red = (color >>> 16) & 0xFF;
int green = (color >>> 8) & 0xFF;
int blue = (color >>> 0) & 0xFF;
// calc luminance in range 0.0 to 1.0; using SRGB luminance constants
float luminance = (red * 0.2126f + green * 0.7152f + blue * 0.0722f) / 255;
// choose brightness threshold as appropriate:
if (luminance >= 0.5f) {
// bright color
} else {
// dark color
}

I suggest first converting the pixel to grayscale, then applying a threshold for converting it pure black&white.
There are libraries that will do this for you, but if you want to learn how images are processed, here you are:
Colour to grayscale
There are various formulas for converting (see a nice article here), I prefer the "luminosity" one. So:
int grayscalePixel = (0.21 * pRed) + (0.71 * pGreen) + (0.07 * pBlue)
I cannot tell what API you are using to manipulate the image, so I left the formula above in general terms. pRed, pGreen and pBlue are the red, green and blue levels (values) for the pixel.
Grayscale to b/w
Now, you can apply a threshold with:
int bw = grayscalePixel > THRESHOLD? 1: 0;
or even:
boolean bw = grayscalePixel > THRESHOLD;
Pixel will be white if above threshold, black if below. Find the right THRESHOLD by experimenting a bit.

Related

Java - Convert Image to black and white - fails with bright colors

I'm attempting to convert an image to black and white only (not grey scale).
I've used this:
BufferedImage blackAndWhiteImage = new BufferedImage(
dWidth.intValue(),
dHeight.intValue(),
BufferedImage.TYPE_BYTE_BINARY);
Graphics2D graphics = blackAndWhiteImage.createGraphics();
graphics.drawImage(colourImage, 0, 0, null);
return blackAndWhiteImage;
Everything fine, until I decided to try out brighter colors, like the Google logo for example:
and it came out with this:
Then I tried first to pass trough grey scale first using:
BufferedImage blackAndWhiteImage2 = new BufferedImage(
dWidth.intValue(),
dHeight.intValue(),
BufferedImage.TYPE_USHORT_GRAY);
And it seemed to have saved the Blue color, but not the brightest (in this case yellow), and as you may see it decreased in quality:
Any suggestions are much appreciated; I believe what I am seeking for is to convert every colour to Black except White (which would be the background color), this is already done when applying TYPE_BYTE_BINARY removing the alpha channel.
EDIT:
Maybe I have not explained very clear:
the final image has to have White background **1
every other color has to be converted to Black
**1 - there are some cases where the image is actually White on Black..which is annoying (whiteOnBlackExample) as it complicates a lot this process, and I will leave this later on, as priority now is to convert "normal" images.
What I did was, first strip out the alpha channel if it exists -> therefore convert the alpha channel to White; then convert every other color to Black
If you use JavaFX you can use the ColorAdjust effect with brightness of -1 (minimum), which makes all the (non-white) colors black:
public class Main extends Application {
Image image = new Image("https://i.stack.imgur.com/UPmqE.png");
#Override
public void start(Stage primaryStage) {
ImageView colorView = new ImageView(image);
ImageView bhView = new ImageView(image);
ColorAdjust colorAdjust = new ColorAdjust();
colorAdjust.setBrightness(-1);
bhView.setEffect(colorAdjust);
primaryStage.setScene(new Scene(new VBox(colorView, bhView)));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
These Effects are optimized so they are probably faster than what you would achieve by applying them manually.
Edit
Since your requirements are that
any pixel which is not opaque should be transformed to white, and
any pixel which is not white should be transformed to black,
the predesigned effects won't suit you as far as I can tell - they are too specific. You can do pixel by pixel manipulation:
WritableImage writableImage = new WritableImage(image.getPixelReader(), (int) image.getWidth(), (int) image.getHeight());
PixelWriter pixelWriter = writableImage.getPixelWriter();
PixelReader pixelReader = writableImage.getPixelReader();
for (int i = 0; i < writableImage.getHeight(); i++) {
for (int j = 0; j < writableImage.getWidth(); j++) {
Color c = pixelReader.getColor(j, i);
if (c.getOpacity() < 1) {
pixelWriter.setColor(j, i, Color.WHITE);
}
if (c.getRed() > 0 || c.getGreen() > 0 || c.getBlue() > 0) {
pixelWriter.setColor(j, i, Color.BLACK);
}
}
}
ImageView imageView = new ImageView(writableImage);
Note that the order in which you apply the rules matter. A transparent non-white pixel will turn white if you apply 1 and then 2, but if you apply 2 and then 1 it will end up black. This is because the predefined WHITE and BLACK colors are opaque. You can manually set the red, green and blue values while not changing the alpha value instead. It all depends on your exact requirements.
Remember that due to lossy compression of some file formats you might not find true white in them at all, but a value which is close to true white and your eye won't be able to tell the difference.
Here is the example from my comment. At first open the input image and create a new one for output.
BufferedImage myColorImage = ImageIO.read(fileInput);
BufferedImage myBWImage = new BufferedImage(myColorImage.getWidth(), myColorImage.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
Then iterate through all the pixels and compare rgb values with a threshold:
for (int x = 0; x < myColorImage.getWidth(); x++)
for (int y = 0; y < myColorImage.getHeight(); y++)
if (rgbToGray(myColorImage.getRGB(x, y), MODE.AVERAGE) > threshold)
myBWImage.setRGB(x, y, 0);
else
myBWImage.setRGB(x, y, 0xffffff);
Here is rgbToGray method implementation to compare with threshold:
private static int rgbToGray(int rgb, MODE mode) {
// split rgb integer into R, G and B components
int r = (rgb >> 16) & 0xff;
int g = (rgb >> 8) & 0xff;
int b = rgb & 0xff;
int gray;
// Select mode
switch (mode) {
case LIGHTNESS:
gray = Math.round((Math.max(r, Math.max(g, b)) + Math.min(r, Math.min(g, b))) / 2);
break;
case LUMINOSITY:
gray = Math.round(0.21f * r + 0.72f * g + 0.07f * b);
break;
case AVERAGE:
default:
gray = Math.round((r + g + b) / 3);
break;
}
return gray;
}
An utility enum:
private enum MODE {
LIGHTNESS, AVERAGE, LUMINOSITY
}
I got the following result:
Note: for your google image even threshold = 1 is suitable, for other images you should pick another values from range [0..255]. For photos, most likely, more appropriate values are about 100-150. MODE also will affect the final result.

color rescaling in java of buffered image

First of all, thanks for reading.
I am writing a class which returns an image from 2D double array(kDEvalue from the code).
However, since so many values(256*256) vary in a very small range(-1 to 1), i cannot use the default color scaling(it is all black if i use the value from the array).
BufferedImage image = new BufferedImage(kDEvalues.length, kDEvalues[0].length, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < 256; x++) {
for (int y = 0; y < 256; y++) {
image.setRGB(x, y, (255*(int)Math.round(kDEvalues[x][y]))/100);
}
}
try {
ImageIO.write(image, "jpg", new File("CustomImage.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
In this case, how I rescale the color in grey scale so that I can see the varience of the value? Thanks in advance
You almost got it right here:
image.setRGB(x, y, (255*(int)Math.round(kDEvalues[x][y]))/100);
There are three things wrong with your expression for the RGB value. First, you round and then multiply. This drops any fractions of your small values, it should be the other way around. Second you only create a value for one color channel (the blue one) and thrid, you do not check for overflow.
Try something like this:
int gray = (int) Math.round((kDEvalues[x][y] - (-1)) * 127.5);
// limit range to 0-255
gray = Math.min(gray, 255);
gray = Math.max(gray, 0);
// copy gray value to r, g and b channel
int rgb = (gray << 16) | (gray << 8) | gray;
image.setRGB(x, y, rgb);
First operation is to scale the value to 0-255. Since you say your values are in range -1 to 1, adjust the values so the lower bound falls on zero (by subtracting the lower bound). Then multiply by a factor that the highes value becomes 255 (thats (1 - (-1)) * 127.5 = 255).
Just to be safe, clip the resulting value to 0-255 (Math.min/max expressions).
Then replicate the gray value in RGB by setting the gray value to all 3 color channels. The format is documented in the javadoc and the bit shifts just place the gray value at the correct position within the int.

Programmatically generate pixel art from an image

I want to generate a 64x64 pixel art for any image provided by the user. I'm currently lost on how to do it. OpenCV does not seem to provide any such functionality. Any pointers in the right direction would be gladly appreciated.
Edit
Pixel art is a pixelated image of a given image. The screen shot below shows what I've achieved so far.
Basically what I've done is as follows.
Scale the image with aspect ratios maintained so that it would fit within a 64x64 grid
Image sourceImage;
Rectangle imageBound = sourceImage.getBounds();
double sx = (double) 64 / (double) imageBound.width;
double sy = (double) 64 / (double) imageBound.height;
double s = Math.min(sx, sy);
int dx = (int) (s * imageBound.width);
int dy = (int) (s * imageBound.height);
Image scaledImage = new Image(d, sourceImage.getImageData().scaledTo(dx, dy));
Read each pixel from the scaled image and output it on the screen.
Following shows how I extract the color from each pixel. I use SWT framework.
Display d;
ImageData data = scaledImage.getImageData();
for(int i=0; i<data.width; i++) {
for(int j=0; j<data.height; j++) {
int pixel = data.getPixel(i,j);
int red = (pixel & 0x00ff0000) >> 16;
int green = (pixel & 0x0000ff00) >> 8;
int blue = pixel & 0x000000ff;
pixelGrid[i][j].setBackground(new Color(d, red, green, blue));
}
}
But as you can see, there is a major difference in the colors after resizing the image. What I want to know is whether I'm on the correct path to achieve this and if so, how can I retain the actual colors while scaling.
You probably have an image in RGBA format, and are extracting the wrong values from the image (as if it were ARGB). It looks very blue currently, and I fail to see any reds. That should have put you on the right track:
int red = (pixel>>24) & 0xff;
int green = (pixel>>16) & 0xff;
int blue = (pixel>>8) & 0xff;
int alpha = pixel & 0xff; // always 0xff for images without transparency

Converting grayscale image pixels to defined scale

I'm looking to use a very crude heightmap I've created in Photoshop to define a tiled isometric grid for me:
Map:
http://i.imgur.com/jKM7AgI.png
I'm aiming to loop through every pixel in the image and convert the colour of that pixel to a scale of my choosing, for example 0-100.
At the moment I'm using the following code:
try
{
final File file = new File("D:\\clouds.png");
final BufferedImage image = ImageIO.read(file);
for (int x = 0; x < image.getWidth(); x++)
{
for (int y = 0; y < image.getHeight(); y++)
{
int clr = image.getRGB(x, y) / 99999;
if (clr <= 0)
clr = -clr;
System.out.println(clr);
}
}
}
catch (IOException ex)
{
// Deal with exception
}
This works to an extent; the black pixel at position 0 is 167 and the white pixel at position 999 is 0. However when I insert certain pixels into the image I get slightly odd results, for example a gray pixel that's very close to white returns over 100 when I would expect it to be in single digits.
Is there an alternate solution I could use that would yield more reliable results?
Many thanks.
Since it's a grayscale map, the RGB parts will all be the same value (with range 0 - 255), so just take one out of the packed integer and find out what percent of 255 it is:
int clr = (int) ((image.getRGB(x, y) & 0xFF) / 255.0 * 100);
System.out.println(clr);
getRGB returns all channels packed into one int so you shouldn't do arithmetic with it. Maybe use the norm of the RGB-vector instead?
for (int x = 0; x < image.getWidth(); ++x) {
for (int y = 0; y < image.getHeight(); ++y) {
final int rgb = image.getRGB(x, y);
final int red = ((rgb & 0xFF0000) >> 16);
final int green = ((rgb & 0x00FF00) >> 8);
final int blue = ((rgb & 0x0000FF) >> 0);
// Norm of RGB vector mapped to the unit interval.
final double intensity =
Math.sqrt(red * red + green * green + blue * blue)
/ Math.sqrt(3 * 255 * 255);
}
}
Note that there is also the java.awt.Color class that can be instantiated with the int returned by getRGB and provides getRed, getGreen and getBlue methods if you don't want to do the bit manipulations yourself.

Change the alpha value of a BufferedImage?

How do I change the global alpha value of a BufferedImage in Java? (I.E. make every pixel in the image that has a alpha value of 100 have a alpha value of 80)
#Neil Coffey:
Thanks, I've been looking for this too; however, Your code didn't work very well for me (white background became black).
I coded something like this and it works perfectly:
public void setAlpha(byte alpha) {
alpha %= 0xff;
for (int cx=0;cx<obj_img.getWidth();cx++) {
for (int cy=0;cy<obj_img.getHeight();cy++) {
int color = obj_img.getRGB(cx, cy);
int mc = (alpha << 24) | 0x00ffffff;
int newcolor = color & mc;
obj_img.setRGB(cx, cy, newcolor);
}
}
}
Where obj_img is BufferedImage.TYPE_INT_ARGB.
I change alpha with setAlpha((byte)125); alpha range is now 0-255.
Hope someone finds this useful.
I don't believe there's a single simple command to do this. A few options:
copy into another image with an AlphaComposite specified (downside: not converted in place)
directly manipulate the raster (downside: can lead to unmanaged images)
use a filter or BufferedImageOp
The first is the simplest to implement, IMO.
This is an old question, so I'm not answering for the sake of the OP, but for those like me who find this question later.
AlphaComposite
As #Michael's excellent outline mentioned, an AlphaComposite operation can modify the alpha channel. But only in certain ways, which to me are somewhat difficult to understand:
is the formula for how the "over" operation affects the alpha channel. Moreover, this affects the RGB channels too, so if you have color data that needs to be unchanged, AlphaComposite is not the answer.
BufferedImageOps
LookupOp
There are several varieties of BufferedImageOp (see 4.10.6 here). In the more general case, the OP's task could be met by a LookupOp, which requires building lookup arrays. To modify only the alpha channel, supply an identity array (an array where table[i] = i) for the RGB channels, and a separate array for the alpha channel. Populate the latter array with table[i] = f(i), where f() is the function by which you want to map from old alpha value to new. E.g. if you want to "make every pixel in the image that has a alpha value of 100 have a alpha value of 80", set table[100] = 80. (The full range is 0 to 255.) See how to increase opacity in gaussian blur for a code sample.
RescaleOp
But for a subset of these cases, there is a simpler way to do it, that doesn't require setting up a lookup table. If f() is a simple, linear function, use a RescaleOp. For example, if you want to set newAlpha = oldAlpha - 20, use a RescaleOp with a scaleFactor of 1 and an offset of -20. If you want to set newAlpha = oldAlpha * 0.8, use a scaleFactor of 0.8 and an offset of 0. In either case, you again have to provide dummy scaleFactors and offsets for the RGB channels:
new RescaleOp({1.0f, 1.0f, 1.0f, /* alpha scaleFactor */ 0.8f},
{0f, 0f, 0f, /* alpha offset */ -20f}, null)
Again see 4.10.6 here for some examples that illustrate the principles well, but are not specific to the alpha channel.
Both RescaleOp and LookupOp allow modifying a BufferedImage in-place.
for a nicer looking alpha change effect, you can use relative alpha change per pixel (rather than static set, or clipping linear)
public static void modAlpha(BufferedImage modMe, double modAmount) {
//
for (int x = 0; x < modMe.getWidth(); x++) {
for (int y = 0; y < modMe.getHeight(); y++) {
//
int argb = modMe.getRGB(x, y); //always returns TYPE_INT_ARGB
int alpha = (argb >> 24) & 0xff; //isolate alpha
alpha *= modAmount; //similar distortion to tape saturation (has scrunching effect, eliminates clipping)
alpha &= 0xff; //keeps alpha in 0-255 range
argb &= 0x00ffffff; //remove old alpha info
argb |= (alpha << 24); //add new alpha info
modMe.setRGB(x, y, argb);
}
}
}
I'm 99% sure the methods that claim to deal with an "RGB" value packed into an int actually deal with ARGB. So you ought to be able to do something like:
for (all x,y values of image) {
int argb = img.getRGB(x, y);
int oldAlpha = (argb >>> 24);
if (oldAlpha == 100) {
argb = (80 << 24) | (argb & 0xffffff);
img.setRGB(x, y, argb);
}
}
For speed, you could maybe use the methods to retrieve blocks of pixel values.
You may need to first copy your BufferedImage to an image of type BufferedImage.TYPE_INT_ARGB. If your image is of type, say, BufferedImage.TYPE_INT_RGB, then the alpha component won't be set correctly. If your BufferedImage is of type BufferedImage.TYPE_INT_ARGB, then the code below works.
/**
* Modifies each pixel of the BufferedImage so that the selected component (R, G, B, or A)
* is adjusted by delta. Note: the BufferedImage must be of type BufferedImage.TYPE_INT_ARGB.
* #param src BufferedImage of type BufferedImage.TYPE_INT_ARGB.
* #param colorIndex 0=red, 1=green, 2=blue, 3= alpha
* #param delta amount to change component
* #return
*/
public static BufferedImage adjustAColor(BufferedImage src,int colorIndex, int delta) {
int w = src.getWidth();
int h = src.getHeight();
assert(src.getType()==BufferedImage.TYPE_INT_ARGB);
for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++) {
int rgb = src.getRGB(x,y);
java.awt.Color color= new java.awt.Color(rgb,true);
int red=color.getRed();
int green=color.getGreen();
int blue=color.getBlue();
int alpha=color.getAlpha();
switch (colorIndex) {
case 0: red=adjustColor(red,delta); break;
case 1: green=adjustColor(green,delta); break;
case 2: blue=adjustColor(blue,delta); break;
case 3: alpha=adjustColor(alpha,delta); break;
default: throw new IllegalStateException();
}
java.awt.Color adjustedColor=new java.awt.Color(red,green,blue,alpha);
src.setRGB(x,y,adjustedColor.getRGB());
int gottenColorInt=src.getRGB(x,y);
java.awt.Color gottenColor=new java.awt.Color(gottenColorInt,true);
assert(gottenColor.getRed()== red);
assert(gottenColor.getGreen()== green);
assert(gottenColor.getBlue()== blue);
assert(gottenColor.getAlpha()== alpha);
}
return src;
}
private static int adjustColor(int value255, int delta) {
value255+= delta;
if (value255<0) {
value255=0;
} else if (value255>255) {
value255=255;
}
return value255;
}

Categories