Ask Your Question
0

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

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

my-lord gravatar image

updated 2020-11-04 09:56:45 -0600

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();
                return;
            }
            let begin = Date.now();
            // start processing.
            cap.read(frame);
            fgbg.apply(frame, fgmask);
            cv.imshow('canvasOutput', fgmask);
            // schedule the next one.
            let delay = 1000/FPS - (Date.now() - begin);
            setTimeout(processVideo, delay);
        } catch (err) {
            utils.printError(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

Comments

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 -0600 )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 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

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

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

Comments

Thank you.I implement copyTo() my code and my last output: http://i66.tinypic.com/f3b9g1.png 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 -0600 )edit
1

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

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

A little gaps.You can see gaps output video

Input: https://streamable.com/k8d3r Output:: https://streamable.com/dmra7

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

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 -0600 )edit

Thanks.I can see BackgroundSubtractorMOG() have better results on OPENCV C++. https://imgur.com/a/1SL3d

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 -0600 )edit
1

Don't hope better results using BackgroundSubtractorMOG

LBerger gravatar imageLBerger ( 2018-02-13 07:04:24 -0600 )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. https://docs.opencv.org/java/2.4.8/or...

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

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

LBerger gravatar imageLBerger ( 2018-02-13 07:36:56 -0600 )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 -0600 )edit

Can you send me the corrected code in opencv C++ to get the original foreground object colors?

svt876 gravatar imagesvt876 ( 2018-10-24 05:06:06 -0600 )edit

Question Tools

1 follower

Stats

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

Seen: 1,592 times

Last updated: Oct 24 '18