Files
opencv-MIRROR/modules/video
Pu Cheng 0c948e3684 video: make clear() reset the MOG2 and KNN background models (#30992)
* video: make clear() reset the MOG2 and KNN background models

cv::Algorithm::clear() is documented to clear the algorithm state, but
BackgroundSubtractorMOG2 and BackgroundSubtractorKNN inherit the empty
base implementation. After clear(), apply() keeps updating the old model
and reports foreground against the previous scene.

Override clear() in both implementations to drop the model, so the next
apply() initializes a new one as it does on a newly created object.

* video: clear MOG2 and KNN models without going through initialize()

clear() called initialize(Size(), 0). With OpenCL active this builds the
apply and getBackgroundImage kernels for frame type 0 and runs create()
and setTo() on zero-size UMats; the setTo() call dereferences an empty
vector in ocl::checkOptimalVectorWidth() and crashes.

Release the model buffers (Mat and UMat) instead and reset the frame size,
frame type, frame counter and KNN's update counters to their constructor
values. The algorithm parameters are kept. The next apply() initializes
the model for the actual frame, as on a new object.

Add UMat variants of the clear() regression tests so the OpenCL path is
covered when OpenCL is available.

* video: do not assign theRNG() in the clear() tests

The clear_resets_model tests reseeded theRNG() before each of the two
apply() calls they compare. That is not needed: KNN takes its update
points from theRNG() only after a frame has been processed, so the
first apply() on an empty model gives the same mask and background
image whatever the RNG state is. Leave the global RNG as the test
system set it.
2026-11-21 09:18:48 +05:30
..