Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to getting SOLID blob for each object using findcontours?

Hi all, I wanna segementing a solid blobs for each object from extracted foreground and bounding each object with a box. But my code show many boxes bounding random blobs on 1 object, because my blob is not solid for 1 object and there're many small blobs too.

Here we go my 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>

int main(int argc, char *argv[])
{
    cv::Mat frame;                                              
    cv::Mat fg;                                                 
    cv::Mat thresholded;
    cv::Mat thresholded2;
    cv::Mat result;
    cv::Mat bgmodel;                                            
    cv::namedWindow("Frame");                                   
    cv::namedWindow("Background Model");                        
    cv::VideoCapture cap(0);                                    

    cv::BackgroundSubtractorMOG2 bgs;                           

        bgs.nmixtures = 2;                                      
        bgs.history = 60;
        bgs.varThreshold = 15;
        bgs.bShadowDetection = true;                            
        bgs.nShadowDetection = 0;                               
        bgs.fTau = 0.5;                                         

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

    for(;;)
    {
        cap >> frame;                                           

        cv::blur(frame,frame,cv::Size(10,10));

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

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

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

        cv::findContours(thresholded, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
        cv::cvtColor(thresholded2, result, CV_GRAY2RGB );

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

                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(result,r0,color,2);

        }

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

And the result here: frame

so how I can segmenting a solid blob for each object found from extracted foreground, and bounding the object on by one with the box? I'll apreciating any help here.

NB: Sorry for my bad English. :)

How to getting SOLID blob for each object using findcontours?

Hi all, I wanna segementing a solid blobs for each object from extracted foreground and bounding each object with a box. But my code show many boxes bounding random blobs on 1 object, because my blob is not solid for 1 object and there're many small blobs too.

Here we go my 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>

int main(int argc, char *argv[])
{
    cv::Mat frame;                                              
    cv::Mat fg;                                                 
    cv::Mat thresholded;
    cv::Mat thresholded2;
    cv::Mat result;
    cv::Mat bgmodel;                                            
    cv::namedWindow("Frame");                                   
    cv::namedWindow("Background Model");                        
    cv::VideoCapture cap(0);                                    

    cv::BackgroundSubtractorMOG2 bgs;                           

        bgs.nmixtures = 2;                                      
        bgs.history = 60;
        bgs.varThreshold = 15;
        bgs.bShadowDetection = true;                            
        bgs.nShadowDetection = 0;                               
        bgs.fTau = 0.5;                                         

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

    for(;;)
    {
        cap >> frame;                                           

        cv::blur(frame,frame,cv::Size(10,10));

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

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

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

        cv::findContours(thresholded, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
        cv::cvtColor(thresholded2, result, CV_GRAY2RGB );

        for (std::vector<std::vector<cv::Point> >::iterator itc=contours.begin() ; itc cv::findContours(thresholded,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
        cv::cvtColor(thresholded2,result,CV_GRAY2RGB);

        int cmin= 50; 
        int cmax= 10000;

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

        while (itc!=contours.end()) {       
                if (itc->size() < contours.end(); itc++ ){

cmin || itc->size() > cmax){
                    itc= contours.erase(itc);} else{

                        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(result,r0,color,2);

                        ++itc;
                    }
        }

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

And the result here: frame

so how I can segmenting a solid blob for each object found from extracted foreground, and bounding the object on by one with the box? I'll apreciating any help here.

NB: Sorry for my bad English. :)

How to getting a SOLID blob for each object using findcontours?

Hi all, I wanna segementing a solid blobs for each object from extracted foreground and bounding each object with a box. But my code show many boxes bounding random blobs on 1 object, because my blob is not solid for 1 object and there're many small blobs too.

Here we go my 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>

int main(int argc, char *argv[])
{
    cv::Mat frame;                                              
    cv::Mat fg;                                                 
    cv::Mat thresholded;
    cv::Mat thresholded2;
    cv::Mat result;
    cv::Mat bgmodel;                                            
    cv::namedWindow("Frame");                                   
    cv::namedWindow("Background Model");                        
    cv::VideoCapture cap(0);                                    

    cv::BackgroundSubtractorMOG2 bgs;                           

        bgs.nmixtures = 2;                                      
        bgs.history = 60;
        bgs.varThreshold = 15;
        bgs.bShadowDetection = true;                            
        bgs.nShadowDetection = 0;                               
        bgs.fTau = 0.5;                                         

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

    for(;;)
    {
        cap >> frame;                                           

        cv::blur(frame,frame,cv::Size(10,10));

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

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

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

        cv::findContours(thresholded,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
        cv::cvtColor(thresholded2,result,CV_GRAY2RGB);

        int cmin= 50; 
        int cmax= 10000;

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

        while (itc!=contours.end()) {       
                if (itc->size() < cmin || itc->size() > cmax){
                    itc= contours.erase(itc);} else{

                        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(result,r0,color,2);

                        ++itc;
                    }
        }

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

And the result here: frame

so how I can segmenting a solid blob for each object found from extracted foreground, and bounding the object on by one with the box? I'll apreciating any help here.

NB: Sorry for my bad English. :)

[RESOLVED] How to getting a SOLID blob for each object using findcontours?

Hi all, I wanna segementing a solid blobs for each object from extracted foreground and bounding each object with a box. But my code show many boxes bounding random blobs on 1 object, because my blob is not solid for 1 object and there're many small blobs too.

Here we go my 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>

int main(int argc, char *argv[])
{
    cv::Mat frame;                                              
    cv::Mat fg;                                                 
    cv::Mat thresholded;
    cv::Mat thresholded2;
    cv::Mat result;
    cv::Mat bgmodel;                                            
    cv::namedWindow("Frame");                                   
    cv::namedWindow("Background Model");                        
    cv::VideoCapture cap(0);                                    

    cv::BackgroundSubtractorMOG2 bgs;                           

        bgs.nmixtures = 2;                                      
        bgs.history = 60;
        bgs.varThreshold = 15;
        bgs.bShadowDetection = true;                            
        bgs.nShadowDetection = 0;                               
        bgs.fTau = 0.5;                                         

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

    for(;;)
    {
        cap >> frame;                                           

        cv::blur(frame,frame,cv::Size(10,10));

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

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

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

        cv::findContours(thresholded,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
        cv::cvtColor(thresholded2,result,CV_GRAY2RGB);

        int cmin= 50; 
        int cmax= 10000;

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

        while (itc!=contours.end()) {       
                if (itc->size() < cmin || itc->size() > cmax){
                    itc= contours.erase(itc);} else{

                        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(result,r0,color,2);

                        ++itc;
                    }
        }

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

And the result here: frame

so how I can segmenting a solid blob for each object found from extracted foreground, and bounding the object on by one with the box? I'll apreciating any help here.

NB: Sorry for my bad English. :)

=====

This is my edited 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>

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::VideoCapture cap(0);    

    cv::BackgroundSubtractorMOG2 bgs;                           

        bgs.nmixtures = 2;                                      
        bgs.history = 60;
        bgs.varThreshold = 15;
        bgs.bShadowDetection = true;                            
        bgs.nShadowDetection = 0;                               
        bgs.fTau = 0.5;                                         

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

    for(;;)
    {
        cap >> frame;                                           

        cv::blur(frame,blurred,cv::Size(10,10));

        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 element50(50,50,CV_8U,cv::Scalar(1));
        cv::morphologyEx(thresholded,thresholded,cv::MORPH_CLOSE,element50);
        cv::morphologyEx(thresholded2,thresholded2,cv::MORPH_CLOSE,element50);

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

        int cmin= 50; 
        int cmax= 10000;

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

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

                if (itc->size() < cmin || itc->size() > cmax){
                    itc= contours.erase(itc);} else{ 

                        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(result,r0,color,2);

                        ++itc;
                    }
        }

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

and the result here: Frame

[RESOLVED] How to getting a SOLID blob for each object using findcontours?

Hi all, I wanna segementing a solid blobs for each object from extracted foreground and bounding each object with a box. But my code show many boxes bounding random blobs on 1 object, because my blob is not solid for 1 object and there're many small blobs too.

Here we go my 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>

int main(int argc, char *argv[])
{
    cv::Mat frame;                                              
    cv::Mat fg;                                                 
    cv::Mat thresholded;
    cv::Mat thresholded2;
    cv::Mat result;
    cv::Mat bgmodel;                                            
    cv::namedWindow("Frame");                                   
    cv::namedWindow("Background Model");                        
    cv::VideoCapture cap(0);                                    

    cv::BackgroundSubtractorMOG2 bgs;                           

        bgs.nmixtures = 2;                                      
        bgs.history = 60;
        bgs.varThreshold = 15;
        bgs.bShadowDetection = true;                            
        bgs.nShadowDetection = 0;                               
        bgs.fTau = 0.5;                                         

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

    for(;;)
    {
        cap >> frame;                                           

        cv::blur(frame,frame,cv::Size(10,10));

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

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

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

        cv::findContours(thresholded,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
        cv::cvtColor(thresholded2,result,CV_GRAY2RGB);

        int cmin= 50; 
        int cmax= 10000;

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

        while (itc!=contours.end()) {       
                if (itc->size() < cmin || itc->size() > cmax){
                    itc= contours.erase(itc);} else{

                        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(result,r0,color,2);

                        ++itc;
                    }
        }

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

And the result here: frame

so how I can segmenting a solid blob for each object found from extracted foreground, and bounding the object on by one with the box? I'll apreciating any help here.

NB: Sorry for my bad English. :)

=====

This is my edited 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>

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::VideoCapture cap(0);    

    cv::BackgroundSubtractorMOG2 bgs;                           

        bgs.nmixtures = 2;                                      
        bgs.history = 60;
        bgs.varThreshold = 15;
        bgs.bShadowDetection = true;                            
        bgs.nShadowDetection = 0;                               
        bgs.fTau = 0.5;                                         

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

    for(;;)
    {
        cap >> frame;                                           

        cv::blur(frame,blurred,cv::Size(10,10));

        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 element50(50,50,CV_8U,cv::Scalar(1));
        cv::morphologyEx(thresholded,thresholded,cv::MORPH_CLOSE,element50);
        cv::morphologyEx(thresholded2,thresholded2,cv::MORPH_CLOSE,element50);

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

        int cmin= 50; 
        int cmax= 10000;

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

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

                if (itc->size() < cmin || itc->size() > cmax){
                    itc= contours.erase(itc);} else{ 

                        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(result,r0,color,2);

                        ++itc;
                    }
        }

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

and the result here: Frame

[RESOLVED] How to getting a SOLID blob for each object using findcontours?

Hi all, I wanna segementing a solid blobs for each object from extracted foreground and bounding each object with a box. But my code show many boxes bounding random blobs on 1 object, because my blob is not solid for 1 object and there're many small blobs too.

Here we go my 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>

int main(int argc, char *argv[])
{
    cv::Mat frame;                                              
    cv::Mat fg;                                                 
    cv::Mat thresholded;
    cv::Mat thresholded2;
    cv::Mat result;
    cv::Mat bgmodel;                                            
    cv::namedWindow("Frame");                                   
    cv::namedWindow("Background Model");                        
    cv::VideoCapture cap(0);                                    

    cv::BackgroundSubtractorMOG2 bgs;                           

        bgs.nmixtures = 2;                                      
        bgs.history = 60;
        bgs.varThreshold = 15;
        bgs.bShadowDetection = true;                            
        bgs.nShadowDetection = 0;                               
        bgs.fTau = 0.5;                                         

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

    for(;;)
    {
        cap >> frame;                                           

        cv::blur(frame,frame,cv::Size(10,10));

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

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

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

        cv::findContours(thresholded,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
        cv::cvtColor(thresholded2,result,CV_GRAY2RGB);

        int cmin= 50; 
        int cmax= 10000;

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

        while (itc!=contours.end()) {       
                if (itc->size() < cmin || itc->size() > cmax){
                    itc= contours.erase(itc);} else{

                        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(result,r0,color,2);

                        ++itc;
                    }
        }

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

And the result here: frame

so how I can segmenting a solid blob for each object found from extracted foreground, and bounding the object on by one with the box? I'll apreciating any help here.

NB: Sorry for my bad English. :)

=====

This is my edited 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>

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::VideoCapture cap(0);    

    cv::BackgroundSubtractorMOG2 bgs;                           

        bgs.nmixtures = 2;                                      
        bgs.history = 60;
        bgs.varThreshold = 15;
        bgs.bShadowDetection = true;                            
        bgs.nShadowDetection = 0;                               
        bgs.fTau = 0.5;                                         

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

    for(;;)
    {
        cap >> frame;                                           

        cv::blur(frame,blurred,cv::Size(10,10));

        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 element50(50,50,CV_8U,cv::Scalar(1));
        cv::morphologyEx(thresholded,thresholded,cv::MORPH_CLOSE,element50);
        cv::morphologyEx(thresholded2,thresholded2,cv::MORPH_CLOSE,element50);

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

        int cmin= 50; 
        int cmax= 10000;

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

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

                if (itc->size() < cmin || itc->size() > cmax){
                    itc= contours.erase(itc);} else{ 

                        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(result,r0,color,2);

                        ++itc;
                    }
        }

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

and the result here: Frame