我正在创建一个图像滤镜程序,我希望借助数组矩阵将彩色图片转换为灰度图片。
这就是我目前的情况:
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);
答案 0 :(得分:2)
以这种方式构建grayLevels
数组:
for (int i = 0; i < 256; i++) {
grayLevels[i] = new Color(i, i, i);
}
然后,当您需要某种颜色时,只需将其检索为grayLevels[index]
。