I’m building a code that will allow you to recognize color cards and, depending on the color, a drawing will appear in the drawing area.
Example:
red = red round
blue = blue round
green = green square
I was able to define my areas, but I can not ask him to display a shape when he tracks the right color.
I show you my code, my English is not great I hope you understand.
import processing.video.*;
Capture video;
PImage scanArea;
color[] palette = {color(28, 5, 253),
color(100, 205, 205),
color(100, 100, 100),
color(250, 5, 40),
color(0, 255, 0),
color(255, 255, 0)};
//color red = color(0,100,100);
color r;
color cible;
int proche;
int index;
int frameWidth = 200; // largeur de la zone de scan
int frameHeight = 200; // hauteur de la zone de scan
Zone maZone1 = new Zone(10, 71, color(255));
Zone maZone2 = new Zone(10, 271, color(255));
void setup() {
size(640, 480);
video = new Capture(this, width, height);
video.start();
colorMode(HSB, 360, 100, 100);
noStroke();
scanArea = createImage(frameWidth, frameHeight, RGB);
}
void captureEvent(Capture video) {
video.read(); // Lecture de l'image provenant de la camera
}
void draw() {
video.loadPixels();
if (video.available()) video.read(); // Update Capture
pushMatrix();
scale(-1, 1); // video en miroir
translate(-video.width, 0);
image(video, 0, 0);
popMatrix();
if (frameCount % 50 == 0) { // Scan sans arret image par image
scanArea.copy(video, 200, 200, frameWidth, frameHeight, 0, 0, frameWidth, frameHeight);
}
proche = 360;
index = 0;
maZone1.display(); // affiche la zone de scan 1
maZone2.display(); // affiche la zone de scan 2
structure();
if (mouseX > 10 && mouseX < 210 && mouseY > 71 && mouseY < 275) {
couleurCible1(get(mouseX, mouseY)); // couleur comparee, ciblée
for (int i= 0; i < palette.length; i++) {
int c = int(hue(palette[i]));
int distance = abs(c - int(hue(cible)));
if (distance < proche) {
proche = distance;
index = i;
}
}
fill(0);
ellipse(index * 20 + 90, 30, 10, 10); // pointeur
}
if (mouseX > 10 && mouseX < 210 && mouseY > 271 && mouseY < 492) {
couleurCible2(get(mouseX, mouseY)); // couleur comparee, ciblée
for (int i= 0; i < palette.length; i++) {
int c = int(hue(palette[i]));
int distance = abs(c - int(hue(cible)));
if (distance < proche) {
proche = distance;
index = i;
}
}
fill(0);
ellipse(index * 20 + 90, 30, 10, 10); // pointeur
}
}
void couleurCible1(color c_) { // affichage de la couleur ciblé
cible = c_;
noStroke();
fill(c_);
rect(20, 20, 20, 20);
r = color(0, 100, 100);
if(cible == r); {
//fill(0,100,100);
rect( width/1.5, height/2.8, 150, 150);
}
}
void couleurCible2(color c_) { // affichage de la couleur ciblé
cible = c_;
noStroke();
fill(c_);
rect(50, 20, 20, 20);
}
class Zone {
//Déclaration des paramètres de base de la zone
float x;
float y;
color couleur;
//Constructeur de la zone
Zone (float nouvX, float nouvY, color nouvCouleur) {
x = nouvX;
y = nouvY;
couleur = nouvCouleur;
}
//Dessin de la zone
void display() {
strokeWeight(2);
stroke(0);
noFill();
rect(x, y, frameWidth,frameHeight);
}
}
void structure() {
fill(255, 0, 255);
rect(10, 71, 25, 24); // Z1 rect
rect(10, 271, 25, 24); // Z2 rect
noStroke();
rect(10, 10, 200, 52);
rect(width/1.8, 0, width, height); // zone de dessin
stroke(0);
strokeWeight(1);
rect(20, 20, 20, 20);
rect(50, 20, 20, 20);
fill(0);
textSize(13);
text("C1", 22, 57);
text("C2", 52, 57);
text("Z1", 15, 87);
text("Z2", 15, 287);
for (int i = 0; i < palette.length; i++) { // dessin de la palette
noStroke();
fill(palette[i]);
rect(i * 20 + 80, 20, 20, 20);
}
}
My problem occurs at this time.
void couleurCible1(color c_) { // affichage de la couleur ciblé
cible = c_;
noStroke();
fill(c_);
rect(20, 20, 20, 20);
r = color(0, 100, 100);
if(cible == r); {
//fill(0,100,100);
rect( width/1.5, height/2.8, 150, 150);
Any tips, or examples.
Thank you for your help.