Programme de génération de forme géométrique de base par sélection et suivi de couleur.
Source: colorCenterDetector.pde / Processing 3.5.3 + librairie blobDetection et video
// colorCenterDetector.pde - processing 3.5.3 - transat@stephanecabee.net
import blobDetection.*;
import processing.video.*;
Capture video;
BlobDetection theBlobDetection;
PImage container;
myColor targetColor;
PImage img;
boolean newFrame=false;
int vectorTreshold = 1;
ArrayList<myColor> trackColor = new ArrayList<myColor>();
myColor trackPixelColor;
float treshold = 40;
float sizeTreshold = 40;
void setup(){
size(1280,960);
String[] cameras = Capture.list();
if (cameras == null) {
println("Failed to retrieve the list of available cameras, will try the default...");
video = new Capture(this, 640, 480, 30);
} if (cameras.length == 0) {
println("There are no cameras available for capture.");
exit();
} else {
println("Available cameras:");
printArray(cameras);
video = new Capture(this, cameras[28]); // change cameras number to your choice
video.start();
}
img = new PImage(width,height);
container = new PImage(width,height);
theBlobDetection = new BlobDetection(width, height);
theBlobDetection.setPosDiscrimination(true);
theBlobDetection.setThreshold(0.2f);
}
void captureEvent(Capture video){
video.read();
newFrame = true;
}
void draw(){
if (newFrame) {
newFrame=false;
image(video,0,0,width,height);
for(myColor c : trackColor){
trackingColor(c);
}
}
}
void trackingColor(myColor c){
container.loadPixels();
for (int x = 0; x < video.width; x ++ ) {
for (int y = 0; y < video.height; y ++ ) {
int loc = x + y*video.width;
color currentColor = video.pixels[loc];
float r1 = red(currentColor);
float g1 = green(currentColor);
float b1 = blue(currentColor);
float r2 = c.r;
float g2 = c.g;
float b2 = c.b;
float d = dist(r1, g1, b1, r2, g2, b2);
if (d < treshold) {
container.pixels[loc] = color(0);
}else{
container.pixels[loc] = color(255);
}
}
}
container.updatePixels();
theBlobDetection.computeBlobs(container.pixels);
drawBlobsAndEdges(c,true,false);
}
void drawBlobsAndEdges(myColor pixelColor, boolean drawBlobs, boolean drawEdges)
{
noFill();
Blob b;
EdgeVertex eA,eB;
for (int n=0 ; n<theBlobDetection.getBlobNb() ; n++)
{
b=theBlobDetection.getBlob(n);
if (b!=null)
{
// Blobs; draw rectangle on the tracked color
if (drawBlobs && b.w*width > sizeTreshold && b.h*height > sizeTreshold)
{
noStroke();
fill(pixelColor.r,pixelColor.g,pixelColor.b);
rectMode(CENTER);
rect(b.x*width,b.y*height, b.w*width,b.h*height);
fill(255);
ellipse(b.x*width,b.y*height, 5, 5);
}
}
// Edges
if (drawEdges)
{
strokeWeight(3);
stroke(0,255,0);
for (int m=0;m<b.getEdgeNb();m++)
{
eA = b.getEdgeVertexA(m);
eB = b.getEdgeVertexB(m);
if (eA !=null && eB !=null)
line(
eA.x*width, eA.y*height,
eB.x*width, eB.y*height
);
}
}
}
}
void mousePressed() {
// Save color where the mouse is clicked in trackColor variable
int loc = mouseX + mouseY*video.width;
targetColor = new myColor(red(video.pixels[loc]), green(video.pixels[loc]), blue(video.pixels[loc]));
trackColor.add(targetColor);
}
void keyPressed(){
if(key == 'a'){
sizeTreshold += 5;
}else if(key == 'q'){
sizeTreshold -= 5;
}else if(key == 'e'){
treshold += 5;
}else if(key == 'd'){
treshold -= 5;
}
println("sizeTreshold: " + sizeTreshold);
println("treshold: " + treshold);
}
class myColor{
float r, g, b;
myColor(float colorRed, float colorGreen, float colorBlue){
r = colorRed;
g = colorGreen;
b = colorBlue;
}
}
Be First to Comment