Pixel Problem.

Hi Guys!
Im trying to make a big Hopfield ANN but i find myself with a libgdx issue.

Heres what im doing :

A) Read an image which just have either complete white dots or black dots ( transparency will be seem as white), though i prefer to work with RGB and not RGBA.

B)Make an array of boolean.False for white pixel and black for a black pixel.
//Unsure if i need to make a 2D array for the hopfield stuff… BurntPizza?

C) … Hopfield stuff

Well, i have this :

 public boolean[] getImageBoolean(Pixmap image) {

        //0 = white
        //1 = Black
        boolean[] result;

        Color response = new Color();

        ByteBuffer pixels = image.getPixels();

        
              
        
       

        return result;
    }

In the white space in the middle, i had some code that was reading int and printing some weird stuff.

Then i was able to find this :

[quote]Color color = new Color();
Color.rgba8888ToColor(color, pixmap.getPixel(x,y));
pixmap.dispose();
return color.r;
[/quote]
I did that.But i found myself with another problem.
How can i limit my for statement? i mean, how can i check the size of the image in pixels?

I ve been stalled in some hours by this…

Any tip is appreciated.

“The height of the Pixmap in pixels.”
getHeight() solves one problem.

But should i use getInt(index) ???

Are you asking how to get the size of the pixmap in terms of pixels? Just to clarify.

I was then someone in chat told me about getHeight.

For some dumb reason i didnt see it.

Now i just need help about how to identify a black pixel and a white pixel :stuck_out_tongue:

Do system.out.println and you will see the value representation of the pixel.

Several ways, you can use a print statement on the pixel at the x y of the loop and get the value of it, it will probably output it as an integer, if you want you can convert this to a hex using:


Integer.toHexString(int value);

Lots of ways you can do it, I currently read my pixels from an image, convert the value to binary and then compare this to a enum that holds possible tile types and then create the correct object at that coordinate.

So basically you want to setup 2 fields to hold the hex, binary, integer value of the pixel, one for white and one for black.
Usual nested loop for x and for y, read the value into a local variable, convert to whatever value you want (hex string, binary string or keep as integer), then you can use a switch statement inside the loop to decide what to put in your 2D array.

Wrote some, probably inaccurate code but should work when fixed, concept is the same:



BufferedImage image;
		boolean[][] pixels = new boolean[image.getWidth()][image.getHeight()];
		String white = "0x00";
		String black = "0xFF";
		for(int x = 0; x < image.getWidth(); x++){
			for(int y = 0; y < image.getHeight(); y++){
				String hexValue = Integer.toHexString(image.getRGB(x, y));
				switch(hexValue){
				case white:
					pixels[x][y] = true;
					break;
				case black:
					pixels[x][y] = false;
					break;
					default:
						throw new someException("Pixel color not recognized at X:" + x + " Y:" + y);
				}
			}
		}


EDIT: Didn’t realise you were using LibGDX, same thing applies except you use Pixmap and getPixels, also dispose of the pixmap in a finally block of your try block.

Ty guys.
Im still working on it :stuck_out_tongue:

 Pixmap pm = new Pixmap(Gdx.files.internal("etc/pixmapTest.png"));
        System.out.println("Format :" + pm.getFormat());
 public boolean[] getImageBoolean(Pixmap image) {

        //0 = white
        //1 = Black
        boolean[] result = new boolean[image.getHeight() * image.getWidth()];
        int index = 0;

        System.out.println("Size :" + result.length);

        String white = "0x00";
        String black = "0xFF";

        for (int x = 0; x < image.getWidth(); x++) {
            for (int y = 0; y < image.getHeight(); y++) {

                String hexValue = Integer.toHexString(image.getPixel(x, y));
                System.out.println("hex Value :" + hexValue);
                if (hexValue.equalsIgnoreCase(white)) {
                     System.out.println("White Pixel Recognized at [" + x + ";" + y + "]");
                    result[index] = false;
                } else if (hexValue.equalsIgnoreCase(black)) {
                     System.out.println("Black Pixel Recognized at [" + x + ";" + y + "]");
                     result[index] = true;
                } else {
                    System.out.println("Pixel Not Recognized at [" + x + ";" + y + "]");
                }

                index++;
            }

        }

        return result;
    }

Format :RGBA8888
Size :6
hex Value :ff
Pixel Not Recognized at [0;0]
hex Value :fefefeff
Pixel Not Recognized at [1;0]
hex Value :10101ff
Pixel Not Recognized at [2;0]
hex Value :ffffffff
Pixel Not Recognized at [3;0]
hex Value :ff
Pixel Not Recognized at [4;0]
hex Value :ffffffff
Pixel Not Recognized at [5;0]

Going to try other methods now.
Maybe change the image to jpeg.

Don’t use Strings, it’s unnecessary.

Yeah i will put integers again But, the last pixel is not being recognized.
Its a white Pixel.

Its a Jpg image.


run:
Format :RGB888
Size :14


hex Value :ff
Pixel Value :255
Black Pixel Recognized at [0;0]


hex Value :ffffffff
Pixel Value :-1
White Pixel Recognized at [0;1]


hex Value :ffffffff
Pixel Value :-1
White Pixel Recognized at [1;0]


hex Value :ff
Pixel Value :255
Black Pixel Recognized at [1;1]


hex Value :ff
Pixel Value :255
Black Pixel Recognized at [2;0]


hex Value :ffffffff
Pixel Value :-1
White Pixel Recognized at [2;1]


hex Value :ffffffff
Pixel Value :-1
White Pixel Recognized at [3;0]


hex Value :ff
Pixel Value :255
Black Pixel Recognized at [3;1]


hex Value :ff
Pixel Value :255
Black Pixel Recognized at [4;0]


hex Value :ffffffff
Pixel Value :-1
White Pixel Recognized at [4;1]


hex Value :ffffffff
Pixel Value :-1
White Pixel Recognized at [5;0]


hex Value :ff
Pixel Value :255
Black Pixel Recognized at [5;1]


hex Value :ff
Pixel Value :255
Black Pixel Recognized at [6;0]


hex Value :fefefeff
Pixel Value :-16843009
Pixel Not Recognized at [6;1]
Java Result: -1
CONSTRUÍDO COM SUCESSO (tempo total: 3 segundos)

I Edited the code

 public boolean[] getImageBoolean(Pixmap image) {

        //0 = white
        //1 = Black
        boolean[] result = new boolean[image.getHeight() * image.getWidth()];
        int index = 0;

        System.out.println("Size :" + result.length);

        int black = 255;
        int white = -1;

        for (int x = 0; x < image.getWidth(); x++) {
            for (int y = 0; y < image.getHeight(); y++) {

                int pixel = image.getPixel(x, y);
                System.out.println("\n Index");
                System.out.println("Pixel Value :" + pixel);

                if (pixel == white) {
                    System.out.println("White Pixel Recognized at [" + x + ";" + y + "]");
                    result[index] = false;
                } else if (pixel == black) {
                    System.out.println("Black Pixel Recognized at [" + x + ";" + y + "]");
                    result[index] = true;
                } else {
                    System.out.println("Pixel Not Recognized at [" + x + ";" + y + "]");
                }

                index++;
            }

        }

        return result;
    }

Never use jpeg, prefer png.

Ty.
Now its working lol

run:
Format :RGBA8888
Size :14

 Index
Pixel Value :255
Black Pixel Recognized at [0;0]

 Index
Pixel Value :-1
White Pixel Recognized at [0;1]

 Index
Pixel Value :-1
White Pixel Recognized at [1;0]

 Index
Pixel Value :255
Black Pixel Recognized at [1;1]

 Index
Pixel Value :255
Black Pixel Recognized at [2;0]

 Index
Pixel Value :-1
White Pixel Recognized at [2;1]

 Index
Pixel Value :-1
White Pixel Recognized at [3;0]

 Index
Pixel Value :255
Black Pixel Recognized at [3;1]

 Index
Pixel Value :255
Black Pixel Recognized at [4;0]

 Index
Pixel Value :-1
White Pixel Recognized at [4;1]

 Index
Pixel Value :-1
White Pixel Recognized at [5;0]

 Index
Pixel Value :255
Black Pixel Recognized at [5;1]

 Index
Pixel Value :255
Black Pixel Recognized at [6;0]

 Index
Pixel Value :-1
White Pixel Recognized at [6;1]
Java Result: -1
CONSTRUÍDO COM SUCESSO (tempo total: 4 segundos)

It depends, hex colours are a lot more easy to read and copy/paste from your photo Editor or whatever.

It’s not a huge hit on the performance since you will likely be doing it at load and not during the program itself.

Gimme a second:


int whitePixel = 0xFF; // Hexadecimal integer decimals...
int blackPixel = 0x00;

// ...
if (pixel == whitePixel) ...
else if (pixel == blackPixel) ...
else ...

It’s what Notch did in one of his LD48 games, btw :slight_smile:

EDIT:

Oh, now that I’m at it…

Try this out:


// Numbers increasing by 30... do they?
public static int[] myInts = new int[] {
    011,
    041,
    071,
    101,
    131
};

public static void main(String... args) {
    for (int i = 0; i < myInts.length; i++) {
        System.out.println(myInts[i]);
    }
}

(warning: Just typed in here, might not compile…)

Explanation:
[spoiler]“0x” in front of a normal number in java means it’s a hexadecimal literal (base 16: 0-9 and then A/a-F/f).
Only “0” in front of a normal number in java means it’s a octal literal (base 8: 0-7);
[/spoiler]

How do i go the way back,lol
Do i just keep puting pixels on the pixmap?

  public boolean[] getBooleanOfImage(Pixmap image) {

        //0 = white
        //1 = Black
        boolean[] result = new boolean[image.getHeight() * image.getWidth()];
        int index = 0;

        System.out.println("Size :" + result.length);

        int black = 255;
        int white = -1;

        for (int x = 0; x < image.getWidth(); x++) {
            for (int y = 0; y < image.getHeight(); y++) {

                int pixel = image.getPixel(x, y);
                System.out.println("\n Index");
                System.out.println("Pixel Value :" + pixel);

                if (pixel == white) {
                    System.out.println("White Pixel Recognized at [" + x + ";" + y + "]");
                    result[index] = false;
                } else if (pixel == black) {
                    System.out.println("Black Pixel Recognized at [" + x + ";" + y + "]");
                    result[index] = true;
                } else {
                    System.out.println("Pixel Not Recognized at [" + x + ";" + y + "]");
                }

                index++;
            }

        }

        return result;
    }

    public Texture getTextureFromBoolean(boolean[] result)
    {
        
        
        
    }