Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //================================================
- // Ex26
- // Video capture and visualization
- // ===============================================
- import processing.video.*;
- Capture camera;
- int screenWidth= 1280, screenHeight = 720;
- color trackingColor = color(255,0,0);
- int matchingTolerance =10;
- int trackColor = 1;
- PImage img;
- // ===============================================
- // ===============================================
- void setup()
- {
- size(screenWidth, screenHeight); // Set window Size
- println(Capture.list()); // Get available devices and resolutions
- camera = new Capture(this, screenWidth, screenHeight);
- camera.start();
- img = loadImage("mostacho.png");
- }
- // ===============================================
- // ===============================================
- void draw()
- {
- image(camera,0,0);
- trackSelectedColor();
- }
- void mousePressed(){
- int loc=(mouseY*screenWidth + mouseX);
- trackingColor = camera.pixels[loc];
- }
- // ===============================================
- // ===============================================
- void captureEvent(Capture camera)
- {
- camera.read();
- }
- // ===============================================
- // ===============================================
- void trackSelectedColor()
- {
- camera.loadPixels();
- float bestMatch = 100000;
- int closestX = 0;
- int closestY = 0;
- for (int x = 0; x < camera.width; x ++ )
- {
- for (int y = 0; y < camera.height; y ++ )
- {
- int linearPosition = x + y*camera.width;
- color currentColor = camera.pixels[linearPosition];
- float r1 = red(currentColor);
- float g1 = green(currentColor);
- float b1 = blue(currentColor);
- float r2 = red(trackingColor);
- float g2 = green(trackingColor);
- float b2 = blue(trackingColor);
- float d = dist(r1,g1,b1,r2,g2,b2);
- // If current color is more similar to tracked color than
- // closest color, save current location and current difference
- if (d < bestMatch )
- {
- bestMatch = d;
- closestX = x;
- closestY = y;
- }
- }
- }
- if ((bestMatch < matchingTolerance) && (trackColor ==1 ))
- {
- println("Similar Point found!!!");
- // Draw a circle at the tracked pixel
- fill(trackingColor);
- stroke(0);
- image(img, closestX-(img.width/2), closestY-(img.height/2));
- // String s="CoordenadaX= "+ closestX+ "CloordenadaY= "+ closestY;
- //text(s,closestX,closestY);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement