处理-粗像素排序算法在部分图像后停止

Dte*_*kms 5 java processing pixel

我写了一个处理代码,以前用选择排序对像素进行了排序。我必须上交它,老师说这样要花很长时间,所以我决定将像素亮度分成50的一部分,然后非常粗略地排序。出来的图像还没有完全排序,我真的不知道哪里出了问题。我不必进行完美分类-实际上,这只是要获得一个看起来很酷的图像。希望有人能帮助我,我的意思是可以理解的!提前致谢

PImage img; 
PImage two;
PImage sorted;
int j = 0;
int x = j;
int y = x;
int u = y;
int h = u;
int d = 1;

void setup() {

    size(736,1051); 
    img = loadImage("guy.png");
    two = loadImage("guy2.png");

    background(two); 
}

void draw() {
   loadPixels();

    for (int y = 0; y < height; y++) { 
        for (int x = 0; x < width; x++) {
            int loc = x + y*width;

            float r = red(img.pixels[loc]);
            float g = green(img.pixels[loc]);
            float b = blue(img.pixels[loc]);
            float av = ((r+g+b)/3.0);

            pixels[loc] =  color(g,b,r, 17); //I know r, g, b are switched here
        }    
    }  

    updatePixels();
    save("guy_coloured.png"); 
}

void keyPressed(){
    sorted = loadImage("guy_coloured.png");
    sorted.loadPixels();
    image(sorted, 0, 0);

    System.out.print("doing it");

    for (int i = 0; i < sorted.pixels.length; i++){

        color colours = sorted.pixels[i]; 
        float b = brightness(colours); 
        if(b<50){                      
            sorted.pixels[j] = sorted.pixels[i];
            j++;}
    }
    for (int f = 0; f < img.pixels.length; f++){ 

        color colours = sorted.pixels[f];
        float b = brightness(colours);
        if(b<100 && b>50){
            sorted.pixels[x] = sorted.pixels[f];
            x++;} 
    }
    for (int k = 0; k < img.pixels.length; k++){ 

        color colours = sorted.pixels[k];
        float b = brightness(colours);
        if(b<150 && b>100){
            sorted.pixels[y] = sorted.pixels[k];
            y++;}
    }
    for (int t = 0; t < img.pixels.length; t++){ 

        color colours = sorted.pixels[t];
        float b = brightness(colours);
        if(b<200 && b>150){
            sorted.pixels[u] = sorted.pixels[t];
            u++;}
    }
    for (int o = 0; o < img.pixels.length; o++){ 

        color colours = sorted.pixels[o];
        float b = brightness(colours);
        if(b>200){
            sorted.pixels[h] = sorted.pixels[o];
            h++;}
    }

    System.out.print("done");
    sorted.updatePixels();


    image(sorted, 0, 0);
    save("guy_sorted.png"); 
    noLoop();
}
Run Code Online (Sandbox Code Playgroud)

我希望对整个图像进行排序,但是它使我返回了从顶部开始排序大约1/4的普通图像。这是当前结果:https : //imgur.com/kHffIpm

包括不相关部分的完整代码:https : //docs.google.com/document/d/1YC97YMq9fKcbCAn3_RvLIm1bNo72FrNnHT3obc9pp7U/edit?usp=sharing

Rab*_*d76 3

您不对像素进行排序。您实际上要做的是将暗像素排列在图像的开头并覆盖那里的像素。如果你想对像素进行排序,那么你必须交换它们。

编写一个可以交换 2 个像素的函数:

void Swap(PImage toSort, int i1, int i2) {
    color c = toSort.pixels[i1];
    toSort.pixels[i1] = toSort.pixels[i2];
    toSort.pixels[i2] = c;
}
Run Code Online (Sandbox Code Playgroud)

一旦某些像素被排序并排列在图像的开头,就不需要进一步研究该区域。

编写一个函数,根据亮度范围 [ b_min, b_max] 对像素进行排序,并从某个索引开始start

int Sort(PImage toSort, int start, float b_min, float b_max) {

    for (int i = start; i < toSort.pixels.length; i++) {
        float b = brightness(toSort.pixels[i]);
        if (b >= b_min && b < b_max) {
            Swap(toSort, i, start);
            start ++;
        }
    }
    return start;
}
Run Code Online (Sandbox Code Playgroud)

按亮度升序对图像进行排序。例如:

PImage img, two, sorted;

void setup() {
    size(736,1051); 
    img = loadImage("guy.png");
    two = loadImage("guy2.png");
    background(two);  
}

void draw() {
    loadPixels();
    for (int y = 0; y < height; y++) { 
        for (int x = 0; x < width; x++) {
            int loc = x + y*width;
            float r = red(img.pixels[loc]), g = green(img.pixels[loc]), b = blue(img.pixels[loc]);
            pixels[loc] =  color(g,b,r, 17); //I know r, g, b are switched here
       }  
    }        
    updatePixels();
    save("guy_coloured.png"); 
}

void Swap(PImage toSort, int i1, int i2) {
    color c = toSort.pixels[i1];
    toSort.pixels[i1] = toSort.pixels[i2];
    toSort.pixels[i2] = c;
}

int Sort(PImage toSort, int start, float b_min, float b_max) {

    for (int i = start; i < toSort.pixels.length; i++) {
        float b = brightness(toSort.pixels[i]);
        if (b >= b_min && b < b_max) {
            Swap(toSort, i, start);
            start ++;
        }
    }
    return start;
}

void keyPressed(){
    sorted = loadImage("guy_coloured.png");
    sorted.loadPixels();
    image(sorted, 0, 0);

    System.out.print("doing it");

    int j = 0;
    j = Sort(sorted, j, 0.0, 50.0);
    j = Sort(sorted, j, 0.50, 100.0);
    j = Sort(sorted, j, 0.100, 150.0);
    j = Sort(sorted, j, 0.150, 200.0);
    j = Sort(sorted, j, 0.200, 256.0);

    System.out.print("done");
    sorted.updatePixels();

    image(sorted, 0, 0);
    save("guy_sorted.png"); 
    noLoop();
}
Run Code Online (Sandbox Code Playgroud)