Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. //================================================
  2. // Ex26
  3. // Video capture and visualization
  4. // ===============================================
  5.  
  6. import processing.video.*;
  7. Capture camera;
  8. int screenWidth= 1280, screenHeight = 720;
  9. color trackingColor = color(255,0,0);
  10. int matchingTolerance =10;
  11. int trackColor = 1;
  12. PImage img;
  13. // ===============================================
  14. // ===============================================
  15. void setup()
  16. {
  17. size(screenWidth, screenHeight); // Set window Size
  18.  
  19. println(Capture.list()); // Get available devices and resolutions
  20.  
  21. camera = new Capture(this, screenWidth, screenHeight);
  22.  
  23. camera.start();
  24. img = loadImage("mostacho.png");
  25.  
  26. }
  27.  
  28.  
  29. // ===============================================
  30. // ===============================================
  31.  
  32. void draw()
  33. {
  34.  
  35.  
  36. image(camera,0,0);
  37. trackSelectedColor();
  38.  
  39.  
  40. }
  41. void mousePressed(){
  42. int loc=(mouseY*screenWidth + mouseX);
  43. trackingColor = camera.pixels[loc];
  44.  
  45.  
  46. }
  47. // ===============================================
  48. // ===============================================
  49.  
  50. void captureEvent(Capture camera)
  51. {
  52. camera.read();
  53. }
  54. // ===============================================
  55. // ===============================================
  56. void trackSelectedColor()
  57. {
  58.  
  59. camera.loadPixels();
  60.  
  61.  
  62. float bestMatch = 100000;
  63. int closestX = 0;
  64. int closestY = 0;
  65.  
  66. for (int x = 0; x < camera.width; x ++ )
  67. {
  68. for (int y = 0; y < camera.height; y ++ )
  69. {
  70. int linearPosition = x + y*camera.width;
  71. color currentColor = camera.pixels[linearPosition];
  72. float r1 = red(currentColor);
  73. float g1 = green(currentColor);
  74. float b1 = blue(currentColor);
  75. float r2 = red(trackingColor);
  76. float g2 = green(trackingColor);
  77. float b2 = blue(trackingColor);
  78. float d = dist(r1,g1,b1,r2,g2,b2);
  79.  
  80. // If current color is more similar to tracked color than
  81. // closest color, save current location and current difference
  82. if (d < bestMatch )
  83. {
  84. bestMatch = d;
  85. closestX = x;
  86. closestY = y;
  87. }
  88. }
  89. }
  90.  
  91. if ((bestMatch < matchingTolerance) && (trackColor ==1 ))
  92. {
  93. println("Similar Point found!!!");
  94. // Draw a circle at the tracked pixel
  95. fill(trackingColor);
  96. stroke(0);
  97. image(img, closestX-(img.width/2), closestY-(img.height/2));
  98. // String s="CoordenadaX= "+ closestX+ "CloordenadaY= "+ closestY;
  99. //text(s,closestX,closestY);
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement