Ask Your Question
1

How to detect human using findcontours based on the human shape?

asked 2013-06-10 21:24:58 -0600

Shaban gravatar image

hi guys, I wanna ask how to detecting humans or pedestrians on blob (findcontours)? I've try to learn how to detecting any object on the frame using findcontours() like this:

#include"stdafx.h"
#include<vector>
#include<iostream>
#include<opencv2/opencv.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>

int main(int argc, char *argv[])
{
    cv::Mat frame;                                              
    cv::Mat fg;     
    cv::Mat blurred;
    cv::Mat thresholded;
    cv::Mat thresholded2;
    cv::Mat result;
    cv::Mat bgmodel;                                            
    cv::namedWindow("Frame");   
    cv::namedWindow("Background Model"
        //,CV_WINDOW_NORMAL
        );
    //cv::resizeWindow("Background Model",400,300);
    cv::namedWindow("Blob"
        //,CV_WINDOW_NORMAL
        );
    //cv::resizeWindow("Blob",400,300);
    cv::VideoCapture cap("campus3.avi");    

    cv::BackgroundSubtractorMOG2 bgs;                           

        bgs.nmixtures = 3;
        bgs.history = 1000;
        bgs.varThresholdGen = 15;
        bgs.bShadowDetection = true;                            
        bgs.nShadowDetection = 0;                               
        bgs.fTau = 0.5;                                         

    std::vector<std::vector<cv::Point>> contours;               

    for(;;)
    {
        cap >> frame;                                           

        cv::GaussianBlur(frame,blurred,cv::Size(3,3),0,0,cv::BORDER_DEFAULT);

        bgs.operator()(blurred,fg);                         
        bgs.getBackgroundImage(bgmodel);                                

        cv::threshold(fg,thresholded,70.0f,255,CV_THRESH_BINARY);
        cv::threshold(fg,thresholded2,70.0f,255,CV_THRESH_BINARY);

        cv::Mat elementCLOSE(5,5,CV_8U,cv::Scalar(1));
        cv::morphologyEx(thresholded,thresholded,cv::MORPH_CLOSE,elementCLOSE);
        cv::morphologyEx(thresholded2,thresholded2,cv::MORPH_CLOSE,elementCLOSE);

        cv::findContours(thresholded,contours,CV_RETR_CCOMP,CV_CHAIN_APPROX_SIMPLE);
        cv::cvtColor(thresholded2,result,CV_GRAY2RGB);

        int cmin = 50; 
        int cmax = 1000;

        std::vector<std::vector<cv::Point>>::iterator itc=contours.begin();

        while (itc!=contours.end()) {   

                if (itc->size() > cmin && itc->size() < cmax){ 

                        std::vector<cv::Point> pts = *itc;
                        cv::Mat pointsMatrix = cv::Mat(pts);
                        cv::Scalar color( 0, 255, 0 );

                        cv::Rect r0= cv::boundingRect(pointsMatrix);
                        cv::rectangle(frame,r0,color,2);

                        ++itc;
                    }else{++itc;}
        }

        cv::imshow("Frame",frame);
        cv::imshow("Background Model",bgmodel);
        cv::imshow("Blob",result);
        if(cv::waitKey(30) >= 0) break;
    }
    return 0;
}

and now I wanna know how to detect humans? am I need to use hog? or haar? if yes I need to use them, how to use them? any tutorials to learn how to use it? because I'm so curious! and it's so much fun when I learn OpenCV! so addictive! :))

anyway I'll appreciate any help here, thanks. :)

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
0

answered 2013-06-24 23:31:21 -0600

Shaban gravatar image

updated 2013-06-24 23:32:41 -0600

Now I'm using CascadeClassifier, and yeah! I can detect human now!

This is my full code:

#include"stdafx.h"
#include<vector>
#include<iostream>
#include<opencv2/opencv.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/objdetect/objdetect.hpp>

int main(int argc, char *argv[])
{
    cv::Mat frame;                                              
    cv::Mat fg;     
    cv::Mat blurred;
    cv::Mat thresholded;
    cv::Mat gray;
    cv::Mat blob;
    cv::Mat bgmodel;                                            
    cv::namedWindow("Frame");   
    cv::namedWindow("Background Model");
    cv::namedWindow("Blob");
    cv::VideoCapture cap("campus3.avi");    

    cv::BackgroundSubtractorMOG2 bgs;      

    bgs.nmixtures = 3;
    bgs.history = 1000;
    bgs.varThresholdGen = 15;
    bgs.bShadowDetection = true;                            
    bgs.nShadowDetection = 0;                               
    bgs.fTau = 0.5;                                         

std::vector<std::vector<cv::Point>> contours;               

cv::CascadeClassifier body;
assert(human.load("hogcascade_pedestrians.xml"));

for(;;)
{
    cap >> frame;                                           

    cv::GaussianBlur(frame,blurred,cv::Size(3,3),0,0,cv::BORDER_DEFAULT);

    bgs.operator()(blurred,fg);                         
    bgs.getBackgroundImage(bgmodel);                                

    cv::threshold(fg,thresholded,70.0f,255,CV_THRESH_BINARY);

    cv::Mat elementCLOSE(5,5,CV_8U,cv::Scalar(255,255,255));
    cv::morphologyEx(thresholded,thresholded,cv::MORPH_CLOSE,elementCLOSE);

    cv::findContours(thresholded,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE);
    cv::cvtColor(thresholded,blob,CV_GRAY2RGB);
    cv::drawContours(blob,contours,-1,cv::Scalar(1),CV_FILLED,8);

    cv::cvtColor(frame,gray,CV_RGB2GRAY);
    cv::equalizeHist(gray, gray);

    int cmin = 50; 
    int cmax = 1000;
    std::vector<cv::Rect> rects;
    std::vector<std::vector<cv::Point>>::iterator itc=contours.begin();

    while (itc!=contours.end()) {   

            if (itc->size() > cmin && itc->size() < cmax){ 

                    human.detectMultiScale(gray, rects);
                    for (unsigned int i=0;i<rects.size();i++) {
                        cv::rectangle(frame, cv::Point(rects[i].x, rects[i].y),
                        cv::Point(rects[i].x+rects[i].width, rects[i].y+rects[i].height),
                        cv::Scalar(0, 255, 0));
                     }

                    ++itc;
                }else{++itc;}
    }

    cv::imshow("Frame",frame);
    cv::imshow("Background Model",bgmodel);
    cv::imshow("Blob",blob);
    if(cv::waitKey(30) >= 0) break;
}
    return 0;
}
edit flag offensive delete link more

