0

I want to analysis the pixel of lena.bmp (matlab) but I don't know how to do it in java and store it to byte[]

I have read this aritle:

how to convert image to byte array in java?

But when I implement, I find some pixel value that did't exist. For example , the pixel range is 0~255, but I can not find '120' on pixel of this photo(lena.bmp).

There is my code

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Image_IO;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

/**
 *
 * @author user
 */
public class ReadImage {
    public static void main (String []args){
        byte[] imageInByte;
        int [] kindOfPixel = new int [256];
        try{
            BufferedImage originalImage = ImageIO.read(new File("C:\\Users\\user\\Desktop\\Project\\LSB_implement\\Lena.bmp"));
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write( originalImage, "bmp", baos );
            baos.flush();
            imageInByte = baos.toByteArray();
            System.out.println(imageInByte.length);
            baos.close();

            for(int i=0;i<imageInByte.length;i++){
                kindOfPixel[imageInByte[i]+128]++;  //java byte defined -128~127  (+128)==> 0~256
            }

            for(int i=0;i<kindOfPixel.length;i++){  //show the pixel color value
                System.out.println(i+" , "+kindOfPixel[i]);
            }

        }catch(IOException e){
            System.out.println(e.getMessage());
        }


   }    
}

I compare this information with the histogram of lena.bmp , but it seems have some different...

Community
  • 1
  • 1
  • You'll get better results if you post the code you have so far. – Evan Knowles Nov 13 '14 at 07:59
  • What is the "pixel range"? the value/color of a pixel? May it be that the color assigned to '120' just never occurs on the image? – cello Nov 13 '14 at 09:39
  • Not sure but i think Java uses signed data types only(while images are typically stored as unsigned data types) so when you get your `byte[]` it contains negative values, try shifting all of them by 128 and check the result. (posting your code will help with the solution) – Dima Maligin Nov 13 '14 at 09:45

1 Answers1

1

First on java byte

You are aware that the signed java byte ranges from -128 to 127. You erred using 128 instead of calculating modulo 256:

That simply comes down to:

            kindOfPixel[imageInByte[i] & 0xFF]++;

To look for:

  • an unsigned byte 120: < 127 hence 120,
  • an unsigned byte 130: >= 127 hence 130 - 256 = -126.

Modulo 256 calculation in the other direction goes:

byte signed = -126;
int unsigned = signed < 0 ? signed + 256 : signed;
int unsigned = signed & 0xFF; // or simply this

The pixels

A BMP file is not (only) a linearized list of pixels. So one better use the int[] BufferedInage.getRGB:

int[] pixels = originalImage.getRGB(0, 0, width, height, null, 0, width);

Remains the color representation

BMP knows many variants: RGB, indexed etcetera. Indexed colors need a lookup in the palette.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138