subtract image colors
in
Programming Questions
•
8 months ago
Hi,
I have a question about pixels color. I'm writing a code for subtract all colors of an image except red, but i have some problems. The result isn't what I expected. Subtracting G and B values, R comes off
equally. An other problem is how subtract black pixels?! I put here my code:
- PImage img;
- PImage imgOut;
- void setup () {
- size(1200, 800);
- img = loadImage("sea.jpg");
- imgOut = createImage(img.width, img.height, ARGB);
- }
- void draw() {
- background(100,100,100);
- copyPixels(img, imgOut, 20, 255, 20, 255);
- image(imgOut, 0, 0);
- }
- void copyPixels(PImage img, PImage imgOut, float gMin, float gMax, float bMin, float bMax) {
- img.loadPixels();
- imgOut.loadPixels();
- for (int i=0; i<img.pixels.length; i++) {
- color c = img.pixels[i];
- float r = red(c); // Get the red value
- float g = green(c);
- float b = blue(c);
- if (g >= gMin && g <= gMax || b >= bMin && b <= bMax) {
- imgOut.pixels[i] = color(0, 0);
- }
- else {
- imgOut.pixels[i] = c;
- }
- }
- imgOut.updatePixels();
- }
1