簡體   English   中英

灰度圖像濾鏡

[英]Grayscale image filter

我正在使用矩陣對圖像進行灰度縮放的圖像過濾器,如下圖所示,在跟蹤循環中將R + G + B顏色除以3。

for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            Color c = inPixels[i][j];
            outPixels[i][j] = grayLevels[(c.getRed() + c.getGreen() + c.getBlue()) / 3];
        }
    }

但是我聽說強度來做會更好,所以我嘗試了類似的方法,但是似乎不起作用。 當我試圖像這樣過濾它時,我的GUI應用程序凍結。 也許有人可以幫助我解決這個問題?

 for (int i = 0; i < height; i++) { 
            for (int j = 0; j < width; j++) { 
                short[][] intensity = computeIntensity(inPixels); 
                Color c = inPixels[i][j]; 

                outPixels[i][j] = grayLevels[(c.getRed() + c.getGreen() + c.getBlue()) / 3];
        }

如果需要的話,我可以發布正在使用的其他類,但是我認為沒有必要,因為代碼是不言自明的。

編輯:這是強度方法:

protected short[][] computeIntensity(Color[][] pixels) {
    int height = pixels.length;
    int width = pixels[0].length;
    short[][] intensity = new short[height][width];
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            Color c = pixels[i][j];
            intensity[i][j] = (short) ((c.getRed() + c.getGreen() + c
                    .getBlue()) / 3);
        }
    }
    return intensity;
}

謝謝,邁克爾。

如上面的評論所述,您可以使用更好的公式來計算灰度: red * 0.299 + green * 0.587 + blue * 0.114

protected Color[][] computeIntensity(Color[][] pixels) {
    int height = pixels.length;
    int width = pixels[0].length;
    Color[][] intensity = new Color[height][width];
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            Color c = pixels[i][j];
            intensity[i][j] = new Color(c.getRed() * 0.299, c.getGreen() * 0.587, c.getBlue() * 0.114);
        }
    }
    return intensity;
}

outPixels = computeIntensity(inPixels); 

computeIntensity已經在計算灰度,因此無需在所有像素上都重復迭代。 您甚至可以將其重命名為computeGrayScales

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM