我正在创建一个图像过滤程序,我想使用数组矩阵将彩色图片转换为灰度图片。
以下是我目前的代码:
看起来我应该使用这个公式:((R+G+B)/3)
我想创建一个像这样的数组矩阵:
这是我想要使用灰度时所指的类:
如何实现这一点的任何反馈?而不是使用
以下是我目前的代码:
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]
这是我想要使用灰度时所指的类:
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);