How to find contours on a image from the webcam

Hello everybody! I have a problem, i'm new to processing. I managed to use the "find contours" feature of the OpenCV library in a progressive way, in this code, I apply the effect on a picture I have saved on my pc. Now I want to save a frame from the webcam and apply the effect, everything on processing. How can I add the code? Please help! Thank you very much to everybody who would help me out

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();                             // cerca di nuovo i contorni e reimposta le variabili per la visualizzazione
}
Sign In or Register to comment.