Color and circle detection in image
There is no need to Dilate. Here is my segmentation results:




And here is my code using OpenCV:

#include &lt;iostream&gt;
using namespace std;
#include &lt;cv.h&gt;
#include &lt;cxcore.h&gt;
#include &lt;highgui.h&gt;
using namespace cv;

int main()
{
        int ht,wt;
    IplImage *im = cvLoadImage("2Dpg6.jpg");
        ht=im-&gt;height,wt=im-&gt;width;
    cvShowImage("RGB",im);

    IplImage *img = cvCreateImage(cvGetSize(im),8,1);
    cvCvtColor(im,img,CV_RGB2GRAY);

    CvRect rec;
    CvSeq *contours;
    CvMemStorage *storage=cvCreateMemStorage(0);
    IplImage *img2 = cvCreateImage(cvGetSize(im),8,1);

    cvThreshold(img,img2,30,255,CV_THRESH_BINARY);  // image segmentation
    cvErode(img2,img2,NULL,7);  // erode
    cvShowImage("BW",img2);  // show black-wihte image
    cvFindContours(img2,storage,&amp;contours,sizeof(CvContour),CV_RETR_CCOMP);  // find contours
    while(contours){
        rec = cvBoundingRect(contours,1);
        cvRectangleR(img,rec,cvScalar(255));
        contours=contours-&gt;h_next;
    }
    cvShowImage("Gray",img);

    cvWaitKey(0);
    return 0;
}