Hey, this is a continuation of my image editing program, I am trying to perform intensity thresholding with an intelligent threshold. My code thus far:
private void thresholdMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
BufferedImage inputImage = getImage();
setUndoImage(inputImage);
BufferedImage thresholdImage = new BufferedImage(inputImage.getWidth(), inputImage.getHeight(), inputImage.getType());
int threshold = 0;
int tnew = 128;
int count = 0;
double m1, m2;
for (int x = 0; x < inputImage.getWidth(); x++) {
for (int y = 0; y < inputImage.getHeight(); y++) {
int red = (inputImage.getRGB(x,y) & 0x00ff0000) >> 16;
int green = (inputImage.getRGB(x,y) & 0x0000ff00) >> 8;
int blue = (inputImage.getRGB(x,y) & 0x000000ff);
int grey = (int) ((0.30 * red) + (0.59 * green) + (0.11 * blue));
thresholdImage.setRGB(x,y, 0xff000000 | grey << 16 | grey << 8 | grey);
}
}
int[] hist = new int[256];
for (int h = 0; h < 255; h++) {
hist[h] = 0;
}
for (int x = 0; x < thresholdImage.getWidth(); x++) {
for(int y = 0; y < thresholdImage.getHeight(); y++) {
int i = (thresholdImage.getRGB(x, y) & 0x000000ff);
hist[i]++;
}
}
do {
threshold = tnew;
m1 = m2 = 0.0;
for (int i = 0; i < threshold; i++){
m1 += hist[i];
count++;
}
m1 /= count;
for (int i = threshold; i < 255; i++){
m2 += hist[i];
count++;
}
m2 /= count;
tnew = (int)((m1 + m2) / 2.0);
} while (tnew != threshold);
for (int x = 0; x < thresholdImage.getWidth(); x++) {
for (int y = 0; y < thresholdImage.getHeight(); y++) {
int bin = (thresholdImage.getRGB(x, y) & 0x000000ff);
if (bin > tnew) {
bin = 255;
} else {
bin = 0;
}
thresholdImage.setRGB(x,y, 0xff000000 | bin << 16 | bin << 8 | bin);
}
}
imageCanvas.setIcon(new ImageIcon(thresholdImage));
System.out.println("Intensity thresholding succeeded.");
setImage(thresholdImage);
}
First I convert the image to a greyscale image. Then I get the blue value (because it's greyscale red green and blue values should be equal) and make a histogram of the grey levels. Then I set an initial threshold of 128, and find the mean of all the values below and above this threshold, and then use them to find an overall mean, set my new threshold to that and try again, until it stabilises. Then I check if the grey value is over or under the threshold value, and set it white or black accordingly. My issue is that my threshold is being set unreasonably high, so that my image is either nearly all white, or it simply crashes with an "ArrayIndexOutOfBoundsException".