# Segmenting pills from the background

I just recently started out with Image processing and taken up a course related to it in Grad school. But I already have a project to do without too much information about the subject, however I've made some steady progress. I am trying to segment the pills from their respective backgrounds. For images with contrasting backgrounds, I've been able to segment the pills using Otsu's method. With regards to images with similar backgrounds, Otsu's method doesn't quite work. I've been reading quite a few papers with regards to segmenting, but most of the papers that I've read use a manual thresholding value depending upon the kind of image. Is it possible to detect the correct thresholding value and automatically threshold an image and use techniques such as seed-growing or clustering to segment the image ?

The color space I've been using is Lab*, so I'd appreciate it if you could recommend the appropriate color space that I should be using too.

The images in question:

original 1

result 1

original 2

result 2

original 3

result 3

• What about Level Set ethods and Active Contours? You can segment the pills from background not only by intensity value (threshold), but based on the object texture. The color space used is just a coordinate system for the colors - use one that best distinguishes colors in your image for sake of segmentation. For example, if the images are in purple tones, you can use greyscale conversion with more weight put on red and blue component. Sep 5, 2012 at 13:57
• Sep 5, 2012 at 16:47
• Quentin: I don't have enough reputation to upload the photos. Sep 5, 2012 at 22:46

If you want to use the thresholding approach, you should use an adaptive thresholding method if there are big lighting variations like in the 3rd example image (dsp question here).

Also, you should experiment with colorspaces, it's easy: the script to decompose the image into different colorspaces should be no more than a few lines long, and a lot of image viewers have that option available. The best one should be easy to determine just visually. If you want to read up on colorspaces, there's anothrt good dsp question here.

Finally, you might want to try a diffrent approach. One idea would be to do a not-perfect segmentation, then an edge detection, and finally using something like Hough transform for circles which also works fine on (incomplete) circle arches. (this idea is ofcourse only applicable to round pills)

• What would be a good idea for a not-perfect segmentation ? Sep 15, 2012 at 23:42
• @Syed Looks like OpenCV Canny (edge detection) and even Hough work on gray images (no need to threshold), so you could skip the thresholding step all together. The thresholding -- segmentation depends greatly on the pictures. But, a straight answer would be: sorry, don't know. I think a non-adaptive method would be sufficient, but I couldn't suggest a specific one since I didn't do this for a while. Just do a little research on simple segmentation methods ;) Sep 16, 2012 at 17:05

Circle hough transforms from the OpenCV library are well-suited for this application. You will have to run a number of radii but the best hough response will give you the pills' boundaries and centers. Note that you would have to use generalized hough transforms to find non-circular pills. It will work even if the pills have occluded or missing edge points.

Thresholding might be a bad solution to this because in the field you might fall into situations where no threshold will separate the pill from the background, which is why an algorithm that depends on relative positions of groups of edges is superior.

To solve this problem you need to separate background and foreground. This is the solution, I propose you:

1) convert the image from Rgb to grayscale; You will obtain an image that we call I1;

2) apply a morphological filter, erosion using a large radius, eventually several times ==> you should erase the pill by erosion and obtain only the background; You will obtain a new image I2;

3) subtract I2 to I1, you will obtain the foreground i.e the pill;

4) apply another morphological filter to fill any hole in the pill that you obtained;

5) apply a morphological filter, erosion, small radius to remove any isolated pixel.

This method does not require any threshold, shape detection, color segmentation or anything else.