繁体   English   中英

使用数组矩阵RGB java将图像转换为灰度

[英]Convert Image to Grayscale with array matrix RGB java

我正在创建一个图像过滤器程序,我想在数组矩阵的帮助下将彩色图片转换为灰度图片。

这是我目前所拥有的:

import java.awt.Color;
import se.lth.cs.ptdc.images.ImageFilter;

public class GrayScaleFilter extends ImageFilter {

    public GrayScaleFilter(String name){
        super(name);
    }

    public Color[][] apply(Color[][] inPixels, double paramValue){
        int height = inPixels.length;
        int width = inPixels[0].length;
        Color[][] outPixels = new Color[height][width];
        for (int i = 0; i < 256; i++) {
          grayLevels[i] = new Color(i, i, i);
        }

        for(int i = 0; i < height; i++){
            for(int j = 0; j < width; j++){
                Color pixel = inPixels[i][j];
            outPixels[i][j] = grayLevels[index];                    
            }
        }
        return outPixels;
    }
}

看起来我应该使用这个公式:((R+G+B)/3)

我想创建一个这样的数组矩阵:

 Color[] grayLevels = new Color[256];
    // creates the color (0,0,0) and puts it in grayLevels[0],
    // (1,1,1) in grayLevels[1], ..., (255,255,255) in grayLevels[255]

当我想使用 grascale 时,这也是我所指的类:

public abstract Color[][] apply(Color[][] inPixels, double paramValue);

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

关于我如何实现这一目标的任何反馈? 而不是使用outPixels[i][j] = new Color(intensity, intensity, intensity);

以这种方式构建grayLevels数组:

for (int i = 0; i < 256; i++) {
  grayLevels[i] = new Color(i, i, i);
}

然后,当您需要某种颜色时,只需将其检索为grayLevels[index]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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