Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Pixel Problem.  (Read 910 times)
0 Members and 1 Guest are viewing this topic.
Offline Andre Lopes
« Posted 2014-03-10 02:01:29 »

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 :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
 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;

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.
Offline Andre Lopes
« Reply #1 - Posted 2014-03-10 03:12:29 »

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

But should i use getInt(index) Huh
Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #2 - Posted 2014-03-10 03:33:46 »

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

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Andre Lopes
« Reply #3 - Posted 2014-03-10 15:08:27 »

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 Tongue

Offline trollwarrior1
« Reply #4 - Posted 2014-03-10 15:17:32 »

Do system.out.println and you will see the value representation of the pixel.
Offline Gibbo3771
« Reply #5 - Posted 2014-03-10 15:50:29 »

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 Tongue



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:

1  
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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
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.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline Andre Lopes
« Reply #6 - Posted 2014-03-10 16:23:55 »

Ty guys.
Im still working on it Tongue

1  
2  
 Pixmap pm = new Pixmap(Gdx.files.internal("etc/pixmapTest.png"));
        System.out.println("Format :" + pm.getFormat());



1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
 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;
    }


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
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.
Offline Troubleshoots

JGO Knight


Medals: 36
Exp: 7-9 months


Damn maths.


« Reply #7 - Posted 2014-03-10 16:26:47 »

Don't use Strings, it's unnecessary.

Why are all OpenGL tutorials written in Brainf**k?
Offline Andre Lopes
« Reply #8 - Posted 2014-03-10 16:30:05 »

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.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
 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;
    }
Offline JVallius
« Reply #9 - Posted 2014-03-10 16:53:00 »

Never use jpeg, prefer png.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Andre Lopes
« Reply #10 - Posted 2014-03-10 17:11:52 »

Never use jpeg, prefer png.

Ty.
Now its working lol

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
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)
Offline Gibbo3771
« Reply #11 - Posted 2014-03-10 20:32:11 »

Don't use Strings, it's unnecessary.

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.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #12 - Posted 2014-03-10 22:47:59 »

Don't use Strings, it's unnecessary.

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:

1  
2  
3  
4  
5  
6  
7  
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 Smiley

EDIT:

Oh, now that I'm at it...

Try this out:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
// 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:
"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);

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline Andre Lopes
« Reply #13 - Posted 2014-03-12 05:32:32 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
  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)
    {
       
       
       
    }
Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (12 views)
2014-08-01 22:53:16

CogWheelz (14 views)
2014-08-01 22:51:43

CopyableCougar4 (15 views)
2014-08-01 19:37:19

CogWheelz (19 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (35 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36
Resources for WIP games
by CogWheelz
2014-08-01 18:20:17

Resources for WIP games
by CogWheelz
2014-08-01 18:19:50

List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!