How to capture a frame and find contours of it?

edited January 2017 in Kinect

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();                           
}
Tagged:

Answers

  • I'd say you just need to change the src variable to store the camera capture.

  • edited January 2017

    should I put this

    import processing.video.*; 
    Capture cam; 
    
    void setup() { 
      size(200, 200); 
      cam = new Capture(this);
      cam.start(); 
    } 
    
    void draw() { 
      if (cam.available()) { 
        // Reads the new frame
        cam.read(); 
      } 
      image(cam, 0, 0); 
    } 
    

    and then src = capture cam ? but in change of what? sorry for my bad english, I'm from italy :(

  • First, I think you can remove the mousePressed function for now - add it later.
    Then, add resetContours to the end (or start) of draw.
    Finally, put src = cam at the end of setup, and include the code you just posted in your previous comment.

  • Help! It doesn't work, it says null pointer error, what is wrong?

       import processing.video.*; 
    Capture cam;
    
    
     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 = cam;
          cam = new Capture(this);
          cam.start();
        }
    
    
        ////////////////////////////////////////////////////////////////////////////////
    
        void resetContours() 
        {
          opencv.loadImage( src );           
    
          opencv.gray();                      
          opencv.blur(5);                     
          opencv.threshold(60);               
    
          // tutte le operazioni possibili sono elencate e descritte in:
          // <a href="http://atduskgreg.github.io/opencv-processing/reference/gab/opencv/OpenCV.html" target="_blank" rel="nofollow">http://atduskgreg.github.io/opencv-processing/reference/gab/opencv/OpenCV.html</a>; 
          // [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;
          }
    
            resetContours();                           
        }
    
  • Line 26 is not good. The Capture class extends from PImage. So any reference to src in your original code can be replaced with cam. You just need to make sure your cam object is properly init.

    Kf

  • edited January 2017

    Try putting line 26 after line 28.
    If that doesn't work, I'll give a more proper solution, one that is neither as abnormal as mine nor as difficult to reuse like kfrajer's.

  • edited January 2017

    Hey guys, here I am again, I tried as you suggested, and it says at line 80: NullPointerException.

    for (int c=0; c<contours.size(); ++c) {

    I'm thinking it would change a lot of the original code (the one that finds contours over an image from my pc). Should I re do everything from zero?

       import processing.video.*; 
    Capture cam;
    
    
     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 = cam;
          cam = new Capture(this);
          cam.start();
            src = cam;
        }
    
    
        ////////////////////////////////////////////////////////////////////////////////
    
        void resetContours() 
        {
          opencv.loadImage( src );           
    
          opencv.gray();                      
          opencv.blur(5);                     
          opencv.threshold(60);               
    
          // tutte le operazioni possibili sono elencate e descritte in:
          // <a href="<a href="http://atduskgreg.github.io/opencv-processing/reference/gab/opencv/OpenCV.html" target="_blank" rel="nofollow">http://atduskgreg.github.io/opencv-processing/reference/gab/opencv/OpenCV.html</a>;" target="_blank" rel="nofollow"><a href="http://atduskgreg.github.io/opencv-processing/reference/gab/opencv/OpenCV.html</a>;" target="_blank" rel="nofollow">http://atduskgreg.github.io/opencv-processing/reference/gab/opencv/OpenCV.html</a></a>; 
          // [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;
          }
    
            resetContours();                           
        }
    
  • Yeah, I think you should redo everything.

    Create a function calculateContours(PImage src) that returns an ArrayList containing all the contours of the image. Shift everything else outside, maybe into draw, maybe somewhere else. Start small and build up.

Sign In or Register to comment.