从静态背景中提取人体的最佳方法是什么?

时间:2018-02-13 17:46:06

标签: algorithm opencv background-subtraction

我已经使用了各种各样的技术来提取人体。有很多方法可以检测人体并在视频中提取这些物体,但每种技术都有其自身的缺点。我如何才能最好地进行人体提取?如果有一个更好的算法请在下面评论。

算法1-)我正在使用Backgroundsubstraction方法进行人体提取。当涉及到检测运动物体时,这种算法运行良好,但如果身体不移动,则此算法不起作用。

例如,如果我们正在观看任何人进入公园,那么这个人就是一个新的前景对象。但它应该永远保持前景吗?在没有前景对象的区域(前景人不移动并退出前景但对象仍在那里),我可以继续更新我们的背景模型。如何让这个不动的人保持提取前景?

1-Apply the BackgroundsubstractorMOG2 to frame

2-Copy colors to the frame

算法2-)我正在使用haarcascades来提取人体。这个算法运行在以下5个步骤。这些算法不能很好地工作,因为有时候haar级联不能检测到物体。

0-Detect human bodies with haar cascades

1-Create a mask with the rectangular coordinates

2-Mask out the object(rectengular) region

3-Edge detection using canny

4-Find contours.(Human face contours)

5-Extract human body by contours

算法1代码:

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

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

Algorithm2代码:

let src = cv.imread('canvasInput');
let gray = new cv.Mat();
cv.cvtColor(src, gray, cv.COLOR_RGBA2GRAY, 0);
let faces = new cv.RectVector();
let poly=new cv.MatVector();
let faceCascade = new cv.CascadeClassifier();
faceCascade.load('haarcascade_frontalface_default.xml');
let msize = new cv.Size(0, 0);
faceCascade.detectMultiScale(gray, faces, 1.1, 3, 0, msize, msize);


//1-Create a mask with the rectangular coordinates
let rect = new cv.Rect(faces.get(0).x, faces.get(0).y,faces.get(0).width,faces.get(0).height);

//2-Mask out
dst = src.roi(rect);

//3-Edge detection using canny 
let cannyoutput=new cv.Mat();
cv.Canny(dst, cannyoutput, 0, 100, 3, true);

//4-Find contours
let contours = new cv.MatVector();
let hierarchy = new cv.Mat(); 
cv.findContours(cannyoutput,contours, hierarchy, cv.RETR_CCOMP, cv.CHAIN_APPROX_NONE);

//cv.drawContours(); 


cv.imshow('canvasOutput', cannyoutput);
src.delete(); gray.delete(); faceCascade.delete();
faces.delete(); 
contours.delete(); hierarchy.delete();

0 个答案:

没有答案