Wanting to change the saturation of an image depending on location

Hi, so im looking to change the saturation of the “coral” im my code. I have the image connected to open CV so it tracks your face. Im wanting to have the colour of the coral be in greyscale when the tracking image is in the middle of the screen, but then the coral will turn colourful when the tracked image gets close to it. Im not sure on how to do this since im kind of new to processing.

This is the a screen shot of my code and what it would look like. The seahorse is the tracked image and the coral is on the left and right.

Here is the code im working with. If you have any idea on how to implement my idea that be amazing !

import gab.opencv.*;
import processing.video.*;
import java.awt.*;

PImage img;
PImage background;
PImage imgCoral;

color colCoral;

Capture video;
OpenCV opencv;

void setup() {
size(1920, 1080);
colorMode (HSB);

  img = loadImage("photo1.png");
    
  imgCoral = loadImage ("coral.png");
  
  background=loadImage ("sea.png");
  
  video = new Capture(this, 640, 480);
  opencv = new OpenCV(this, 640, 480);
  opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);  

  video.start();
  
 
}

void draw() {
  background (background);
  image(imgCoral,0,0);
  scale(2);
  opencv.loadImage(video);
  
  noFill();
  stroke(0, 0, 0);
  strokeWeight(3);
  Rectangle[] faces = opencv.detect();
  println(faces.length);

  for (int i = 0; i < faces.length; i++) {
    println(faces[i].x + "," + faces[i].y);
    image(img, faces[i].x, faces[i].y, faces[i].width,faces[i].height);
   }
}

void captureEvent(Capture c) {
  c.read();
}

1 Like

Use the tint() command to change the saturation of your image:

pushStyle()
tint(...)
image(...)
popStyle();

Then, control that tint with a variable based on the location you are getting from opencv.

To change the saturation specifically, you could use a PShader – see shaders tutorial. For a simpler (although less efficient) approach, you can just loop over the pixels of your image and change them based on your value. Switch into colorMode(HSB) to make it easier to change the saturation channel of each pixel.

Another alternative is to desaturate your coral as a separate image, then cross-fade it or overlay it with your colored coral, using tint() and the alpha (transparency) channel.

So, many ways to do this.