Comments

@Shaban , can you post some pictures to show your results? You can upload pictures for free to Imgur, then link them here.

Will Stewart gravatar imageWill Stewart ( 2014-02-20 09:06:32 -0600 )edit
1

answered 2015-11-19 04:54:24 -0600

updated 2015-11-19 05:04:36 -0600

I tried this CPP code and adopt it to Java side as below. I changed some of the lines because no need of them. So, the last state of it with Java goes here. I used *.mp4 file but i resize the frame before processing because more than a 640x480 resolution, processing is too slow. I prefer to dynamic resize the frame to 320x240 or max. 640x480 before processing :

// Fields...

 Mat fg;     
 Mat blurred;
 Mat thresholded;
 Mat gray;
 Mat blob;
 Mat bgmodel;                                            

 BackgroundSubtractorMOG2 bgs;      

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

 CascadeClassifier body;

// Initializer part (you may put it to a private method and call it from constructor)...

     bgs = new BackgroundSubtractorMOG2();
     bgs.setInt("nmixtures", 3);
     bgs.setInt("history", 1000);
     bgs.setInt("varThresholdGen", 15);
     //bgs.setBool("bShadowDetection", true);   // This feautee does not exist on OpenCV-2.4.11
     bgs.setInt("nShadowDetection",0);                               
     bgs.setDouble("fTau", 0.5);                                         

     body=new CascadeClassifier("mapbase/hogcascade_pedestrians.xml"); // Standart sample pedestrian trained data file
      if(body.empty())  
      {  
           System.out.println("--(!)Error loading hogcascade file\n");  
            return;  
      }  
      else  
      {  
                 System.out.println("Pedestrian classifier loaded up");  
      }

// Frame reader part...

VideoCapture cam = new VideoCapture("D:\\Sample_Videos\\pedestrian-much.mp4"); // int 0 : Webcam, String : rtsp://IP:PORT/PATH for ip camera etc.
        if(cam.isOpened())  
          {  
           Thread.sleep(500); /// This one-time delay allows the Webcam to initialize itself  
           while( true )  
           {  
             cam.read(cam_image);  
             if(!cam_image.empty())  
              {            
                   // Thread.sleep(50); // If too much computation, you may give a time to CPU        
                   Imgproc.resize(cam_image, resizeImage, new Size(320, 240));
                   //Apply the classifier to the captured image  
                   renderImage=faceDetector.detect(resizeImage);
                   //Display the image  
                   BufferedImage img = facePanel.matToBufferedImage(renderImage);
                   facePanel.repaint();
                   renderImage.release();
              }  
              else  
              {   
                   System.out.println(" --(!) No captured frame from video source!");
                   break;   
              }  
             }  
            }
           cam.release(); //release the webcam   }

// Detect method which is called in while read frame loop... public Mat detect(Mat inputframe){

        Imgproc.resize(webcam_image, resizeImage, new Size(320, 240));
        Mat mRgba=new Mat();
        inputframe.copyTo(mRgba); // The frame read from video source (camera, *.mp4, *.avi file etc)
        blurred = new Mat();
        fg = blurred.clone();
        thresholded = fg.clone();
        blob = thresholded.clone();
        gray = blob.clone();

        Imgproc.cvtColor(mRgba,gray,Imgproc.COLOR_BGR2GRAY);  // Before processing we convert to gray
        Imgproc.GaussianBlur(gray,blurred,new Size(3,3),0,0,Imgproc.BORDER_DEFAULT); // Avoid from unnecessary pixels

             bgs.apply(blurred,fg); // Extract background from scene                             

            Imgproc.threshold(fg,thresholded,70.0f,255,Imgproc.THRESH_BINARY); // Apply max and min threshold

        Mat elementCLOSE = new Mat(5,5,CvType.CV_8U,new Scalar(255,255,255));
        Imgproc.morphologyEx(thresholded,thresholded,Imgproc.MORPH_CLOSE,elementCLOSE); // Do a body figure analysis (for object scene change optimization)

        Mat v = new Mat();
        Imgproc.findContours(thresholded,contours,v, Imgproc.RETR_EXTERNAL,Imgproc.CHAIN_APPROX_SIMPLE); // Find the changes of scene (the moving parts)

        Imgproc.equalizeHist(gray, gray); // Balance the contrast to gain proper result

        int cmin = 500; 
        int cmax = 80000;
        MatOfRect rects = new MatOfRect();
        if (contours != null && !contours.isEmpty()) {
            //if (contours.size() >= 50) { // You may use this control and a threshold value if too much contour you have
                //Imgproc.drawContours(mRgba,contours,-1,new Scalar(0, 0, 255),2); // If you want you ...
(more)
edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-06-10 21:24:58 -0600

Seen: 9,474 times

Last updated: Nov 19 '15