Ask Your Question
0

i want a code for histogram equalization

asked 2013-02-15 09:52:39 -0600

dinesh gravatar image

i am doing project using opencv on android platform using eclipse.Can anyone provide me a code for histogram equalisation using java language.

edit retag flag offensive close merge delete

Comments

Basically your complete post pushes me towards closure, if people wouldn't bother placing any remarks. This is a Question & Answer forum which means that you try something, you get stuck and you ask people for help and solutions. This is not a Please provide me the code forum. Next time posts like these will be deleted. In respect to people answering, I will leave it open...

StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-08 08:56:57 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
1

answered 2013-02-15 11:59:04 -0600

berak gravatar image

well, in c++, there is cv::equalizeHist()

http://opencv.willowgarage.com/documentation/cpp/histograms.html#cv-equalizehist

found this, too: https://groups.google.com/forum/?fromgroups#!topic/android-opencv/f5PGo8MuySo

edit flag offensive delete link more
0

answered 2013-04-08 08:02:12 -0600

here is code of Histogram Equalization in java...

package he;

import java.awt.Color; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.ArrayList; import javax.imageio.ImageIO;

public class HistogramEqualization {

private final int greyRange = 256;

public HistogramEqualization(String fileName) throws IOException {

    File file;
    BufferedImage image;
    float histogram[][];
    histogram = new float[3][];

    histogram[0] = new float[greyRange];
    histogram[1] = new float[greyRange];
    histogram[2] = new float[greyRange];

    file = new File(fileName);
    image = ImageIO.read(file);

    //================== Histogram Generation ==============================
    histogram[0] = getHistogramByColor(image, 1);
    histogram[1] = getHistogramByColor(image, 2);
    histogram[2] = getHistogramByColor(image, 3);
    //======================================================================

    //===================== Normalizing Whole Image ========================
    normalizedFunction(histogram[0], 0, histogram[0].length - 1);
    normalizedFunction(histogram[1], 0, histogram[0].length - 1);
    normalizedFunction(histogram[2], 0, histogram[0].length - 1);
    //======================================================================

    //===================== Histogram EQUALIZATION =========================
    histogramEqualization(histogram[0], 0, (int) 255);
    histogramEqualization(histogram[1], 0, (int) 255);
    histogramEqualization(histogram[2], 0, (int) 255);
    //======================================================================

    ArrayList<int[]> imageLUT = new ArrayList<int[]>();
    int[] rhistogram = new int[256];
    int[] ghistogram = new int[256];
    int[] bhistogram = new int[256];

    for (int i = 0; i < rhistogram.length; i++) {
        rhistogram[i] = (int) histogram[0][i];
        ghistogram[i] = (int) histogram[1][i];
        bhistogram[i] = (int) histogram[2][i];
    }

    imageLUT.add(rhistogram);
    imageLUT.add(ghistogram);
    imageLUT.add(bhistogram);

    BufferedImage originalImage = getOriginalImage(ImageIO.read(file), imageLUT);
    String fileN = fileName(file);
    writeImage(file.getParent(), fileN, originalImage);
}

public void histogramEqualization(float histogram[], int low, int high) {

    float sumr, sumrx;
    sumr = sumrx = 0;
    for (int i = low; i <= high; i++) {
        sumr += (histogram[i]);
        sumrx = low + (high - low) * sumr;
        int valr = (int) (sumrx);
        if (valr > 255) {
            histogram[i] = 255;
        } else {
            histogram[i] = valr;
        }
    }
}

public BufferedImage getOriginalImage(BufferedImage original, ArrayList<int[]> histLUT) {

    int red;
    int green;
    int blue;
    int alpha;
    int newPixel = 0;

    // Get the Lookup table for histogram equalization        

    BufferedImage histogramEQ = new BufferedImage(original.getWidth(), original.getHeight(), original.getType());

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

            // Get pixels by R, G, B
            alpha = new Color(original.getRGB(i, j)).getAlpha();
            red = new Color(original.getRGB(i, j)).getRed();
            green = new Color(original.getRGB(i, j)).getGreen();
            blue = new Color(original.getRGB(i, j)).getBlue();

            // Set new pixel values using the histogram lookup table
            red = histLUT.get(0)[red];
            green = histLUT.get(1)[green];
            blue = histLUT.get(2)[blue];

            // Return back to original format
            newPixel = colorToRGB(alpha, red, green, blue);

            // Write pixels into image
            histogramEQ.setRGB(i, j, newPixel);
        }
    }
    return histogramEQ;
}

// Convert R, G, B, Alpha to standard 8 bit
public int colorToRGB(int alpha, int red, int green, int blue) {

    int newPixel = 0;
    newPixel += alpha;
    newPixel = newPixel << 8;
    newPixel += red;
    newPixel = newPixel << 8;
    newPixel += green;
    newPixel = newPixel << 8;
    newPixel += blue;
    return newPixel;

}

public void writeImage(String output, String fileName, BufferedImage img) throws IOException {
    File file = new File(output + "\\" + fileName + "HE.bmp");
    ImageIO.write(img, "bmp", file);
}

public String fileName(File file) throws IOException {
    String fileName = file.getName().substring(0, file.getName().length() - 4);
    return fileName;
}

public void normalizedFunction(float myArr[], int low, int high) {

    float sumV = 0.0f;
    for (int i = low ...
(more)
edit flag offensive delete link more

Comments

Dear Mayank and friends,

I am very interesting about your source code . In this source code, I don't understand 2 lines in histogramEqualization method as follow:

    *sumr += (histogram[i]);
    sumrx = low + (high - low) * sumr;*

Can you explain for me about it.

Thanks,

vietcusc gravatar imagevietcusc ( 2016-07-20 01:12:37 -0600 )edit

How to do using opencv?

arqam gravatar imagearqam ( 2017-06-29 19:42:38 -0600 )edit

Question Tools

Stats

Asked: 2013-02-15 09:52:39 -0600

Seen: 7,205 times

Last updated: Apr 08 '13