Histogram in Java

Asked

Viewed 340 times

0

Hello,

I need to transform an image into a pixel array.

For example: Given the image "Netuno.jpeg", of 256 x 256 pixels, I need to allocate the value of pixels in that Matrix and print the amount that these pixels appear in another vector:

Matrix[0][0] = 2 (where 2 is the pixel value),

Matrix[0][1] = 12, ... , Matrix[255][255] = 23

Qtd of pixels 0 = 34 (34 being the number of times the pixel of value 0 appeared) ,

Qtd of pixels 1 = 21, ... , Qtd of pixels 255 = 120

So far I have read the image using Bufferedimage, but do not know how to access the image pixels.

import xxx;

public class Histograma {

public static void main(String[] args) throws IOException {

    //Atributos

    //Métodos

    //Leitura da Imagem - Planeta Netuno

    BufferedImage img = ImageIO.read(new File("/home/patch/netuno.jpeg"));
    int altura = img.getHeight();
    int largura = img.getWidth();
    int[][] matrix = new int[largura][altura];

    for(int i = 0; i < img.getWidth(); i++) {
        for(int j = 0; j < img.getHeight(); j++) {

        }
    }           
}   

Thank you!

1 answer

0

Reading the class documentation Bufferedimage, it seems to me that you can read the RGB value of each pixel at position (x, y), this value which is returned as a int. The name of the method is getRGB(int x, int y). Once the RGB value is obtained, you can break it into the red, green and blue components by applying a little bit of bit brushing, if necessary. Or just use it as an index for your histogram.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.