Hi everyone, I'm trying to write an image editor in Java, one of the functions is histogram equalisation, but my code is producing a greyscale image instead :(

Here is my code, any help would be greatly appreciated, ta.

private void histEqMenuItemActionPerformed(java.awt.event.ActionEvent evt) {                                               
        BufferedImage inputImage = getImage();
        BufferedImage histEqImage = new BufferedImage(inputImage.getWidth(), inputImage.getHeight(), inputImage.getType());
        int maxIntensity = 255;

        int[] red = new int[256];

        for (int h = 0; h < 255; h++) {
            red[h] = 0;
        }

        for (int x = 0; x < inputImage.getWidth(); x++) {
            for(int y = 0; y < inputImage.getHeight(); y++) {
                int i = inputImage.getRGB(x,y) & 0x00ff0000 >> 16;
                red[i]++;
            }
        }

        int sumr = 0;

        for (int x = 0; x < red.length; x++) {
            sumr += red[x];
            red[x] = sumr * maxIntensity /(inputImage.getWidth() * inputImage.getHeight());
        }

        for (int x = 0; x < inputImage.getWidth(); x++) {
            for(int y = 0; y < inputImage.getHeight(); y++) {
                int i = inputImage.getRGB(x,y) & 0x00ff0000 >> 16;
				i = red[i];
				histEqImage.setRGB(x, y, (histEqImage.getRGB(x, y) & 0xff00ffff) | (i << 16));
            }
        }

        int[] green = new int[256];

        for (int h = 0; h < 255; h++) {
            green[h] = 0;
        }

        for (int x = 0; x < inputImage.getWidth(); x++) {
            for(int y = 0; y < inputImage.getHeight(); y++) {
                int i = inputImage.getRGB(x,y) & 0x0000ff00 >> 8;
                green[i]++;
            }
        }

        int sumg = 0;

        for (int x = 0; x < green.length; x++) {
            sumg += green[x];
            green[x] = sumg * maxIntensity /(inputImage.getWidth() * inputImage.getHeight());
        }

        for (int x = 0; x < inputImage.getWidth(); x++) {
            for (int y = 0; y < inputImage.getHeight(); y++) {
                int i = inputImage.getRGB(x,y) & 0x0000ff00 >> 8;
				i = green[i];
				histEqImage.setRGB(x, y, (histEqImage.getRGB(x, y) & 0xffff00ff) | (i << 8));
            }
        }

        int[] blue = new int[256];

        for (int h = 0; h < 255; h++) {
            blue[h] = 0;
        }

        for (int x = 0; x < inputImage.getWidth(); x++) {
            for (int y = 0; y < inputImage.getHeight(); y++) {
                int i = inputImage.getRGB(x,y) & 0x000000ff;
                blue[i]++;
            }
        }

        int sumb = 0;

        for (int x = 0; x < blue.length; x++) {
            sumb += blue[x];
            blue[x] = sumb * maxIntensity /(inputImage.getWidth() * inputImage.getHeight());
        }

        for (int x = 0; x < inputImage.getWidth(); x++) {
            for (int y = 0; y < inputImage.getHeight(); y++) {
                int i = inputImage.getRGB(x,y) & 0x000000ff;
				i = blue[i];
				histEqImage.setRGB(x, y, (histEqImage.getRGB(x, y) & 0xffffff00) | i);
            }
        }

       System.out.println("Histogram Equalisation succeeded.");
       setImage(histEqImage);
    }

Turns out I just had my brackets in the wrong place when I was getting the red, green and blue values, oh dear! Works now though :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.