I have to write a method that:
How can I do it? I wrote a bit of code but I'm deadlocked.
public histogram(BufferedImage image){
WritableRaster writableRaster = image.getRaster();
int width = image.getWidth();
int height = image.getHeight();
int pixelValue;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
pixelValue = writableRaster.setDataElements(x, y, width, height, );
}
}
}
The following code snippet should help:
Raster raster = image.getRaster();
int numBands = raster.getSampleModel().getNumBands();
int width = image.getWidth();
int height = image.getHeight();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int[] pixelValue = new int[numBands];
raster.getPixel(x, y, pixelValue);
}
}
If you really have a grayscale image without alpha, the SampleModel will contain only one band. In this case the pixelValue array will contain your desired int value. You only have to add an histogram array of 256 int values and increase the value at the index of your pixel value.