How can I make openCV Backgroundsubtraction KNN al

2019-06-24 02:10发布

I am trying to substract this building brick. orange building brick .

For that I am using the KNN algorithm provided by opencv 3.0. To initialize the background model I am using 40 frames without the brick.

All in all it works pretty well. (Brick with Shadow) enter image description here

The only problem is that the algorithm starts loosing the brick around Frame 58

(Image shows frame 62)

enter image description here

After frame 64 I get only black images. I know this wouldn't happen if the brick would move, but unfortunatly there are long sequences where it doesn`t.

Does somebody know a solution to this?

PS: I tried playing around with the history Paramer of

cv::createBackgroundSubtractorKNN(int history,double Threshold, bool detectShadows= true)

But there is no difference between history = 500 or history = 500000

1条回答
仙女界的扛把子
2楼-- · 2019-06-24 02:46

A easy but slow solution is to reinitialize the background model every five frames.

for (size_t i = 0; i < imageList.size(); i++){
    if (i % 5 == 0){
        for (auto& it : backgroundList){

            string nextFrameFilename(it.string());
            frame = cv::imread(nextFrameFilename);
            pMOG->apply(frame, fgMaskMOG2);
            imshow("Frame", frame);
            imshow("FG Mask MOG 2", fgMaskMOG2);
            keyboard = cv::waitKey(30);
        }
    }
}
查看更多
登录 后发表回答