2
我正在創建一個圖像過濾器程序,並且希望藉助數組矩陣將彩色圖片轉換爲灰度圖片。使用數組矩陣將圖像轉換爲灰度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);
我添加了循環並寫道: outPixels [i] [j] = new Color(grayLevels [index]); 但它似乎沒有工作 – Rob
它應該是outPixels = grayLevels [index]。我猜想目標是你不會創建那麼多的Color對象。 –
您是否還需要實際構建BufferedImage並將其保存到磁盤的代碼? –