Access to image pixels

Can someone show me here how to access all the pixels of an image in order to be able to plot a histogram of this image?

+3
source share
3 answers

you can use

java.awt.image.BufferedImage image = ImageIO.read( "image.gif" );
int[] samples = image.getData().getPixel( x, y, null );

// or the pixel converted to the default RGB color model:
int rgb = image.getRGB( x, y )

I think samplesyou get the RGB values ​​from here, but you should check that ...

+4
source

If you can use JAI , it has options for generating Histograms .

+1
source

, - , (, , !).

These histograms don't align or nothing here, just literally counting a pixel of a specific value in each strip.

I believe the groups (you will need to check): R, G, B + Alpha in that order.

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

public class SimpleImageReader {


public static void main(String[] args) throws Exception {
       // File f=new File(args[0]);
        File f=new File("C:\\1.jpg");
        BufferedImage img = ImageIO.read(f);
        Raster r=img.getData();
        int levels=256;
        int bands=r.getNumBands();
        int histogram[][]=new int[bands][levels]; 

        for (int x=r.getMinX();x<r.getWidth();x++) {
            for (int y=r.getMinY();y<r.getHeight();y++) {
                for (int b=0;b<3;b++) {
                    int p=r.getSample(x, y, b);
                    histogram[b][p]++;
                }
            }
        }
      for (int b=0;b<histogram.length;b++) {
        System.out.println("Band:"+b);
        for (int i=0;i<histogram[b].length;i++) {
          System.out.println("\t"+i+"="+histogram[b][i]);
        }
       }
}
}
+1
source

Source: https://habr.com/ru/post/1731694/


All Articles