Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import gab.opencv.*;
- import processing.video.*;
- import java.awt.Rectangle;
- Capture video;
- OpenCV opencv;
- PImage src, colorFilteredImage;
- ArrayList<Contour> contours;
- // <1> Set the range of Hue values for our filter
- int rangeLow = 20;
- int rangeHigh = 35;
- int num = 40;
- float mx[] = new float[num];
- float my[] = new float[num];
- void setup() {
- String[] cameras = Capture.list();
- if (cameras.length == 0) {
- println("There are no cameras available for capture.");
- exit();
- } else {
- println("Available cameras:");
- for (int i = 0; i < cameras.length; i++) {
- println(cameras[i]);
- }
- }
- video = new Capture(this, 640, 480, cameras[0]);
- video.start();
- opencv = new OpenCV(this, video.width, video.height);
- contours = new ArrayList<Contour>();
- size(640, 480, P2D);
- }
- void draw() {
- // Read last captured frame
- if (video.available()) {
- video.read();
- }
- // <2> Load the new frame of our movie in to OpenCV
- opencv.loadImage(video);
- // Tell OpenCV to use color information
- opencv.useColor();
- src = opencv.getSnapshot();
- // <3> Tell OpenCV to work in HSV color space.
- opencv.useColor(HSB);
- // <4> Copy the Hue channel of our image into
- // the gray channel, which we process.
- opencv.setGray(opencv.getH().clone());
- // <5> Filter the image based on the range of
- // hue values that match the object we want to track.
- opencv.inRange(rangeLow, rangeHigh);
- // <6> Get the processed image for reference.
- colorFilteredImage = opencv.getSnapshot();
- ///////////////////////////////////////////
- // We could process our image here!
- // See ImageFiltering.pde
- ///////////////////////////////////////////
- // <7> Find contours in our range image.
- // Passing 'true' sorts them by descending area.
- contours = opencv.findContours(true, true);
- // <8> Display background images
- image(src, 0, 0);
- image(colorFilteredImage, src.width, 0);
- // <9> Check to make sure we've found any contours
- if (contours.size() > 10) {
- // <9> Get the first contour, which will be the largest one
- Contour biggestContour = contours.get(0);
- // <10> Find the bounding box of the largest contour,
- // and hence our object.
- Rectangle r = biggestContour.getBoundingBox();
- // <12> Draw a dot in the middle of the bounding box, on the object
- // Cycle through the array, using a different entry on each frame.
- // Using modulo (%) like this is faster than moving all the values over.
- int which = frameCount % num;
- mx[which] = r.x + r.width/2;
- my[which] = r.y + r.height/2;
- noStroke();
- fill(255);
- for (int i = 0; i < num; i++) {
- // which+1 is the smallest (the oldest in the array)
- int index = (which+1 + i) % num;
- ellipse(mx[index], my[index], i, i);
- }
- }
- }
- void mousePressed() {
- color c = get(mouseX, mouseY);
- println("r: " + red(c) + " g: " + green(c) + " b: " + blue(c));
- if ( green(c) >= 100) {
- println("GROEN!");
- }
- int hue = int(map(hue(c), 0, 255, 0, 180));
- println("hue to detect: " + hue);
- rangeLow = hue - 2;
- rangeHigh = hue + 2;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement