0
我必須對緩衝圖像進行n輪行像素排序,循環遍歷每行並將當前像素的亮度與當前像素的左側的亮度進行比較。如果電流的亮度小於左側的亮度,則需要交換像素的顏色。這是我現在的代碼。交換兩個像素的顏色
BufferedImage result = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
for (int m = 0; m<n;m++){
for (int y=0;y<img.getHeight();y++){
for(int x =1; x<img.getWidth();x++){
int temp = img.getRGB(x-1,y);
int temp2 = img.getRGB(x, y);
int gr = (int)brightness(temp);
int gr2 = (int)brightness(temp2);
if(gr2<gr){
result.setRGB(x, y, rgbColour(temp2,temp2,temp2));
result.setRGB(x-1, y, rgbColour(temp,temp,temp));
}
}
}
}
我給出的方法如下。
public static int getRed(int rgb) { return (rgb >> 16) & 0xff; }
public static int getGreen(int rgb) { return (rgb >> 8) & 0xff; }
public static int getBlue(int rgb) { return rgb & 0xff; }
public static int rgbColour(int r, int g, int b) {
return (r << 16) | (g << 8) | b;
}
public static double brightness(int rgb) {
int r = getRed(rgb);
int g = getGreen(rgb);
int b = getBlue(rgb);
return 0.21*r + 0.72*g + 0.07*b;
}
public static BufferedImage convertToGrayscale(BufferedImage img) {
BufferedImage result = new BufferedImage(
img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB
);
for(int x = 0; x < img.getWidth(); x++) {
for(int y = 0; y < img.getHeight(); y++) {
int col = img.getRGB(x, y);
int gr = (int)brightness(col);
result.setRGB(x, y, rgbColour(gr, gr, gr));
}
}
return result;
}
什麼是你的問題? –
看來我正在做的是不給我所需的輸出。 – JClutch
'for(int m = 0; m