Generalize background subtraction function.