import gab.opencv.*;
import processing.video.*;
import java.awt.Rectangle;
Capture video;
OpenCV opencv;
PImage src;
ArrayList<Contour> contours;
boolean colorA, colorB, colorC, colorD;
int maxColors = 4;
int[] hues;
int[] colors;
int rangeWidth = 10;
PImage[] outputs;
int colorToChange = -1;
void setup() {
video = new Capture(this, 640, 480);
opencv = new OpenCV(this, video.width, video.height);
contours = new ArrayList<Contour>();
size(opencv.width + opencv.width/4 + 30, opencv.height, P2D);
colors = new int[maxColors];
hues = new int[maxColors];
outputs = new PImage[maxColors];
video.start();
}
void draw() {
background(150);
if (video.available()) {
video.read();
}
opencv.loadImage(video);
opencv.useColor();
src = opencv.getSnapshot();
opencv.useColor(HSB);
image(src, 0, 0);
for (int i=0; i<outputs.length; i++) {
if (outputs[i] != null) {
image(outputs[i], width-src.width/4, i*src.height/4, src.width/4, src.height/4);
noStroke();
fill(colors[i]);
rect(src.width, i*src.height/4, 30, src.height/4);
}
}
detectColors();
textSize(20);
stroke(255);
fill(255);
if (colorToChange > -1) {
text("click to change color " + colorToChange, 10, 25);
} else {
text("press key [1-4] to select color", 10, 25);
}
// displayContoursBoundingBoxes();
}
void detectColors() {
for (int i=0; i<hues.length; i++) {
if (hues[i] <= 0) continue;
opencv.loadImage(src);
opencv.useColor(HSB);
opencv.setGray(opencv.getH().clone());
int hueToDetect = hues[i];
opencv.inRange(hueToDetect-rangeWidth/2, hueToDetect+rangeWidth/2);
opencv.erode();
outputs[i] = opencv.getSnapshot();
opencv.loadImage(outputs[i]);
contours = opencv.findContours(true, true);
if (outputs[i] != null) {
opencv.loadImage(outputs[i]);
contours = opencv.findContours(true, true);
for (int j=0; j<contours.size (); j++) {
Contour contour = contours.get(j);
Rectangle r = contour.getBoundingBox();
if ((r.width > 50 || r.height > 50) && (r.width < 200 || r.height < 200) ) {
stroke(colors[i]);
fill(colors[i], 150);
strokeWeight(2);
rect(r.x, r.y, r.width, r.height);
}
if ((r.x>0&&r.x<width)&&(r.y>0&&r.y<height)) {
if (i==0) {
println("color A");
}
if (i==1) {
println("color B");
}
if (i==2) {
println("color C");
}
if (i==3) {
println("color D");
}
}
}
}
}
}
void displayContoursBoundingBoxes() {
for (int i=0; i<contours.size (); i++) {
Contour contour = contours.get(i);
Rectangle r = contour.getBoundingBox();
if (r.width < 20 || r.height < 20)
continue;
stroke(255, 0, 0);
fill(255, 0, 0, 150);
strokeWeight(2);
rect(r.x, r.y, r.width, r.height);
}
}
void mousePressed() {
if (colorToChange > -1) {
color c = get(mouseX, mouseY);
// println("r: " + red(c) + " g: " + green(c) + " b: " + blue(c));
int hue = int(map(hue(c), 0, 255, 0, 180));
colors[colorToChange-1] = c;
hues[colorToChange-1] = hue;
// println("color index " + (colorToChange-1) + ", value: " + hue);
}
}
void keyPressed() {
if (key == '1') {
colorToChange = 1;
} else if (key == '2') {
colorToChange = 2;
} else if (key == '3') {
colorToChange = 3;
} else if (key == '4') {
colorToChange = 4;
}
}
void keyReleased() {
colorToChange = -1;
}