Ask Your Question
0

How to tracking blob per pixel? (blob using findcontours)

asked 2013-09-07 23:45:20 -0500

Shaban gravatar image

Hi all,

There're my code below, and there's a place where I need to tracking a blob per pixel.

Can you tell my how to do that?

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::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;               

    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){ 

                  //tracking blob here!

        }

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

I'll appreciate any help here... Thanks :)

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-12-03 08:48:43 -0500

Shaban gravatar image

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::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;               

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;

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

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

            for(int cpos = 0; cpos < contours[cnum].size(); cpos++){
                              cv::Point3_ <uchar>* p = frame.ptr<cv::Point3_ <uchar> >(contours[cnum][cpos].y, contours[cnum][cpos].x); //tracking blob per pixel here :D
                            }
             }

    }

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

}

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

Stats

Asked: 2013-09-07 23:45:20 -0500

Seen: 473 times

Last updated: Dec 03 '13