In background subtraction, it is challenging to detect foreground objects in the presence of dynamic background motions. The paper proposes two new algorithms to this problem by improving the codebook model with the incorporation of the spatial and temporal context of each pixel. The spatial context involves the local spatial dependency between neighboring pixels, and the temporal context involves the preceding detection result. Only the spatial context is incorporated into the first algorithm which makes the background representation more compact than the standard codebook. The second algorithm explicitly models the spatio-temporal context with a Markov random field model, thus achieving more accurate foreground detection. Extensive experiments on several dynamic scenes are conducted to compare the two proposed algorithms with each other and with the standard codebook algorithm. (C) 2009 Elsevier GmbH. All rights reserved.