Ask Your Question
5

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

asked 2013-05-28 22:44:26 -0600

Shaban gravatar image

updated 2020-10-28 03:04:39 -0600

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 ...
(more)
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
4

answered 2013-06-13 04:09:41 -0600

Shaban gravatar image

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

edit flag offensive delete link more

Comments

Please, can you explain how your code is working, I do not know c++? Thank you!!

brij_mal gravatar imagebrij_mal ( 2015-09-16 09:37:01 -0600 )edit

Question Tools

Stats

Asked: 2013-05-28 22:44:26 -0600

Seen: 5,656 times

Last updated: Jun 13 '13