Ask Your Question

hunter's profile - activity

2014-04-14 18:14:52 -0600 received badge  Student (source)
2013-03-03 03:22:03 -0600 asked a question Background subtraction problem

Hi I am working on a video processing project to detect foreground objects and my code to separate foreground and background is as follows :

#include "opencv2/core/core.hpp"
#include "opencv2/video/background_segm.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv/highgui.h"
#include <stdio.h>
#include <opencv2/imgproc/imgproc.hpp>


using namespace std;
using namespace cv;


//this is a sample for foreground detection functions
int main(int argc, const char** argv)
{


    VideoCapture cap;
    bool update_bg_model = true;

    cap.open(0);


    if( !cap.isOpened() )
    {
        printf("can not open camera or video file\n");
        return -1;
    }

    namedWindow("image", CV_WINDOW_NORMAL);
    namedWindow("foreground mask", CV_WINDOW_NORMAL);
    namedWindow("foreground image", CV_WINDOW_NORMAL);
    namedWindow("mean background image", CV_WINDOW_NORMAL);

    BackgroundSubtractorMOG2 bg_model(500,10,false);
    Mat img, fgmask, fgimg;

    for(;;)
    {
        cap >> img;

        if( img.empty() )
            break;

        if( fgimg.empty() )
          fgimg.create(img.size(), img.type());
        cv::Mat framecopy;


        //update the model
        blur(img,framecopy, Size(10,10), Point(-1,-1) );


        bg_model(framecopy, fgmask, update_bg_model ? -1 : 0);

        fgimg = Scalar::all(0);
        img.copyTo(fgimg, fgmask);

        Mat bgimg;

        bg_model.getBackgroundImage(bgimg);
        /*std::vector< std::vector<cv::Point> > contours;
        findContours(fgmask,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE,Point(0,0));
        std::vector<Rect> boundRect( contours.size() );

        for(int i=0;i<contours.size();i++)
        {
            if(contourArea(contours[i])>20)
            boundRect[i] = boundingRect( Mat(contours[i]) );
            cv::rectangle( img, boundRect[i].tl(), boundRect[i].br(), 255, 2, 8, 0 );
        }*/



        //erode(fgmask,fgmask,cv::Mat(50,50));

        imshow("image", img);
        imshow("foreground mask", fgmask);
        imshow("foreground image", fgimg);
        if(!bgimg.empty())
          imshow("mean background image", bgimg );

        char k = (char)waitKey(30);
        if( k == 27 ) break;
        if( k == ' ' )
        {
            update_bg_model = !update_bg_model;
            if(update_bg_model)
                printf("Background update is on\n");
            else
                printf("Background update is off\n");
        }
    }

    return 0;
}

and the resulting foregroundmask I got is : image description

t seems my code doesnt cover the whole object completely. Is there anyway I can improve it??

2013-03-02 10:00:51 -0600 asked a question improving background subtraction

Hi, I am working on video processing project to detect fore ground objects. Below is a part of my code which is used to separate foreground and background.

#include "opencv2/core/core.hpp"
#include "opencv2/video/background_segm.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdio.h>

using namespace std;
using namespace cv;


//this is a sample for foreground detection functions
int main(int argc, const char** argv)
{


    VideoCapture cap;
    bool update_bg_model = true;

    cap.open(0);


    if( !cap.isOpened() )
    {
        printf("can not open camera or video file\n");
        return -1;
    }

    namedWindow("image", CV_WINDOW_NORMAL);
    namedWindow("foreground mask", CV_WINDOW_NORMAL);
    namedWindow("foreground image", CV_WINDOW_NORMAL);
    namedWindow("mean background image", CV_WINDOW_NORMAL);

    BackgroundSubtractorMOG2 bg_model;
    Mat img, fgmask, fgimg;

    for(;;)
    {
        cap >> img;

        if( img.empty() )
            break;

        if( fgimg.empty() )
          fgimg.create(img.size(), img.type());

        //update the model
        bg_model(img, fgmask, update_bg_model ? -1 : 0);

        fgimg = Scalar::all(0);
        img.copyTo(fgimg, fgmask);

        Mat bgimg;
        bg_model.getBackgroundImage(bgimg);

        imshow("image", img);
        imshow("foreground mask", fgmask);
        imshow("foreground image", fgimg);
        if(!bgimg.empty())
          imshow("mean background image", bgimg );

        char k = (char)waitKey(30);
        if( k == 27 ) break;
        if( k == ' ' )
        {
            update_bg_model = !update_bg_model;
            if(update_bg_model)
                printf("Background update is on\n");
            else
                printf("Background update is off\n");
        }
    }

    return 0;
}

In the foreground mask window I am getting lot of noise along with the actual fore ground object. Also the fulll object is note recognized as foreground. I need to bound the fore ground objects with rectangles as well. Wil BoundRect() do the job if I draw contours around in the foreground mask?...Also what are the most recommended parameter to be passed while finding contours(findcontour()) and for the BoundRect function...thanks in advance

2013-03-01 08:45:03 -0600 answered a question i just included header files, but...

If you are working on Project check the project (.pro) file and make sure you have included the libraries

2013-02-26 06:07:11 -0600 asked a question Implenting background subtraction

I am working on project that needs to identify humans completely or partially. I tried using various haar cascade Hod cascades etc but these dont really seem to have the expected results with my project. The thing I want to implement is same as in this video : https://www.youtube.com/watch?v=KRKKektCcok

I have a static background and hence the detection of the foreground will be much more easy with that. I need some clues to start with for implementing something as in the above video.