Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Blob Segmentation from Extracted Foreground

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. :)