Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9011039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:37:20+00:00 2026-06-16T02:37:20+00:00

I want to mask the moving objects from video. I found that OpenCV has

  • 0

I want to mask the moving objects from video.
I found that OpenCV has some built-in BackgroundSubtractors which could possibly saving my time a lot. However, according to the official reference, the function:

void BackgroundSubtractorMOG2::operator()(InputArray image, OutputArray fgmask, double learningRate=-1)

should output a mask, fgmask, but it doesn’t. The fgmask variable will contain the “contour of the mask” instead after invoking above method. That’s weird. All I want is a simple closed region filled with white color(for example) to represent the moving objects. How could I do that?

Any reply or recommendation would be very appreciate. Thanks a lot.

Here’s my code:

int main(int argc, char *argv[])
{
    cv::BackgroundSubtractorMOG2 bg = BackgroundSubtractorMOG2(30,16.0,false);
    cv::VideoCapture cap(0);
    cv::Mat frame, mask, _frame, _fmask;
    cvNamedWindow("mask", CV_WINDOW_AUTOSIZE);
    for(;;)
    {
        cap >> frame;
        bg(frame,fmask,-1);

        _frame = IplImage(frame);
        _fmask = IplImage(fmask);

        cvShowImage("mask", &_fmask);
        if(cv::waitKey(30) >= 0) break;
    }
    return 0;
}

A snapshot of the output video is:
enter image description here

p.s. My working environment is OpenCV2.4.3 on OSX 10.8 and XCode 4.5.2 with apple LLVM compiler 4.1.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T02:37:21+00:00Added an answer on June 16, 2026 at 2:37 am

    If you want to acquire the whole objects filled with white pixels in the foreground then I would ask you to tell me something about your experience.

    My question is, for the code, you mentioned above, do you get more white pixels when you generate more motion in front of your camera?

    If yes then there are two paramenters to learn about for your requirement.

    First is the History parameter. which you have configured as 30 in the constructor BackgroundSubtractorMOG2(30,16.0,false);. You can test this param by incresing, say to 300. It will maintain the motion history of the object in the foreground. So if you have moved completely from your starting location within the 300 frames then you will get whole object covered with white pixels as you want. but it will be erased gradually. So it cannot give you the 100% solution.

    The second parameter is called learning rate. In the code you mentioned bg(frame,fmask,-1); where -1 is your learning rate. you can set it to 0.0 to 1.0 and default is -1. When you set it 0, you will get what you want for the objects which are not part of the frame in the starting of the video. You can call this kind of object “foreign objects”. You will get foreign object covered with white pixels.

    Explore your testing from the information I have mentioned above and share your experience.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to generate a binary mask that has ones for all voxels inside
I want to remove or mask password from some confidential files. I am using
My application's creationComplete=onInit() has a lot of processing. Therefore I want to mask the
I want to set the Mask of MaskedtextBox from Regex expression. Such as i
I want to use the MaskedEditExtender to mask short dates. The problem is that
I have a string such as 'xxox-x' that I want to mask each line
I have credit card number which I want to mask as below: $cc =
I've seen similar questions, but haven't found workable answers. I want to mask a
I want to display a text box on my webpage which has a Date
I have multiple simple circle objects in grid of an image from which I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.