Simplifying colours.
in
Contributed Library Questions
•
1 year ago
Hi everyone,
I'm chasing 2 answers to help with a script I'm currently working on + any other additions, revisions or simpler ways you can see on what I've done already.
I've currently got an OpenCV head tracking script that identifies a rectangle one head height below the actual head and its averaging the color into a RGB format.
This output is too complex for what I need so I need a way to simplify the numbers down into the ones below, in addition to this once simplified I need the number in brackets to be assigned to a 'cref' integer for later use.
Red - 255, 0, 0 (1)
Green - 0, 255, 0 (2)
Blue - 0, 0, 255 (3)
Yellow - 0, 255, 255 (4)
Black - 0, 0, 0 (5)
White - 255, 255, 255 (6)
I envision a huge < & > else if statement but wanted to see if anyone had other thoughts on how to make it more streamlined.
The second question: I want this all this like a slideshow, by that I mean. Until someone walks into the frame I want the head tracking to continuously scan. But once it identifies someone's head I don't want it to refresh, I want it to display the simplified color for 20seconds then go back to searching for another head.
I've seen many different ways to do this but I cant have the program using a wait function or anything like that as I need some other functions run during this process.
Here is my code:
I'm chasing 2 answers to help with a script I'm currently working on + any other additions, revisions or simpler ways you can see on what I've done already.
I've currently got an OpenCV head tracking script that identifies a rectangle one head height below the actual head and its averaging the color into a RGB format.
This output is too complex for what I need so I need a way to simplify the numbers down into the ones below, in addition to this once simplified I need the number in brackets to be assigned to a 'cref' integer for later use.
Red - 255, 0, 0 (1)
Green - 0, 255, 0 (2)
Blue - 0, 0, 255 (3)
Yellow - 0, 255, 255 (4)
Black - 0, 0, 0 (5)
White - 255, 255, 255 (6)
I envision a huge < & > else if statement but wanted to see if anyone had other thoughts on how to make it more streamlined.
The second question: I want this all this like a slideshow, by that I mean. Until someone walks into the frame I want the head tracking to continuously scan. But once it identifies someone's head I don't want it to refresh, I want it to display the simplified color for 20seconds then go back to searching for another head.
I've seen many different ways to do this but I cant have the program using a wait function or anything like that as I need some other functions run during this process.
Here is my code:
Copy code
- import hypermedia.video.*;
- import java.awt.Rectangle;
- OpenCV opencv;
- float grabAverageR;
- float grabAverageG;
- float grabAverageB;
- color grabAveCol;
- PImage grab;
- void setup() {
- size( 540, 420 );
- opencv = new OpenCV(this);
- opencv.capture(width, height);
- opencv.cascade(OpenCV.CASCADE_FRONTALFACE_ALT);
-
- Rectangle[] faces;
- }
- void draw() {
- opencv.read();
- image( opencv.image(), 0, 0 );
- filter(POSTERIZE, 3);
- // detect anything ressembling a FRONTALFACE
- Rectangle[] faces = opencv.detect();
-
- // draw detected face area(s)
- noFill();
- stroke(255, 0, 0);
- for ( int i=0; i<faces.length; i++ ) {
- noFill();
- rect( faces[i].x, faces[i].y, faces[i].width, faces[i].height );
- grab=get(faces[i].x, faces[i].y+int(faces[i].height*1.75), faces[i].width, faces[i].height);
- stroke(0, 255, 0);
- rect(faces[i].x, faces[i].y+(faces[i].height*1.75), faces[i].width, faces[i].height);
- grab.loadPixels();
- //return values to 0
- grabAverageR=0;
- grabAverageG=0;
- grabAverageB=0;
- for (int t=0;t<grab.pixels.length;t++) {
- grabAverageR=grabAverageR+red(grab.pixels[t]);
- grabAverageG=grabAverageG+green(grab.pixels[t]);
- grabAverageB=grabAverageB+blue(grab.pixels[t]);
- }
- grabAverageR=grabAverageR/grab.pixels.length;
- grabAverageG=grabAverageG/grab.pixels.length;
- grabAverageB=grabAverageB/grab.pixels.length;
- grabAveCol=color(grabAverageR, grabAverageG, grabAverageB);
- fill(grabAveCol);
- rect(i*50, 0, 50, 50);
- println("R"+int(grabAverageR)+" G"+int(grabAverageG)+" B"+int(grabAverageB));
- }
- }
1