Accurate and efficient localization of patterns from noisy images is very crucial in automated visual inspection. In this paper we present an accurate, efficient and robust algorithm for 2D pattern localization based on a modified optical flow constraint, which is derived from the first-order Taylor series approximation of the generalized brightness assumption. Our algorithm allows for large global illumination changes since this factor is explicitly modeled on the generalized brightness assumption. The proposed pattern localization algorithm is based on an energy minimization approach, with the energy function being defined as a weighted sum of the modified optical flow constraints at selected locations with reliable constraints. This location selection is facilitated by a reliability measure given in this paper. The energy minimization is accomplished via an efficient Newton-like iterative algorithm, which has been proved to be reliable for precise localization problems with a rough initial guess from our experiments. Experimental results on some real images and the corresponding simulated images are given to demonstrate the accuracy, efficiency and robustness of our algorithm.