Search code examples
opencvbackgroundforegroundsubtraction

How to generate foreground-only image given the foreground silhouette in OpenCV?


I did the background substraction part for my foreground extraction task. Now that i have silhouette representing foreground in white and background in black, I dont know how to make a foreground image containing only the pixels values (from the original frame). I am using Opencv 2.3 and i am using Mat for storing images. Any ideas? Thanks in advance..


Solution

  • You can do as follow:

    Mat image; // the original image
    
    Mat foreground_bw; // the foreground in black and white
    background_subtractor ( image, foreground_bw, -1.0 );
    
    // getting the corresponding pixel values for the foreground.
    Mat foreground = Mat::zeros ( image.rows, image.cols, image.type() );
    image.copyTo ( foreground, foreground_bw );