【问题标题】:Histogram Equalization of image using lookup table使用查找表对图像进行直方图均衡
【发布时间】:2017-10-26 04:05:59
【问题描述】:

我正在尝试用 Java 对灰度图像进行直方图均衡化。描述如下: 使用每个像素的RGB的一个波段作为查找表的索引,对图像进行迭代,以确定图像的新像素值。将每个像素的RGB设置为新像素值对应的RGB。

实现这一点后,我得到了一张蓝色的图像:

[已删除]

(预期结果)

[已删除]

这是我目前的代码:

private void histogramEqualize(BufferedImage im, int[] lut) {
     for (int x = 0; x < im.getWidth(); x++) {
            for (int y = 0; y < im.getHeight(); y++) {
                Color c = new Color(im.getRGB(x, y));
                Color eq = new Color(lut[c.getRed()], c.getGreen(), c.getBlue());
                im1.setRGB(x, y, eq.getRGB());
            }
     }
 }

public int[] getLookupTable(int[] h, int n) {
     // h: Histogram for im1 in either the red band or luminance.
     lut = new int[256];
     double sf = 255/n;
     int sumH = 0;
     int sk = 0;
     for(int i=0; i<h.length; i++) {
         sumH += h[i];
         sk = (int)(sf*sumH);
         lut[i] = sk;
     }
     return lut;
 }

我也尝试将Color eq = new Color(lut[c.getRed()], c.getGreen(), c.getBlue()); 更改为Color eq = new Color(lut[c.getRed()], lut[c.getGreen()], lut[c.getBlue()]);,但这导致图像为黑色。

【问题讨论】:

    标签: java image-processing histogram


    【解决方案1】:

    您提到要在灰度图像上应用直方图均衡化,但您使用的是像素的 RGB 颜色值。

    对于灰度图像,您可以仅对图像中的灰度级进行归一化以进行直方图均衡,如下所示:

    1) 遍历每个灰度像素值,并通过统计它们在图像中的出现次数,生成每个灰度级别的直方图数据。

    2) 求上述直方图的累积分布。

    3) 遍历原始图像中的每个灰度像素值,并使用下面的公式将它们的值替换为对应的归一化值。

     where L=255, that is total gray scale levels,
     M = Image height,
     N = Image width,
     MxN to get total number of pixels in image.
     cdfmin = min value of cumulative distribution data in step 2.
    

    这将为您提供新的标准化图像矩阵。

    如果您想对 RGB 图像应用直方图均衡化,则需要将 RGB 颜色空间转换为 HSV 颜色空间,并在值通道上应用与灰度图像相同的步骤,而不更改其色调和饱和度值。

    【讨论】:

    • 非常感谢;我明白了。
    猜你喜欢
    • 2015-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-20
    • 2020-02-11
    • 1970-01-01
    • 1970-01-01
    • 2020-01-27
    相关资源
    最近更新 更多