我如何填补空白并改善前景提取?

时间:2018-02-15 14:08:48

标签: opencv

我想在opencv.js项目中改进我的前景提取。我从静态背景中提取移动前景。我的输出前景有间隙,有点吵。如何用原始前景对象颜色填充这个空白?

输入

Input

输出

Output

这是我的代码:

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, false);

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);

        frame.copyTo(fgmask, 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);

1 个答案:

答案 0 :(得分:1)

对最终图像应用侵蚀然后透析,调整内核大小和迭代次数以获得最佳结果

kernel = np.ones((5,5),np.uint8)

erosion = cv.erode(img,kernel,iterations = 1)

dilation = cv.dilate(img,kernel,iterations = 1)