Background subtraction is a typical approach to foreground segmentation by comparing each new frame with a learned model of the scene background in image sequences taken from a static camera. In this paper, we propose a flexible method to estimate the background model with the finite Gaussian mixture model. A stochastic approximation procedure is used to recursively estimate the parameters of the Gaussian mixture model, and to simultaneously obtain the asymptotically optimal number of the mixture components. The experimental results show our method is efficient and effective.