Ask Your Question

Is there a way get original foreground object colors on foreground substraction?

asked 2018-02-12 10:19:33 -0500

my-lord gravatar image

updated 2018-02-12 13:48:17 -0500

Hello guys.I am using BackgroundSubtractorMOG2() to extract foreground of video.I am wondering about "how to get original colors of the image and set it foreground".I can extract objects with original colors not threshold.

Foreground Extraction Code:

let video = document.getElementById('videoInput');
let cap = new cv.VideoCapture(video);

let frame = new cv.Mat(video.height, video.width, cv.CV_8UC4);
let fgmask = new cv.Mat(video.height, video.width, cv.CV_8UC1);
let fgbg = new cv.BackgroundSubtractorMOG2(500, 16, true);

    const FPS = 30;
    function processVideo() {
        try {
            if (!streaming) {
                // clean and stop.
                frame.delete(); fgmask.delete(); fgbg.delete();
            let begin =;
            // start processing.
            fgbg.apply(frame, fgmask);
            cv.imshow('canvasOutput', fgmask);
            // schedule the next one.
            let delay = 1000/FPS - ( - begin);
            setTimeout(processVideo, delay);
        } catch (err) {

    // schedule the first one.
    setTimeout(processVideo, 0);

Original image

Original image

My output

My output

I want this

I want this

edit retag flag offensive close merge delete


Do you want third image ?To get it copy image using fgmask in c++ something like frame.copyTo(dst,fgmask)

LBerger gravatar imageLBerger ( 2018-02-12 11:17:02 -0500 )edit

yes.How can i do this OpencCV.js?I cannot find this feature on OPENCV.js

my-lord gravatar imagemy-lord ( 2018-02-12 13:47:35 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-02-12 14:02:44 -0500

LBerger gravatar image

Just read doc :

How to copy Mat

There are 2 ways to copy a Mat:

// 1. Clone

let dst = src.clone();

// 2. CopyTo(only entries indicated in the mask are copied)

src.copyTo(dst, mask);
edit flag offensive delete link more


Thank you.I implement copyTo() my code and my last output: But there is a little gaps on my output.How to fill these gaps?

my-lord gravatar imagemy-lord ( 2018-02-13 02:19:34 -0500 )edit

Gaps? Hole? you can use dilate on mask image to fill hole

LBerger gravatar imageLBerger ( 2018-02-13 02:58:53 -0500 )edit

A little gaps.You can see gaps output video

Input: Output::

my-lord gravatar imagemy-lord ( 2018-02-13 06:01:44 -0500 )edit

Gap: if you mean that background is visible when you move box : you cannot solve this problem with mog2. mog2 detects change in pixel value and does not detect object

LBerger gravatar imageLBerger ( 2018-02-13 06:41:05 -0500 )edit

Thanks.I can see BackgroundSubtractorMOG() have better results on OPENCV C++.

But i can't find BackgroundSubtractorMOG() on opencv.js documentation. When i trying to run cv.BackgroundSubtractorMOG() I get this error: TypeError: cv.BackgroundSubtractorMOG is not a constructor

my-lord gravatar imagemy-lord ( 2018-02-13 06:59:05 -0500 )edit

Don't hope better results using BackgroundSubtractorMOG

LBerger gravatar imageLBerger ( 2018-02-13 07:04:24 -0500 )edit

BackgroundSubstarctorMOG2 detect shadows and doing background visible because of shadows.BackgroundSubstarctorMOG don't detect shadow pixel.I think BackgroundSubstarctorMOG is better than BackgroundSubstarctorMOG2 in this case.

my-lord gravatar imagemy-lord ( 2018-02-13 07:16:55 -0500 )edit

You can disable shadow detection in BackgroundMOG2 (C++ or python)

LBerger gravatar imageLBerger ( 2018-02-13 07:36:56 -0500 )edit

Thank you.I have this question in my mind "Why BackgroundMOG2?Is BackgroundMOG2 better than BackgroundMOG?" And What is the best way to extract human body and objects?

my-lord gravatar imagemy-lord ( 2018-02-13 09:19:38 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-02-12 10:19:33 -0500

Seen: 68 times

Last updated: Feb 12