Ask Your Question
1

Blob Segmentation from Extracted Foreground

asked 2013-05-06 01:04:42 -0600

Shaban gravatar image

I'm newbie with OpenCV + C++ + Visual Studio 2012. And now I need to learn them. I just learned how to substract the background and extract the foreground. Here's the code for background substraction/foreground extraction.

    include opencv2/opencv.hpp
    include iostream
    include vector

int main(int argc, char *argv[])
{
    cv::Mat frame;
    cv::Mat bgmodel;
    cv::Mat fg;
    cv::VideoCapture cap(0);

cv::BackgroundSubtractorMOG2 bg;
bg.nmixtures = 3;
bg.bShadowDetection = true;

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

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

for(;;)
{
    cap >> frame;
    bg.operator ()(frame,fore);
    bg.getBackgroundImage(bgmodel);

    cv::erode(fg,fg,cv::Mat());
    cv::dilate(fg,fg,cv::Mat());

    cv::findContours(fg,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
    cv::drawContours(frame,contours,-1,cv::Scalar(0,0,255),2);

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

}

and now I want to build blobs from the extracted foreground. what should I do with my code? thanks. :)

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-05-06 03:07:52 -0600

updated 2013-05-06 03:09:17 -0600

Find contours is find blobs for you, or more precisely, connected components. This code is extracted from the documentation here.

findContours( fg, contours, hierarchy,
    CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
int idx = 0;
for( ; idx >= 0; idx = hierarchy[idx][0] )
{
    Scalar color( rand()&255, rand()&255, rand()&255 );
    drawContours( frame, contours, idx, color, CV_FILLED, 8, hierarchy );
}

Otherwise, you can use the blob detector (doc) that can do it for you. Read the algorithm explain below the function. See a sample here or any other on Google...

edit flag offensive delete link more

Comments

Thanks Mat! It works! :D

Shaban gravatar imageShaban ( 2013-05-07 21:20:30 -0600 )edit

Question Tools

Stats

Asked: 2013-05-06 01:04:42 -0600

Seen: 5,642 times

Last updated: May 06 '13