3

我正在对图像进行直方图均衡化。我首先获取 RGB 图像并将其转换为 YUV。我在 YUV 的 Y' 上运行直方图均衡算法,然后转换回 RGB。是我,还是图像看起来很奇怪?我这样做正确吗?这张图片很亮,其他图片有点红。

这是之前/之后的图像:

前 后

算法(注释值是我之前用于转换的值。两者产生几乎相同的结果):

    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));
        }
    }

}

我的下一步是绘制前后的直方图。我只是想在这里得到一个意见。

4

1 回答 1

0

这个问题有点老了,但让我回答一下。

原因是直方图均衡的工作方式。该算法尝试使用所有 0-255 范围而不是给定图像的范围。

因此,如果你给它一个暗图像,它会将相对较亮的像素变为白色。而相对较深的颜色为黑色。

如果你给它一个明亮的图像,出于同样的原因它会变暗。

于 2016-06-21T05:25:00.307 回答