Ask Your Question
0

detcting colour of blobs

asked 2017-02-21 09:55:00 -0500

shreya gravatar image

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 );

}

edit retag flag offensive close merge delete

Comments

somehow all the blobs are detected as red even green ones

shreya gravatar imageshreya ( 2017-02-21 09:59:05 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-02-21 14:27:24 -0500

LBerger gravatar image

updated 2017-02-23 08:03:10 -0500

In opencv color images are BGR blue(channel 0) Green (channel 1) Red (channel 2)

float R=(float)org.at<Vec3b>(y1,x1)[1]; SHOULD BE 2
float B=(float)org.at<Vec3b>(y1,x1)[0];
float G=(float)org.at<Vec3b>(y1,x1)[2];// SHOULD BE 1
cout<<"location of "<<i+1<<" blob is "<<x1<<","<<y1<<endl;
cout<<" blue channel pixel value at the location is"<<(float)org.at<Vec3b>(y1,x1)[0]<<endl;
cout<<" green channel pixel value at the location is"<<(float)org.at<Vec3b>(y1,x1)[2]<<endl; // SHOULD BE 1
cout<<" red channel pixel value at the location is"<<(float)org.at<Vec3b>(y1,x1)[1]<<endl;// SHOULD BE 2
edit flag offensive delete link more

Comments

i tried that too that doesn't work either and even if that was the problem green blobs would be detected as red and red as green ones but all the blobs were detected as red

shreya gravatar imageshreya ( 2017-02-22 06:06:46 -0500 )edit

sorry I miss an another error. In method at first argument is row and second column

LBerger gravatar imageLBerger ( 2017-02-23 08:04:52 -0500 )edit

thanks a lot

shreya gravatar imageshreya ( 2017-02-23 08:26:12 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2017-02-21 09:55:00 -0500

Seen: 1,964 times

Last updated: Feb 23 '17