Color and circle detection in image I try to detect and count circles in image (for example smarties)


I use HSL color space. But I am not able to distiguish colors od the same color if they are in touch. I try to erode and dilate picture. But result is the same, I have only some blobs(connected components) od the same color. Do you have some general algorithm for this problem?

(I try to do that with EMGUCV library)

Thanks in advance.
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;
}