fabianmoronzirfas
2/24/2016 - 11:36 AM

imageFilteringSimple.pde

/**
 * ImageFiltering
 * This sketch will help us to adjust the filter values to optimize blob detection
 *
 * It uses the OpenCV for Processing library by Greg Borenstein
 * https://github.com/atduskgreg/opencv-processing
 * 
 * @author: Jordi Tost (@jorditost)
 *
 * Versions with blob persistence here: 
 * https://github.com/jorditost/ImageFiltering/tree/master/ImageFilteringWithBlobPersistence
 * https://github.com/jorditost/ImageFiltering/tree/master/ImageFilteringWithBlobPersistenceAdvanced
 *
 * University of Applied Sciences Potsdam, 2014
 */

import gab.opencv.*;
import java.awt.Rectangle;
import java.awt.Polygon;
import processing.video.*;
import controlP5.*;

OpenCV opencv;
Capture video;
PImage src, preProcessedImage, processedImage, contoursImage;
ArrayList<Contour> contours;

float contrast = 1.35;
int brightness = 0;
int threshold = 75;
boolean showSrc = false;
boolean showPre = false;
boolean showPro = true;
boolean useAdaptiveThreshold = false; // use basic thresholding
int thresholdBlockSize = 489;
int thresholdConstant = 45;
int blobSizeThreshold = 10;
int blurSize = 4;

// Control vars
ControlP5 cp5;
int buttonColor;
int buttonBgColor;

void setup() {
  frameRate(15);
  video = new Capture(this, 640, 480);
  video.start();
  opencv = new OpenCV(this, 640, 480);
  contours = new ArrayList<Contour>();
  size(640, 480, P2D);
}
void draw() {
  // Read last captured frame
  if (video.available()) {
    video.read();
  }
  // Load the new frame of our camera in to OpenCV
  opencv.loadImage(video);
  src = opencv.getSnapshot();
  ///////////////////////////////
  // <1> PRE-PROCESS IMAGE
  // - Grey channel 
  // - Brightness / Contrast
  ///////////////////////////////
  // Gray channel
  opencv.gray();
  //opencv.brightness(brightness);
  opencv.contrast(contrast);
  // Save snapshot for display
  preProcessedImage = opencv.getSnapshot();
  ///////////////////////////////
  // <2> PROCESS IMAGE
  // - Threshold
  // - Noise Supression
  ///////////////////////////////
  // Adaptive threshold - Good when non-uniform illumination
  if (useAdaptiveThreshold) {
    // Block size must be odd and greater than 3
    if (thresholdBlockSize%2 == 0) thresholdBlockSize++;
    if (thresholdBlockSize < 3) thresholdBlockSize = 3;
    opencv.adaptiveThreshold(thresholdBlockSize, thresholdConstant);

    // Basic threshold - range [0, 255]
  } else {
    opencv.threshold(threshold);
  }
  // Invert (black bg, white blobs)
  opencv.invert();
  // Reduce noise - Dilate and erode to close holes
  opencv.dilate();
  opencv.erode();
  // Blur
  opencv.blur(blurSize);
  // Save snapshot for display
  processedImage = opencv.getSnapshot();
  ///////////////////////////////
  // <3> FIND CONTOURS  
  ///////////////////////////////
  // Passing 'true' sorts them by descending area.
  contours = opencv.findContours(true, true);
  // Save snapshot for display
  contoursImage = opencv.getSnapshot();
  if (showSrc) image(src, 0, 0);
  if (showPre) image(preProcessedImage, 0, 0);
  if (showPro) image(processedImage, 0, 0);

  //  
  //for (int i=0; i<contours.size(); i++) {
  //  Contour contour = contours.get(i);
  //  noFill();
  //  stroke(0, 255, 0);
  //  strokeWeight(3);
  //  contour.draw();
  //}

  if (contours.size() > 0) {
    Contour contour = contours.get(0);
    ArrayList <PVector> points = contour.getPoints();
    // display them
    Polygon poly = new Polygon();
    fill(0, 255, 0, 100);
    beginShape();

    for (int i = 0; i < points.size(); i++) {
      int x = int(points.get(i).x);
      int y = int( points.get(i).y);
      poly.addPoint(x, y);
      vertex(x, y);
    }
    endShape(CLOSE);
   
    // ArrayList <PVector> allMyPoints
    // size()
    // get()
    // add()
    // PVector
    // 
    //for (int j = 0; j < allMyPoints.size(); j++) {
    //  if (poly.contains(allMyPoints.get(j).x, allMyPoints.get(j).y) == false) {
    //    ellipse(x,y, 10,10);
        
    //  }else{
    //    ellipse(x,y, 5,5);
    //  }
    //} 


  //for(int x = 0; x < width;x +=5 ){
  //  for(int y = 0; y <height; y += 5){

  //  ellipse(x, y, 5,5);
  //  }
  //}
  
    //Rectangle r = contour.getBoundingBox();
    //if ((r.width < blobSizeThreshold || r.height < blobSizeThreshold))
    //stroke(255, 0, 0);
    //fill(255, 0, 0, 150);
    //strokeWeight(2);
    //rect(r.x, r.y, r.width, r.height);
    //}
  }
}
//for (int i=0; i<contours.size(); i++) {
//  Contour contour = contours.get(i);
//  Rectangle r = contour.getBoundingBox();
//  if ((r.width < blobSizeThreshold || r.height < blobSizeThreshold))
//    continue;
//  stroke(255, 0, 0);
//  fill(255, 0, 0, 150);
//  strokeWeight(2);
//  rect(r.x, r.y, r.width, r.height);
//}
//}
//