Quantcast
Channel: Kinect - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 530

How to capture a frame and find contours of it?

$
0
0

Hi everybody, I don't know if it's the wrong section, by the way, I have this find contours code in which I contour a specific image from my files, but now I need to change it and I would like to capture a frame with the webcam and then find the contour of it. How can I do it? Thanks to everybody who will help!

here's the code I have now

import gab.opencv.*;

PImage src;
OpenCV opencv;
ArrayList<Contour> contours;
int pointsTot;
int pointsCurr;
float countourApproximation;



////////////////////////////////////////////////////////////////////////////////

void setup()
{
  size( 1080, 720, P2D );

  colorMode( HSB, 360, 100, 100 );

  opencv = new OpenCV( this, 1080, 720 );

  src = loadImage("room.jpg");
  countourApproximation = 2;
  resetContours();
}


////////////////////////////////////////////////////////////////////////////////

void resetContours()
{
  opencv.loadImage( src );

  opencv.gray();
  opencv.blur(5);
  opencv.threshold(60);

  // tutte le operazioni possibili sono elencate e descritte in:
  // http://atduskgreg.github.io/opencv-processing/reference/gab/opencv/OpenCV.html
  // [vedi "Method Summary"]

  contours = opencv.findContours();

  pointsTot = 0;
  for (Contour contour : contours) {
    contour.setPolygonApproximationFactor(countourApproximation);
    pointsTot += contour.getPolygonApproximation().getPoints().size();
  }
  //  pointsCurr = pointsTot-1;
  pointsCurr = 1;
}


////////////////////////////////////////////////////////////////////////////////

void draw()
{
  //  background( 0 );

  if (pointsCurr < pointsTot-1) {
    noTint();
  } else {
    tint( 60 );
  }
  image( src, 0, 0 );

  noFill();
  strokeWeight(3);

  int pointsCount = 0;                                  // numero di punti (dei segmenti) visualizzati
  for (int c=0; c<contours.size(); ++c) {

    Contour contour = contours.get( c );
    ArrayList<PVector> points = contour.getPolygonApproximation().getPoints();

    float h = map( c, 0, contours.size(), 0, 360 );

    beginShape();

    for (int p=0; p<points.size(); ++p) {

      if (pointsCount < pointsCurr) {

        PVector point = points.get( p );

        float s = map( p, 0, points.size(), 20, 100 );

        stroke( h, s, 100 );
        vertex( point.x, point.y );
        //        curveVertex( point.x, point.y );

        ++pointsCount;
      } else {
        break;
      }
    }

    endShape();
  }

  if (pointsCurr < pointsTot-1) {
    ++pointsCurr;
  }
}


////////////////////////////////////////////////////////////////////////////////

void mousePressed()
{

  countourApproximation = exp( random(3.5) );

  resetContours();
}

Viewing all articles
Browse latest Browse all 530

Trending Articles