I'm looping through an image with X and Y coordinates. However, I can only set an sRGB value using the method: image.setRGB(int x, int y, int rgb);
I want to set a custom RED, GREEN and BLUE values to the current X and Y coordinates. The new RGB values are in this case the average of the original RGB values.
The result I get is a blue image instead of greyscale.
for (int y = 0; y < imageInput.getHeight(); y++) {
for (int x = 0; x < imageInput.getWidth(); x++) {
int clr = imageInput.getRGB(x, y);
Color color = new Color(clr);
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
int sumRGB = red + green + blue;
int avg = sumRGB/3;
System.out.println("sRGB: " + clr);
System.out.println("Input Image: x= " + x + " y= " + y + " [" + red + "," + green + "," + blue + "]");
System.out.println("Avg RGB: " + avg);
imageCopy.setRGB(x, y, avg);
int clrCopy = imageCopy.getRGB(x, y);
color = new Color(clrCopy);
red = color.getRed();
green = color.getGreen();
blue = color.getBlue();
System.out.println("Output Image: x= " + x + " y= " + y + " [" + red + "," + green + "," + blue + "]\n");
}
}
You could write
Color greyScaleColor = new Color(avg, avg, avg);
int rgbValueForGrey = greyScaleColor.getRGB();
This will give you the int value that you need to set in imageCopy.
Related
So i'm trying to get a byte array of compressed data in an image, the maker told me that I need to read every color channel.
Here's my code so far:
byte[] RGBAb = null;
for (int x = 0; x < bitmap.getWidth(); x++)
{
for (int y = 0; y < bitmap.getHeight(); y++)
{
int mask = 0xFF;
Integer R = Math.abs(bitmap.getPixel(x, y) >> 16 & mask);
Integer G = Math.abs(bitmap.getPixel(x, y) >> 8 & mask);
Integer B = Math.abs(bitmap.getPixel(x, y) & mask);
Integer A = Math.abs(bitmap.getPixel(x, y) >> 24 & mask);
byte Rb = R.byteValue();
byte Gb = G.byteValue();
byte Bb = B.byteValue();
byte Ab = A.byteValue();
RGBAb = new byte[] { Rb, Gb, Bb, Ab};
//System.out.println("Red" + R + "\n Red to bytes: " + Rb);
//System.out.println("Green" + G + "\n Green to bytes: " + Gb);
//System.out.println("Blue" + B + "\n Blue to bytes: " + Bb);
//System.out.println("Alpha" + A + "\n Alpha to bytes: " + Ab);
System.out.println("RGBA Bytes COMBINED \n" + RGBAb);
}
}
I dont know if it's outputting the right value.
This is the photo im reading:
Click Me
Output of hue image
I am trying the image from RGB to HSI in java and then I want to show three images:
H- hue image, S - Saturation image, I- Intensity image
private void jButton_Convert_HSIActionPerformed(java.awt.event.ActionEvent evt)
{
// TODO add your handling code here:
BufferedImage image = null;
BufferedImage processed_hue, processed_sat, processed_in;
try {
image = ImageIO.read(new File("C:\\Users\\ATH\\Desktop\\photo\\Rotate\\test_rotate_lena.png"));
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
int width = image.getWidth();
int height = image.getHeight();
processed_hue = new BufferedImage(width, height, image.getType());
processed_sat = new BufferedImage(width, height, image.getType());
processed_in = new BufferedImage(width, height, image.getType());
for (int y = 0; y < width; y++) //thuc hien lap cho y chay tu o den chieu cao anh
{
for (int x = 0; x < height; x++) //thuc hien lap cho x chay tu 0 den chieu rong
{
Color c = new Color(image.getRGB(x, y));
float h = 0;
float r, g, b, s, i, theta;
// RGB TO HSI CONVERSION
int red = c.getRed();
int green = c.getGreen();
int blue = c.getBlue();
float total = red + green + blue;
r = red / total;
g = green / total;
b = blue / total;
System.out.println("normalize red " + r);
System.out.println(" red = " + red + " green = " + green + " blue = " + blue);
s = 1 - (3 * Math.min(r, Math.min(g, b)));
i = total / (3 * 255);
theta = (float) Math.acos((0.5 * ((r - g) + (r - b))) / Math.pow((((r - g) * (r - g)) + ((r - b) * (g - b))), 1 / 2));
System.out.println(" theta " + theta);
if (b <= g) {
// h = ((1 / Math.cos((0.5 * (2 * r - g - b)) / (Math.sqrt(((r - g) * (r - g) + (r - b) * (g - b)))))));
h = theta;
} else {
h = (float) ((2 * Math.PI) - theta);
}
System.out.println("Intial hue = " + h + " saturation = " + s + " intensity = " + i);
float h1 = (float) (h * (180 / Math.PI)); //to change Radian to Degree - multiply 180/pi
float s1 = (s * 100);
float i1 = (i * 255);
System.out.println(" Hue = " + Math.round(h1) + " Saturation = " + Math.round(s1) + " Intensity = " + Math.round(i1));
// hue
float HSV[] = new float[3];
// Color.RGBtoHSB(red, green, blue, HSV);
processed_hue.setRGB(x, y, Color.getHSBColor(h1, HSV[1], HSV[2]).getRGB());
// processed_sat.setRGB(x, y, Color.getHSBColor(HSV[0], s1, HSV[2]).getRGB());
// processed_in.setRGB(x, y, Color.getHSBColor(HSV[0], HSV[1], i1).getRGB());
}
} // end for loop
try {
// save images
ImageIO.write(processed_hue, "png", new File("C:\\Users\\ATH\\Desktop\\photo\\HSI\\hue_image.png"));
ImageIO.write(processed_sat, "png", new File("C:\\Users\\ATH\\Desktop\\photo\\HSI\\sat_image.png"));
ImageIO.write(processed_in, "png", new File("C:\\Users\\ATH\\Desktop\\photo\\HSI\\in_image.png"));
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
System.out.println("Image is saved in D drive: ");
/*
try {
File sat_out1 = new File("C:\\Users\\ATH\\Desktop\\photo\\HSI\\sat_image.png");
ImageIO.write(sat_image, "png", sat_out1);
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
System.out.println("Image is saved in D drive: ");
try {
File inten_out1 = new File("C:\\Users\\ATH\\Desktop\\photo\\HSI\\inten_image.png");
ImageIO.write(inten_image, "png", inten_out1);
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
System.out.println("Image is saved in D drive: ");
*/
}
Please advice me and help me. How can I get H image, S image and I image?
Right now i am trying to make a game. I am having problems with loading my images. To do so I have to give an image to the removeImageBackground(BufferedImage img, Color tc) method in my Main class. Basically what this method does is take the image specified in the parameters (img). What happens next is it goes through the image with some for statements and if it finds a pixel who's color matches the color specified in the parameters (tc) it changes that pixel to be transparent. I have no problem with this but then what happens next in this method is the image is then expanded (this has to be done because my game has 8-bit graphics). anyway it makes a new bufferedimage named outImg. I have 4 nested for statements after that. The first 2 cycle through the original img image and the last 2 cycle through the new outImg image. What this does is reads the original img image while writing 5x5 pixel squares into the new outImg image in the corresponding locations. But it's at this line of code that I have a problem. I keep getting arrayindexoutofboundsexceptions. I tried putting in System.out.println() statements to try to debug, but I can't seem to figure out what's wrong. Here's the part of my code where I'm having problems, along with my error messages and my debug results:
My removeImageBackground method from my Main Class:
//removed the background of the given image with the given transparent color
public static BufferedImage removeImageBackground(BufferedImage img, Color tc) {
System.out.println("Method Fired:Main.removeImageBackground(BufferedImage img, Color tc)"); //debug
//remove the background
for(int y = 0; y < img.getHeight(); y++) {
for(int x = 0; x < img.getWidth(); x++) {
Color c = new Color(img.getRGB(x, y));
if(c == tc) {
c = new Color(c.getRed(), c.getGreen(), c.getBlue(), 255);
}
img.setRGB(x, y, c.getRGB());
}
}
//expand the image
BufferedImage outImg = new BufferedImage(img.getWidth() * graphicsScale, img.getHeight() * graphicsScale, BufferedImage.TYPE_INT_ARGB);
System.out.println("outImg height=" + outImg.getHeight());
System.out.println("outImg width=" + outImg.getWidth());
for(int y = 0; y < img.getHeight(); y++) {
System.out.println("for y=" + y);
System.out.println("img height=" + img.getHeight());
for(int x = 0; x < img.getWidth(); x++) {
System.out.println("for x=" + x);
System.out.println("img width=" + img.getWidth());
for(int y2 = 0; y2 < graphicsScale; y++) {
for(int x2 = 0; x2 < graphicsScale; x++) {
outImg.setRGB((x * graphicsScale) + x2, (y * graphicsScale) + y2, img.getRGB(x, y));
}
}
}
}
return outImg;
}
My entire Gale Class (Which is specified in My error messages):
package entity;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.awt.Color;
import java.io.File;
import java.io.IOException;
import main.Main;
public class Gale extends Player {
//variables
private static BufferedImage downStand;
private static BufferedImage leftStand;
private static BufferedImage rightStand;
private static BufferedImage upStand;
private static BufferedImage downWalk;
private static BufferedImage leftWalk;
private static BufferedImage rightWalk;
private static BufferedImage upWalk;
private static String objectName = "Gale";
//constructors
//used ONLY when loading
public Gale() throws IOException {
System.out.println("Constructor Fired:Gale()"); //debug
downStand = Main.removeImageBackground(ImageIO.read(new File(Main.filePath + Main.directory + Main.resourceFile + Main.directory + objectName + Main.directory + "DownStand.jpg")), Main.transparentColor);
leftStand = Main.removeImageBackground(ImageIO.read(new File(Main.filePath + Main.directory + Main.resourceFile + Main.directory + objectName + Main.directory + "LeftStand.jpg")), Main.transparentColor);
rightStand = Main.removeImageBackground(ImageIO.read(new File(Main.filePath + Main.directory + Main.resourceFile + Main.directory + objectName + Main.directory + "RightStand.jpg")), Main.transparentColor);
upStand = Main.removeImageBackground(ImageIO.read(new File(Main.filePath + Main.directory + Main.resourceFile + Main.directory + objectName + Main.directory + "UpStand.jpg")), Main.transparentColor);
downWalk = Main.removeImageBackground(ImageIO.read(new File(Main.filePath + Main.directory + Main.resourceFile + Main.directory + objectName + Main.directory + "DownWalk.jpg")), Main.transparentColor);
leftWalk = Main.removeImageBackground(ImageIO.read(new File(Main.filePath + Main.directory + Main.resourceFile + Main.directory + objectName + Main.directory + "LeftWalk.jpg")), Main.transparentColor);
rightWalk = Main.removeImageBackground(ImageIO.read(new File(Main.filePath + Main.directory + Main.resourceFile + Main.directory + objectName + Main.directory + "RightWalk.jpg")), Main.transparentColor);
upWalk = Main.removeImageBackground(ImageIO.read(new File(Main.filePath + Main.directory + Main.resourceFile + Main.directory + objectName + Main.directory + "UpWalk.jpg")), Main.transparentColor);
}
//to be used for anything else
public Gale(int x, int y) {
System.out.println("Constructor Fired:Gale(int x, int y)"); //debug
}
//methods
//returns the images
public BufferedImage getDownStand() {
System.out.println("Method Fired:Gale.getDownStand()"); //debug
return downStand;
}
public BufferedImage getLeftStand() {
System.out.println("Method Fired:Gale.getLeftStand()"); //debug
return leftStand;
}
public BufferedImage getRightStand() {
System.out.println("Method Fired:Gale.getRightStand()"); //debug
return rightStand;
}
public BufferedImage getUpStand() {
System.out.println("Method Fired:Gale.getUpStand()"); //debug
return upStand;
}
public BufferedImage getDownWalk() {
System.out.println("Method Fired:Gale.getDownWalk()"); //debug
return downWalk;
}
public BufferedImage getLeftWalk() {
System.out.println("Method Fired:Gale.getLeftWalk()"); //debug
return leftWalk;
}
public BufferedImage getRightWalk() {
System.out.println("Method Fired:Gale.getRightWalk()"); //debug
return rightWalk;
}
public BufferedImage getUpWalk() {
System.out.println("Method Fired:Gale.getUpWalk()"); //debug
return upWalk;
}
}
My Debug Results:
Method Fired:Main.removeImageBackground(BufferedImage img, Color tc)
outImg height=250
outImg width=125
for y=0
img height=50
for x=0
img width=25
My Error Messages:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
at sun.awt.image.ByteInterleavedRaster.getDataElements(ByteInterleavedRaster.java:318)
at java.awt.image.BufferedImage.getRGB(BufferedImage.java:888)
at main.Main.removeImageBackground(Main.java:101)
at entity.Gale.<init>(Gale.java:28)
at main.Main.load(Main.java:186)
at main.Main.main(Main.java:69)
That's pretty much it. My main method just calls on my load method. And my load method just calls on my Gale() constructor. So there shouldn't be a problem in either of those.
Someone please help me fix this. I'm really excited to get this game finished and this is becoming a very big problem for me since it is preventing me from loading everything.
Really quickly...
for(int y2 = 0; y2 < graphicsScale; y++) {
for(int x2 = 0; x2 < graphicsScale; x++) {
You are incrementing x and y within your inner loop, which I believe you want to be incrementing x2 and y2
for(int y2 = 0; y2 < graphicsScale; y2++) {
for(int x2 = 0; x2 < graphicsScale; x2++) {
Original...
Scaled (4x)
i am trying to make two screenshots with 6 seconds difference, to see if there is some changes on the website.
But my code says me that the screenshots are always different, even if i test it without any changing on the screen.
what am i doing wrong?
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screensize = toolkit.getScreenSize();
Rectangle rectangle = new Rectangle(0,0,screensize.width,screensize.height);
Robot robot = new Robot();
BufferedImage image1 = robot.createScreenCapture(rectangle);
System.out.println("screenshot "+i+"");
Thread.sleep(6000);
BufferedImage image2 = robot.createScreenCapture(rectangle);
System.out.println("screenshot "+(i+10)+"");
int x1 = image1.getWidth();
int x2 = image2.getWidth();
if ( x1 != x2 ) {
System.out.println( "Widths are different: " + x1 + " != " + x2 );
return;
}
int y1 = image1.getHeight();
int y2 = image2.getHeight();
if ( y1 != y2 ) {
System.out.println( "Heights are different: " + y1 + " != " + y2 );
return;
}
for ( int x = 0; x < x1; x++ ) {
for ( int y = 0; y < y1; y++ ){
int p1 = image1.getRGB( x, y );
int p2 = image2.getRGB( x, y );
if ( p1 != p2 ) {
System.out.println("Pixel is different at x/y " + x + "/" + y + ": " + p1 + " != " + p2 );
return;
}
}
}
System.out.println( "Images are identical" );
I tried your code and my pixel is different because of a blinking cursor in Eclipse Console.
Then I had a problem with an animated icon (process explorer in task bar)
Finally it said Image identical.
Note : Mouse isn't part of the thing :
Creates an image containing pixels read from the screen. This image does not include the mouse cursor.
I have a byte array:
byte[] blue_color = {-1,0,112,-64};
How to convert into byte array of RGB?
And also how can I get the real RGB value of the color?
Assuming it's the first element that's the A component:
byte[] rgb = Arrays.copyOfRange(blue_color, 1, 4);
To get the "real" colour values, you need to undo the two's complement representation:
int x = (int)b & 0xFF;
How to convert ARGB array into RGB?
byte[] argb = ...;
byte[] rgb = new byte[(argb.length / 4) * 3];
int index = rgb.length - 1;
for (int i = argb - 1; i >= 0; i -= 4) {
rgb[index--] = argb[i];
rgb[index--] = argb[i - 1];
rgb[index--] = argb[i - 2];
}
How to print integer value:
byte[] oneColor = {..., ..., ..., ...};
int alpha = oneColor[0] & 0xFF;
int red = oneColor[1] & 0xFF;
int green = oneColor[2] & 0xFF;
int blue = oneColor[3] & 0xFF;
System.out.println("Color: " + alpha + ", " + red + ", " + green ", " + blue);
System.out.println("Hexa color: 0x" + Integer.toHexString(alpha) + " " + Integer.toHexString(red) + " " + Integer.toHexString(green) + " " + Integer.toHexString(blue));
Could be done prettier with printf.
How to convert into byte array of RGB?
byte[] rgb = new byte[3];
System.arraycopy(blue_color, 1, rgb, 0, 3);
And also how can I get the real RGB value of the color?
int red = rgb[0] >= 0 ? rgb[0] : rgb[0] + 256;
int green = rgb[1] >= 0 ? rgb[1] : rgb[1] + 256;
int blue = rgb[2] >= 0 ? rgb[2] : rgb[2] + 256;