Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. import gab.opencv.*;
  2. import processing.video.*;
  3. import java.awt.Rectangle;
  4.  
  5. Capture video;
  6. OpenCV opencv;
  7. PImage src, colorFilteredImage;
  8. ArrayList<Contour> contours;
  9.  
  10. // <1> Set the range of Hue values for our filter
  11. int rangeLow = 20;
  12. int rangeHigh = 35;
  13.  
  14. int num = 40;
  15. float mx[] = new float[num];
  16. float my[] = new float[num];
  17.  
  18. void setup() {
  19. String[] cameras = Capture.list();
  20.  
  21. if (cameras.length == 0) {
  22. println("There are no cameras available for capture.");
  23. exit();
  24. } else {
  25. println("Available cameras:");
  26. for (int i = 0; i < cameras.length; i++) {
  27. println(cameras[i]);
  28. }
  29. }
  30. video = new Capture(this, 640, 480, cameras[0]);
  31. video.start();
  32.  
  33. opencv = new OpenCV(this, video.width, video.height);
  34. contours = new ArrayList<Contour>();
  35.  
  36. size(640, 480, P2D);
  37. }
  38.  
  39. void draw() {
  40.  
  41. // Read last captured frame
  42. if (video.available()) {
  43. video.read();
  44. }
  45. // <2> Load the new frame of our movie in to OpenCV
  46. opencv.loadImage(video);
  47.  
  48. // Tell OpenCV to use color information
  49. opencv.useColor();
  50. src = opencv.getSnapshot();
  51.  
  52. // <3> Tell OpenCV to work in HSV color space.
  53. opencv.useColor(HSB);
  54.  
  55. // <4> Copy the Hue channel of our image into
  56. // the gray channel, which we process.
  57. opencv.setGray(opencv.getH().clone());
  58.  
  59. // <5> Filter the image based on the range of
  60. // hue values that match the object we want to track.
  61. opencv.inRange(rangeLow, rangeHigh);
  62.  
  63. // <6> Get the processed image for reference.
  64. colorFilteredImage = opencv.getSnapshot();
  65.  
  66. ///////////////////////////////////////////
  67. // We could process our image here!
  68. // See ImageFiltering.pde
  69. ///////////////////////////////////////////
  70.  
  71. // <7> Find contours in our range image.
  72. // Passing 'true' sorts them by descending area.
  73. contours = opencv.findContours(true, true);
  74.  
  75. // <8> Display background images
  76. image(src, 0, 0);
  77. image(colorFilteredImage, src.width, 0);
  78.  
  79. // <9> Check to make sure we've found any contours
  80. if (contours.size() > 10) {
  81. // <9> Get the first contour, which will be the largest one
  82. Contour biggestContour = contours.get(0);
  83.  
  84. // <10> Find the bounding box of the largest contour,
  85. // and hence our object.
  86. Rectangle r = biggestContour.getBoundingBox();
  87. // <12> Draw a dot in the middle of the bounding box, on the object
  88. // Cycle through the array, using a different entry on each frame.
  89. // Using modulo (%) like this is faster than moving all the values over.
  90. int which = frameCount % num;
  91. mx[which] = r.x + r.width/2;
  92. my[which] = r.y + r.height/2;
  93.  
  94. noStroke();
  95. fill(255);
  96. for (int i = 0; i < num; i++) {
  97. // which+1 is the smallest (the oldest in the array)
  98. int index = (which+1 + i) % num;
  99. ellipse(mx[index], my[index], i, i);
  100. }
  101. }
  102. }
  103.  
  104.  
  105.  
  106.  
  107. void mousePressed() {
  108.  
  109. color c = get(mouseX, mouseY);
  110. println("r: " + red(c) + " g: " + green(c) + " b: " + blue(c));
  111.  
  112. if ( green(c) >= 100) {
  113. println("GROEN!");
  114. }
  115.  
  116. int hue = int(map(hue(c), 0, 255, 0, 180));
  117. println("hue to detect: " + hue);
  118.  
  119. rangeLow = hue - 2;
  120. rangeHigh = hue + 2;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement