How can I make openCV Backgroundsubtraction KNN al

2019-06-24 02:42发布

问题:

I am trying to substract this 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)

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

(Image shows frame 62)

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:

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