Author Topic: image.getRGB(int x, int y) always returns the same  (Read 1514 times)

0 Members and 1 Guest are viewing this topic.

Offline DreX

  • Serf
  • *
  • Posts: 42
  • Cookies: -5
    • View Profile
image.getRGB(int x, int y) always returns the same
« on: September 15, 2014, 05:08:34 pm »
The problem is that the image.getRGB(int x, int y); always returns the same amount.
The code:

Code: (java) [Select]
public class ReferencialNumbers  {   
    public static void main (String [] args) throws IOException {
        int x=762;
        int y1=356;
        int y2=369;

        int rgb=0;
        for (int i=0; i<(y2-y1); i++){
            rgb+=getNumber(x, (y1+i));
        }
        System.out.println ("RGB : "+rgb);
    }
    public static int getNumber(int x, int y) throws IOException{
        BufferedImage image=ImageIO.read(new File("C:/Users/User/Desktop/chalx.png"));
        int rgb;
        rgb=image.getRGB(x,y);
        return rgb;
    }
}

I go through a set coloumn of pixels to try and get the number out of the picture.
I go through pixels indicated by the red line (going through 8-the line is "imaginable"), but even if the number changes and I go through the same pixels, I get the same value.

I am looking for a way to get a different value if the number changes.

Staff note: we have code tags, ya know.
« Last Edit: September 15, 2014, 05:13:51 pm by Kulverstukas »

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: image.getRGB(int x, int y) always returns the same
« Reply #1 on: September 16, 2014, 12:57:51 pm »
Given that the coordinates are correct (which only you can know) have you considered that the amount of "color" needed for the number in that column actually is the same for both numbers?
Why do you call the variable rgb if it is actually a sum?

Think about something that really changes for all numbers. This is, e.g., where you encounter a different color and how often.
Here is a start to work with, so you get some output:

Code: (Java) [Select]
public static void main(String[] args) throws IOException {
BufferedImage image = ImageIO.read(new File(
"C:/Users/User/Desktop/chalx.png"));
int x=762;
                int y1=356;
int y2 = image.getHeight();
int referencePixel = image.getRGB(x, y1);

for (int i = 0; i < (y2 - y1); i++) {
int currentPixel = image.getRGB(x, (y1 + i));
if (currentPixel != referencePixel) {
System.out.println(colorDescription(currentPixel));
}
}
}

public static String colorDescription(int rgb) {
Color color = new Color(rgb);
return color.getRed() + ", " + color.getGreen() + ", "
+ color.getBlue();
}

If the output does not show any changed pixels, you have the wrong column.

Besides that a little tip: Don't read in the whole image every time you want to check one little pixel. This will slow down everything a lot, file IO is costly.
« Last Edit: September 16, 2014, 12:58:28 pm by Deque »

Offline DreX

  • Serf
  • *
  • Posts: 42
  • Cookies: -5
    • View Profile
Re: image.getRGB(int x, int y) always returns the same
« Reply #2 on: September 16, 2014, 08:20:06 pm »
Given that the coordinates are correct (which only you can know) have you considered that the amount of "color" needed for the number in that column actually is the same for both numbers?
Why do you call the variable rgb if it is actually a sum?

I have considered this and I thought I would solve this by taking in a column, though I see I might be wrong.
rgb is just a name, didn't put much thought into naming the variables.


Think about something that really changes for all numbers. This is, e.g., where you encounter a different color and how often.
Here is a start to work with, so you get some output:

Code: (Java) [Select]
public static void main(String[] args) throws IOException {
      BufferedImage image = ImageIO.read(new File(
            "C:/Users/User/Desktop/chalx.png"));
      int x=762;
                int y1=356;
      int y2 = image.getHeight();
      int referencePixel = image.getRGB(x, y1);
     
      for (int i = 0; i < (y2 - y1); i++) {
         int currentPixel = image.getRGB(x, (y1 + i));
         if (currentPixel != referencePixel) {
            System.out.println(colorDescription(currentPixel));
         }
      }
}

public static String colorDescription(int rgb) {
      Color color = new Color(rgb);
      return color.getRed() + ", " + color.getGreen() + ", "
            + color.getBlue();
}

If the output does not show any changed pixels, you have the wrong column.

Besides that a little tip: Don't read in the whole image every time you want to check one little pixel. This will slow down everything a lot, file IO is costly.

This should be good to work with, thank you.