We present a two-dimensional version of the classical one-dimensional Kolmogorov-Smirnov (K-S) test, extending an earlier idea due to Peacock (1983) and an implementation proposed by Fasano & Franceschini (1987). The two-dimensional K-S test is used to optimise the goodness of fit in an iterative source-detection scheme for astronomical images. The method is applied to a ROSAT/HRI x-ray image of the post core-collapse globular cluster NGC 6397 to determine the most probable source distribution in the cluster core. Comparisons to other widely-used source detection methods, and to a Chandra image of the same field, show that our iteration scheme is superior in measuring statistics-limited sources in severely crowded fields.