I have tried to grayscale a already black-white-gray picture and it become black.
When I try to grayscale a picture with Java, I do like this:
    // This turns the image data to grayscale and return the data
    private static RealMatrix imageData(File picture) {
        try {
            BufferedImage image = ImageIO.read(picture);
            int width = image.getWidth();
            int height = image.getHeight();
            RealMatrix data = MatrixUtils.createRealMatrix(height * width, 1);
            // Convert to grayscale
            int countRows = 0;
            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++) {
                    // Turn image to grayscale
                    int p = image.getRGB(x, y);
                    int r = (p >> 16) & 0xff;
                    int g = (p >> 8) & 0xff;
                    int b = p & 0xff;
                    // calculate average and save
                    int avg = (r + g + b) / 3;
                    data.setEntry(countRows, 0, avg);
                    countRows++;
                }
            }
            return data;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
The problem what I see is that p is an 32-bit value and I only want 8-bit value. Even if the picture is already grayscaled, the p value is already a 32-bit value. That cause trouble for me.
So if I grayscale a gray picture, it will become black. Or at least darker. 
And I want 0..255 values of p, which is a 32-bit integer value.
Do you have any suggestions how to read pictures as they where 8-bit? It's for image classification.
Summarize:
I need help to get each pixels from a picture in 0..255 format. One way is to gray scale it, but how can I verify if the picture is already gray scaled?
Update:
I have tried to read a picture as it was 8-bit values. It works. Then I try to save the picture with the same values. The picture becomes very dark.
I have a matlab example I want to show. First I read my picture:
image = imread("subject01.normal");
And then I save the picture.
imwrite(uint8(image), "theSameImage.gif")
If I try with a minimal Java code snipped for reading an image.
private static void imageData(File picture) {
        try {
            BufferedImage image = ImageIO.read(picture);
            int width = image.getWidth();
            int height = image.getHeight();
            DataBuffer buffer = image.getRaster().getDataBuffer();
            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++) {
                    int p = buffer.getElem(x + y * width);
                    image.setRGB(x, y, p);
                }
            }
            File output = new File(picture.getName());
            ImageIO.write(image, "gif", output);
            return data;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
I will get this picture:
So even if there is a marked answer in this question, it's still not going to help you.


