OpenCV - How can I ad a different random image on every face?

edited October 2016 in Kinect

I want to write a Code, where every face gets another random image out of 10 images I have. I struggle to separate the different faces. Every time I try, the same image appears on all faces. Would be great, if someone can give me a hint how to separate the faces for the Code.

You will see, I was a bit desperate. I see the mistake, its a big one I know. But I can't see the solution.

import gab.opencv.*;
import java.awt.*;
 int num = 3;
 PImage[] myImageArray = new PImage[num];
 Capture video;
 OpenCV opencv;

 void setup() {

for (int i=0; i<myImageArray.length; i++){             
   myImageArray[i] = loadImage( str(i) + ".png");

  size(800, 600);
   video = new Capture(this, 800/2, 600/2);
   opencv = new OpenCV(this, 800/2, 600/2);


 void draw() {
   image(video, 0, 0 );

  Rectangle[] faces = opencv.detect();
   println(faces.length);                    //print number of faces
   for (int i = 0; i < faces.length; i++) {
     println(faces[i].x + "," + faces[i].y); //print position(x/y) of faces 
  image(myImageArray[(int)random(num)], faces[i].x-70, faces[i].y-60, faces[i].width+80, faces[i].height+80);

 void captureEvent(Capture c) {;


  • Please make your code readable and testable by other forum members by formatting your code.

    1. edit your post
    2. highlight your code
    3. press CTRL-o / Apple-o
    4. save your post
  • Your code works for me. The only issue I am having is that each face detected does not hold a single image but it changes all the time. A solution to this is to keep track of the center of each face detected and associated that coordinate position to an image.

    You say that all faces get assigned the same image? That is odd, mine every face gets a random image.

    Also you should remove the scale() function as you do not needed here.


Sign In or Register to comment.