Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 交换两个像素的颜色_Java_Sorting_Pixel_Bufferedimage - Fatal编程技术网

Java 交换两个像素的颜色

Java 交换两个像素的颜色,java,sorting,pixel,bufferedimage,Java,Sorting,Pixel,Bufferedimage,我必须对缓冲图像进行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.get

我必须对缓冲图像进行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));
                }
            }
    }
    }
BufferedImage结果=新的BufferedImage(img.getWidth()、img.getHeight()、BufferedImage.TYPE\u INT\u RGB);

对于(int m=0;m而言,在比较后未正确切换值:

for(int x =1; x<img.getWidth();x++){
    int temp = img.getRGB(x-1,y);
    int temp2 = img.getRGB(x, y);
...
    if (gr2 < gr) {
        result.setRGB(x, y, rgbColour(temp2,temp2,temp2)); // same as in img
        result.setRGB(x-1, y, rgbColour(temp,temp,temp)); // same as in img
    }
}

for(int x=1;x)您的问题是什么?我所做的似乎没有给我想要的输出。
for(int m=0;mI需要“n”轮像素排序。
for(int x =1; x<img.getWidth();x++){
    int temp = img.getRGB(x-1,y);
    int temp2 = img.getRGB(x, y);
...
    if (gr2 < gr) {
        result.setRGB(x, y, rgbColour(temp2,temp2,temp2)); // same as in img
        result.setRGB(x-1, y, rgbColour(temp,temp,temp)); // same as in img
    }
}
    ...
    if (gr2 < gr) { // swap values in result
        result.setRGB(x-1, y, rgbColour(temp2,temp2,temp2));
        result.setRGB(x, y, rgbColour(temp,temp,temp));
    } else { // keep values in result same as in img
        result.setRGB(x, y, rgbColour(temp2,temp2,temp2));
        result.setRGB(x-1, y, rgbColour(temp,temp,temp));
    }