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

enter image description here

result 1

enter image description here

original 2

enter image description here

result 2

enter image description here

original 3

enter image description here

result 3

enter image description here

  • 7
    $\begingroup$ Could you upload an image please ? What about adaptive thresholding ? $\endgroup$ Sep 5, 2012 at 7:26
  • 3
    $\begingroup$ 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. $\endgroup$
    – Libor
    Sep 5, 2012 at 13:57
  • $\begingroup$ dsp.stackexchange.com/q/3063/29 dsp.stackexchange.com/q/2516/29 ? $\endgroup$
    – endolith
    Sep 5, 2012 at 16:47
  • $\begingroup$ Quentin: I don't have enough reputation to upload the photos. $\endgroup$
    – GamingX
    Sep 5, 2012 at 22:46
  • $\begingroup$ @Syed Upload them somewhere public and add the links to the question if you please. $\endgroup$
    – Maurits
    Sep 6, 2012 at 13:23

3 Answers 3


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)

  • $\begingroup$ What would be a good idea for a not-perfect segmentation ? $\endgroup$
    – GamingX
    Sep 15, 2012 at 23:42
  • $\begingroup$ @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 ;) $\endgroup$
    – penelope
    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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.