Segmentation based on color K-means clustering is widely used in practice. Good introduction is in a classic Christopher M. Bishop book "Pattern Recognition and Machine Learning"
The k-means method recommended in this thread gives excellent results but is VERY slow and iterative. The Otsu method (also recommended above) does not involve scanning but is based on the histogram and assumes that the brightness distributions are Gaussian which is rarely true. An intermediate approach uses a two-way threshold of pixel values and pixel neighbor values to find the setting with the maximum conditional entropy (in other words, the boundaries that class pixels with those most like themselves and most different from neighbors across the boundary). Use Google Scholar to look up Pal & Pal 1989, Chang et al. 1994, Shi & Malik 2000, Tolliver & Miller 2006, Sharon et al, 2006. These will lead you to many other similar refs.