Ask Your Question
0

How to take "cv::Mat frame" inside contours?

asked 2013-12-03 09:10:20 -0600

Shaban gravatar image

Hi I want to tracking human using detectMultiScale inside contours, how to do that?

This is my code, loot at the last line:

    cv::Mat frame;
    cv::Mat blurred;
    cv::Mat fg;     
    cv::Mat bgmodel;
    cv::Mat threshfg;
    cv::Mat blob;
    cv::Mat blobarray[10000];
    int pixblob = 0;
    int tot_bgr = 0;
    int tot_ex_bgr = 0;
    int green0 = 0;
    int green1 = 0;
    int green2 = 0;
    int green3 = 0;

    cv::namedWindow("Frame");   
    cv::namedWindow("Background Model");
    cv::namedWindow("Blob");

    cv::VideoCapture cap("campus.avi"); 

    cv::BackgroundSubtractorMOG2 bgs;                       
        bgs.nmixtures = 3;
        bgs.history = 500;
        bgs.bShadowDetection = true;                            
        bgs.nShadowDetection = 0;                               
        bgs.fTau = 0.25;                                        

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

    cv::CascadeClassifier human;
    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::erode(fg,fg,cv::Mat(),cv::Point(-1,-1),1);                         
        cv::dilate(fg,fg,cv::Mat(),cv::Point(-1,-1),3); 

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

        cv::findContours(threshfg,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE);
        cv::cvtColor(threshfg,blob,CV_GRAY2RGB);
        cv::drawContours(blob,contours,-1,cv::Scalar(255,255,255),CV_FILLED,8);
        blob.copyTo(blobarray[(int)cap.get(CV_CAP_PROP_POS_FRAMES)]);

        int cmin = 20; 
        int cmax = 1000;
        bool FOD1 = true;
        bool FOD2 = true;
        std::vector<cv::Rect> rects;

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

            if(contours[cnum].size() > cmin && contours[cnum].size() < cmax){       

                human.detectMultiScale(???, rects);

What should I replace ??? with? I wanna take Mat from frame inside contours. I'll appreciate any help here, thanks. :)

NB: sorry for my english. LOL

edit retag flag offensive close merge delete

Comments

??? == frame( boundingRect(contours[cnum] ) )

berak gravatar imageberak ( 2013-12-03 10:09:06 -0600 )edit

Ok! but I can't draw a rectangle around the detected human in orginal frame when using "frame(boundingRect(contours[cnum]))"

Shaban gravatar imageShaban ( 2013-12-03 10:21:30 -0600 )edit

cv::rectangle(frame, contours[cnum], ???, cv::Scalar(255, 0, 0)); and now can you help me to replace "???" with?

Shaban gravatar imageShaban ( 2013-12-03 11:04:28 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-12-04 18:19:54 -0600

Shaban gravatar image

Solved!

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

            if(contours[cnum].size() > cmin && contours[cnum].size() < cmax){       


                human.detectMultiScale(frame(cv::boundingRect(contours[cnum])),rects);

                if(rects.size() > 0){
                            cv::Rect r0 = cv::boundingRect(contours[cnum]);
                            cv::rectangle(frame, 
                                    r0,
                                    cv::Scalar(255, 0, 0));

                            cv::putText(frame,
                                "HUMAN",
                                cv::Point(r0.x + r0.width / 2, r0.y + r0.height / 2),
                                cv::FONT_HERSHEY_SIMPLEX,
                                0.5,
                                cv::Scalar(0,0,255),
                                2,
                                8);

                }
            }
edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-12-03 09:10:20 -0600

Seen: 530 times

Last updated: Dec 04 '13