MG lolenstine
MG lolenstine

Reputation: 51

Replace colors in an image without having to iterate through all pixels

Let's say that I have an image like this one (in reality, I have a lot of them, but let's keep it simple) example picture and I'd like to replace background color (that's the one that's not roads) with green color.

To do that I'd have to iterate through all of the pixels in the map and replace ones that match the color I want to remove.

But as you might think, my image is not a simple as 256x256 picture, but it's slightly bigger, it's 1440p, and the performance drop is significant.

How would I replace all of the unwanted pixels without iterating through all of the pixels.

I'm working with Processing 3 - Java(Android) and I'm currently using this piece of code:

for (x = 0; x < img.width; x++){
    for (int y = 0; y < img.height; y++) {
         //Color to transparent
         int index = x + img.width * y;
         if (img.pixels[index] == -1382175 || img.pixels[index] == 14605278 || img.pixels[index] == 16250871) {
            img.pixels[index] = color(0, 0, 0, 0);
         } else {
            img.pixels[index] = color(map(bright, 0, 255, 64, 192));
         }
    }
}

Upvotes: 0

Views: 174

Answers (1)

MG lolenstine
MG lolenstine

Reputation: 51

Solved it with this one:

private PImage swapPixelColor(PImage img, int old, int now) {
    old &= ~0x00000000;
    now &= ~0x00000000;

    img.loadPixels();
    int p[] = img.pixels, i = p.length;

    while (i-- != 0) if ((p[i]) == old) p[i] = now;

    img.updatePixels();
    return img;
}

It works like a charm and it takes almost no time:

Swapped colors in 140ms // That's replacing it three times(different colors ofc)

Upvotes: 0

Related Questions