简体   繁体   中英

Android Histogram equalization algorithm gives me really bright or red image

I am doing histogram equalization on an image. I first get the RGB image and convert it to YUV. I run the histogram equalization algorithm on Y' of YUV and then convert back to RGB. Is it me, or does the image look weird? I am doing this correctly? this image is pretty bright, other images are a little red.

Here are the before/after images:

之前后

The algorithm (the commented values are values that I used previously for conversion. Both yield pretty much the same results) :

    public static void createContrast(Bitmap src) {

    int width = src.getWidth();
    int height = src.getHeight();

    Bitmap processedImage = Bitmap.createBitmap(width, height, src.getConfig());

    int A = 0,R,G,B;
    int pixel;
    float[][] Y = new float[width][height];
    float[][] U = new float[width][height];
    float[][] V = new float [width][height];
    int [] histogram = new int[256];
    Arrays.fill(histogram, 0);

    int [] cdf = new int[256];
    Arrays.fill(cdf, 0);
    float min = 257;
    float max = 0;

    for(int x = 0; x < width; ++x) {
        for(int y = 0; y < height; ++y) {
            pixel = src.getPixel(x, y);
            //Log.i("TEST","("+x+","+y+")");
            A = Color.alpha(pixel);
            R = Color.red(pixel);
            G = Color.green(pixel);
            B = Color.blue(pixel);

            /*Log.i("TESTEST","R: "+R);
            Log.i("TESTEST","G: "+G);
            Log.i("TESTEST","B: "+B);*/

            // convert to YUV
            /*Y[x][y] = 0.299f * R + 0.587f * G + 0.114f * B;
            U[x][y] = 0.492f * (B-Y[x][y]);
            V[x][y] = 0.877f * (R-Y[x][y]);*/

            Y[x][y] = 0.299f * R + 0.587f * G + 0.114f * B;
            U[x][y] = 0.565f * (B-Y[x][y]);
            V[x][y] = 0.713f * (R-Y[x][y]);
            // create a histogram
            histogram[(int) Y[x][y]]+=1;
            // get min and max values
            if (Y[x][y] < min){
                min = Y[x][y];
            }
            if (Y[x][y] > max){
                max = Y[x][y];
            }
        }
    }

    cdf[0] = histogram[0];
    for (int i=1;i<=255;i++){
        cdf[i] = cdf[i-1] + histogram[i];
        //Log.i("TESTEST","cdf of: "+i+" = "+cdf[i]);
    }

    float minCDF = cdf[(int)min];
    float denominator = width*height - minCDF;
    //Log.i("TEST","Histeq Histeq Histeq Histeq Histeq Histeq");
    for(int x = 0; x < width; ++x) {
        for(int y = 0; y < height; ++y) {
            //Log.i("TEST","("+x+","+y+")");
            pixel = src.getPixel(x, y);
            A = Color.alpha(pixel);
            Y[x][y] = ((cdf[ (int) Y[x][y]] - minCDF)/(denominator)) * 255;
            /*R = minMaxCalc(Y[x][y] + 1.140f * V[x][y]);
            G = minMaxCalc (Y[x][y] - 0.395f * U[x][y] - 0.581f * V[x][y]);
            B = minMaxCalc (Y[x][y] + 2.032f * U[x][y]);*/

            R = minMaxCalc(Y[x][y] + 1.140f * V[x][y]);
            G = minMaxCalc (Y[x][y] - 0.344f * U[x][y] - 0.714f * V[x][y]);
            B = minMaxCalc (Y[x][y] + 1.77f * U[x][y]);
            //Log.i("TESTEST","A: "+A);
            /*Log.i("TESTEST","R: "+R);
            Log.i("TESTEST","G: "+G);
            Log.i("TESTEST","B: "+B);*/
            processedImage.setPixel(x, y, Color.argb(A, R, G, B));
        }
    }

}

My next step is to graph the histograms before and after. I just want to get an opinion here.

The question is a little bit old, but let me answer.

The reason is the way histogram equalization works. The algorithm tries to use all of the 0-255 range instead of given image's range.

So if you give it a dark image, it will change relatively brighter pixels to white colors. And relatively darker colors to black colors.

If you give it a bright image, for the same reason it will get darkened.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM