Changing colour depending on location

Hi there, im a huge beginner to processing, but i was wondering how you could go about changin the colour of an image depending on the location of an object, (a users face location which is being located by OpenCV).

Thanks

Try tint():
tint(float red, float green, float blue)
https://processing.org/reference/tint_.html

1 Like

How would i go about calling the location of an object? ie. coordinated of a face or a mouse. Also whats the best way to have it change colour only when that object is in a certain area?

I am not very familiar with OpenCV, but I’ll try to find out what I can :)

Do you mean a code object, (created from a class), or a real-world object that was found by OpenCV?
Could you expand a little on your question?

tint is the right way, after get the face to a extra image

// copy PDE / File / Examples / Contributed Libraries / OpenCV for Processing / FaceDetection
// mod for 
// https://discourse.processing.org/t/changing-colour-depending-on-location/14799
import gab.opencv.*;
import java.awt.Rectangle;

OpenCV opencv;
Rectangle[] faces;
PImage face;
int x,y,w,h;

void setup() {
  opencv = new OpenCV(this, "test.jpg");
  size(1080, 720);

  opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);  
  faces = opencv.detect();
  println("found faces "+faces.length);
  if ( faces.length >= 1) {
    x = faces[0].x;
    y = faces[0].y;
    w = faces[0].width;
    h = faces[0].height;
    face = opencv.getInput().get(x,y,w,h);
  } 
  noLoop();
}

void draw() {
  image(opencv.getInput(), 0, 0);
  tint(0, 153, 204, 126);
  image(face,x,y);
  noFill();
  stroke(0, 255, 0);
  strokeWeight(3);
  for (int i = 0; i < faces.length; i++) {
    rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
  }
  
}

1 Like

Bascally, i am looking to have an image change colour if the face (from openCV) is in a certain area.

sorry, that now sounds much more easy to change the whole picture color

// copy PDE / File / Examples / Contributed Libraries / OpenCV for Processing / FaceDetection
// mod for 
// https://discourse.processing.org/t/changing-colour-depending-on-location/14799
// v02. only change whole picture color if face rect in spec area

import gab.opencv.*;
import java.awt.Rectangle;

OpenCV opencv;
Rectangle[] faces;
PImage face;               // optional
int x, y, w, h;            // face position from CV
PVector centerf, areaf;
int xk=400, yk=400, wk=150, hk=150;     // check area for face position
int lim = 200;  // pix dist both area centers
boolean over= false;

void setup() {
  opencv = new OpenCV(this, "test.jpg");
  size(1080, 720);

  opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);  
  faces = opencv.detect();
  println("found faces "+faces.length);
  if ( faces.length >= 1) {
    x = faces[0].x;
    y = faces[0].y;
    w = faces[0].width;
    h = faces[0].height;
    println(x, y, w, h);
    centerf = new PVector(x+w/2, y+h/2);
    areaf   = new PVector(xk+wk/2, yk+hk/2);
    over = ( areaf.dist(centerf) < lim );     // check if face near defined area center    
    face = opencv.getInput().get(x, y, w, h); // optional
  } 
  noLoop();
}

void draw() {
  if ( over ) tint(200, 0, 0, 126);              // colorchange all picture if face close
  image(opencv.getInput(), 0, 0);
  image(face, 0, 0);                             // optional
  for (int i = 0; i < faces.length; i++) {
    noFill();
    stroke(0, 255, 0);
    strokeWeight(3);
    rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
    stroke(200, 0, 0);
    rect(xk,yk,wk,hk);
    line(xk+wk/2,yk+hk/2,x+w/2,y+h/2);
  }
}


1 Like

Hahah im still a little confused. Ive attached an image of the print.

Basically, the seahorse is the printed image on to the persons face. And the coral in the corners would be grey (dis-coloured). my plan is to have the coral turn colourful if the seahorse (face) is near the coral

i expected that this part is working already?
but you not show your code so i started from a working example of that library
what i just loaded after you posted this.

if you have problems with that library i can not help as i also not know it.
anyhow you might need to rephrase your question


independent that latest approach with

checking the distance of two rectangles

  • one predefined in code
  • one found by the openCV

and depending on that do a TINT on the picture

is that part answered now?

Yeah the basic code works, where it detects a face and prints an image onto the face. Ive shown my code below;

As for my question, the coral image and the actual background are 2 different images. And i just want the coral to change tint() if the face is near it. Does that make more sense?

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);

frameRate(60);

  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();
    
  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