Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

detcting colour of blobs

I am making a project that detects circles in a real time image and then classifies the circle as red or green. My approach towards the problem is that first i make a saliency map of the original image then i apply simple blob detector on that saliency map by setting suitable parameters.Hence the circles are detected as blobs .Then i extract the coordinates of the centre of th blob and go to that coordinate in the original 3 channel image , if the pixel value in the red channel is more than that in green it is classified as red otherwise green,but this logic fails to work i dont get why my code:

 int main()
 {


// Read image
Mat org=imread("32.jpg");
Mat im = imread( "sal12.jpg", IMREAD_GRAYSCALE );
Size s(400,300);
resize(im,im,s);
resize(org,org,s);
imshow("original",org);
imshow("saliency map",im);





// Setup SimpleBlobDetector parameters.
SimpleBlobDetector::Params params;

// Change thresholds
params.minThreshold = 10;
params.maxThreshold = 256;

// Filter by Area.
params.filterByArea = true;
params.minArea =70;
params.maxArea=1000;

// filter my min distance
//params.minDistBetweenBlobs=100;

// Filter by Circularity
params.filterByCircularity = true;
params.minCircularity = 0.8;

// Filter by Convexity
params.filterByConvexity = true;
params.minConvexity = 0.5;

// Filter by Inertia
params.filterByInertia = false;
params.minInertiaRatio = 0.01;

//filter by colour
params.filterByColor=false;
params.blobColor=255;

// Storage for blobs
vector<KeyPoint> keypoints;



// Set up detector with params
Ptr<SimpleBlobDetector> detector = SimpleBlobDetector::create(params);   

// Detect blobs
detector->detect( im, keypoints);

//the total no of blobs detected are:
int x=keypoints.size();
cout<<"total no of circles detected are:"<< x <<endl;
cout<<endl;
        int green=0;
    int red=0;
for(int i=0;i<x;i++)
{  

   //location of first blob
    Point2f point1=keypoints.at(i).pt;
    int x1=point1.x;
    int y1=point1.y;

    float R=(float)org.at<Vec3b>(x1,y1)[1];
    float B=(float)org.at<Vec3b>(x1,y1)[0];
    float G=(float)org.at<Vec3b>(x1,y1)[2];
    cout<<"location of "<<i+1<<" blob is "<<x1<<","<<y1<<endl;
    cout<<" blue channel pixel value at the location is"<<(float)org.at<Vec3b>(x1,y1)[0]<<endl;
    cout<<" green channel pixel value at the location is"<<(float)org.at<Vec3b>(x1,y1)[2]<<endl;
    cout<<" red channel pixel value at the location is"<<(float)org.at<Vec3b>(x1,y1)[1]<<endl;
    if (R>G)
    {
            cout<<" THE BLOB IS RED"<<endl;
           red++;
    }
    if(G>R)
    {
        cout<<"THE BLOB IS GREEN"<<endl;
        green++;
    }
    //cout<<"hue value : "<<(int)hsv.at<Vec3b>(x1,y1)[0]<<endl;
    float size=keypoints.at(i).size;
    cout<<"size of the blob is : "<<size<<cout<<endl;
    cout<<endl;



}

cout<<"no of green circles: "<<green<<endl;
cout<<"no of red circles :"<<red <<endl;
 //Draw detected blobs as red circles.
//DrawMatchesFlags::DRAW_RICH_KEYPOINTS flag ensures
// the size of the circle corresponds to the size of blob
Mat im_with_keypoints;
drawKeypoints(org, keypoints, im_with_keypoints, Scalar(0,0,255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS );

}