Drawcontour on Webcam stream

asked 2014-03-05 02:51:55 -0500

orochi gravatar image

updated 2014-03-05 03:55:16 -0500

berak gravatar image

Hi all, I need some help. I create face detection through a webcam. So far i manage to draw an ellipse circle on detected face, realtime. then i want to draw contour of the face, i use findCountours and drawContours, no errors in my code, but it wont draw the contours.

here snippet of my code

 import java.util.ArrayList;
import java.util.List;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;

class FaceDetector {  
    private CascadeClassifier face_cascade;  
    // Create a constructor method  
    public FaceDetector(){  
         face_cascade=new CascadeClassifier("./cascades/haarcascade_frontalface_alt.xml");  
         if(face_cascade.empty())  
         {  
              System.out.println("Error loading Cascade Classifier");  
               return;  
         }  
         else  
         {  
                    System.out.println("Face classifier loaded up");  
         }  
    }  
    public Mat detect(Mat inputframe){  
         Mat mRgba=new Mat();  
         Mat mGrey=new Mat(); 
         MatOfRect faces = new MatOfRect();  
         inputframe.copyTo(mRgba);  
         inputframe.copyTo(mGrey);  
         Imgproc.cvtColor( mRgba, mGrey, Imgproc.COLOR_BGR2GRAY);  
         Imgproc.equalizeHist( mGrey, mGrey );  
         face_cascade.detectMultiScale(mGrey, faces);  
         System.out.println(String.format("Detected %s faces", faces.toArray().length));  

         List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); 

         //find the contours
         Imgproc.findContours(mGrey, contours, new Mat(), Imgproc.RETR_LIST,Imgproc.CHAIN_APPROX_SIMPLE);


         for(Rect rect:faces.toArray())  
         {  
              Point center= new Point(rect.x + rect.width*0.5, rect.y + rect.height*0.5 );  
              Core.ellipse( mRgba, center, new Size( rect.width*0.5, rect.height*0.5), 0, 0, 360, new Scalar( 255, 0, 255 ), 4, 8, 0 ); 

              //draw the contours
              Imgproc.drawContours(mRgba, contours, 1, new Scalar(0,0,255));
         }  
         return mRgba;  
    }  
}

am i doing it correctly?

edit retag flag offensive close merge delete

Comments

  • you need a binary img for findContours ( threshold, inRange, compare,Canny, etc )
  • it probably works more like you expect, if you put the findContours code inside the rects loop, and use the submat() for the rect.
berak gravatar imageberak ( 2014-03-05 04:01:20 -0500 )edit

Hi @berak is this correct?

      List&lt;MatOfPoint&gt; contours = new ArrayList&lt;MatOfPoint&gt;(); 
     Imgproc.threshold(mGrey, mTreshold, 0, 255, Imgproc.THRESH_BINARY);

     for(Rect rect:faces.toArray())  
     {  
          Point center= new Point(rect.x + rect.width*0.5, rect.y + rect.height*0.5 );  
          Core.ellipse( mRgba, center, new Size( rect.width*0.5, rect.height*0.5), 0, 0, 360, new Scalar( 255, 0, 255 ), 4, 8, 0 ); 
        //find the contours
          Imgproc.findContours(mTreshold, contours, new Mat(), Imgproc.RETR_LIST,Imgproc.CHAIN_APPROX_SIMPLE);
          //draw the contours
          Imgproc.drawContours(mRgba, contours, 1, new Scalar(0,0,255));
     }  
     return mRgba;

but still no contour

orochi gravatar imageorochi ( 2014-03-10 03:58:21 -0500 )edit