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

标签 java arrays matrix rgb

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

这是我目前拥有的:

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]

关于java - 使用数组矩阵RGB java将图像转换为灰度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13497211/

相关文章:

java - 设置来自 Java 的 PostgreSQL JDBC 连接的连接选项

c - 如何找到分隔数组中元素的常量值

c++ - 解决PnP : Obtaining the rotation translation matrix

arrays - 数组到矩阵

java - 与 Libgdx 的圆形和多边形碰撞

java - 多线程,统计正在运行的线程数

java - BigDecimal 使用带前导零的整数文字初始化

java - 如何从ArrayList的ArrayList中获取二维数组?

arrays - 相当于 PHP 的 list() 的 go 是什么?

python - 加入两个numpy矩阵