1

this is what I've gone so far and I can't seem to go further because I don't understand bitwise operations on the RGB

// Read from a file
File file = new File("src/a.gif");
image = ImageO.read(file);
int[] rgbarr = image.getRGB(0, 0, 13, 15, null, 0, 13);// image is 13*15 
System.out.println(rgbarr.length);
for (int i : rgbarr)
    {
        System.out.println(i);
    }

Output: was values such as -16777216 and -1 Because I've already made an image black and white just to ease my understanding

But in our case here let's suppose it would be just random image , how do I get from normal RGB image to binary values (0 or 1) for each pixel.

3 Answers 3

2

I'm betting that each int in the array is a packed ARGB value;

boolean [] output = new boolean[rgbarr.length];
for ( int i=0; i<rgbarr.length; ++i ) {
  int a = (rgbarr[i] >> 24 ) & 0x0FF;
  int r = (rgbarr[i] >> 16 ) & 0x0FF;
  int g = (rgbarr[i] >> 8  ) & 0x0FF;
  int b = (rgbarr[i]       ) & 0x0FF;
  output[i] = (0.30*r + 0.59*g + 0.11*b) > 127;
}

The output equation I choose was taken from a definition of Luminance from wikipedia. You can change the scales so long as the coefficients add up to 1.

Sign up to request clarification or add additional context in comments.

1 Comment

the 127 made it all false, I tried (1) and it worked fine, donno why though :)
1

You can do some thing like this,

BufferedImage img = (BufferedImage) image;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(img , "jpg", baos);
byte[] previewByte = baos.toByteArray();

Hope this helps!!

Comments

0

You already have RGB values in the int[], that is color of each pixel. You could compare this to a specific background color, like black, to get a 1-bit pixel value. Then maybe set a bit in another appropriately sized byte[] array...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.