This paper describes an automatic technique for the segmentation, detection and quantification of bacilli and clusters present in a digital image of sputum smear samples prepared with the Ziehl-Neelsen technique. Algorithms for color space segmentation, quantification and automatic diagnosis are described. Different color spaces (RGB, HSV,YIQ, YCbCr, Lab) were considered in order to develop and algorithm both efficient (in terms of bacilli and cluster segmentation) and robust against noise and varying illumination conditions in the image. The combination of YCbCr and Lab color spaces offered the best results, with a sensitivity of 90.9% and a specificity of 100%, which represents an improvement when compared with previous works. The accuracy of the algorithm was also measured, and results show that in 85.7% of the cases the algorithm provides a correct diagnosis in terms of infection level.