# How to Detect Color of a detected blob?

Hi im new with opencv, currently i need to detect a beans from a live feed camera, im able to detect it with SimpleBlobDetector function. Now i want to know the average color inside of the blob to do some calculation. What is the best way to do it?

``````            Mat im_with_keypoints;
drawKeypoints( capture, keypoints, im_with_keypoints, Scalar(0,0,255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS );
size_t i, k;
Point Coordinate;
for( i = k = 0; i < keypoints.size(); i++ )
{
Coordinate =    keypoints[i].pt ;
qDebug ()<< "x " << Coordinate.x << "y " <<Coordinate.y;
qDebug ()<< "s " << keypoints[i].size ;

}
``````

This is my Code to detect the coordinate of each blob and the diameter of it

since you already have the location and the size, you can sample the mean from a rectangular crop, centered around the keypoint position:

`````` Point p = keypoints[i].pt;
int w = keypoints[i].size;
Rect r(p.x-w/2, p.y-w/2, w, w);
Scalar m,d; // mean and stddev
meanStdDev( img(r), m, d);``````
1 Like

Thanks for the reply this help me alot! I tried to change a little bit of the code

``````              Mat im_with_keypoints;

drawKeypoints( capture,  keypoints, im_with_keypoints, Scalar(0,0,255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS );
size_t i, k;
Point Coordinate;

for( i = k = 0; i < keypoints.size(); i++ )
{
Coordinate =    keypoints[i].pt ;
Rect r(Coordinate.x-(keypoints[i].size /2), Coordinate.y-(keypoints[i].size/2), keypoints[i].size, keypoints[i].size) ;
Mat roi(im_with_keypoints, r);
cv::Scalar mean;
mean = cv::mean(roi);
qDebug() << "Blue " << mean[0] ;
qDebug() << "Green " << mean[1] ;
qDebug() << "Red " << mean[2] ;
}
``````

This code above also the same right? also i have another question, if i use cvtColor to change the color from BGR to HSV is it better to detect the color or it will be just the same with BGR color?

yep, looks like

please explain the reason to do so.

no, results will not be the same, itâ€™s another colorspace

please explain the reason to do so.
I read some journal that said its better to do image processing in HSV color space, is this also work in my case?

sure, but you still have to explain â€śmy caseâ€ť, please. which is ? what are you trying to do ?

is it still: " detect a beans" ? if so, you might profit from doing it in HSV space, since most likely you only have to look at hue for classification (not 3 r,g,b vaues)

Yess its still about detecting the beans. Since the result of the color is not exactly the same with the real color but i know there is a factor from my camera and lighting as well, but maybe i can optimize it if i change the color space, maybe?

using hsv will make it less sensitive against variation in lighting, so try if you can exploit it ;9

Okay one last question if you dont mind
` cvtColor(capture, im_with_keypoints, COLOR_BGR2HSV);`

i just to need to do this right to change it?
i dont need to change other line?

you probably do not want to use `im_with_keypoints` for this

so i make a new output then use it for my input in drawKeypoints?

yep, exactly â€¦

(actually, drawing the keypoints is irrelevant for the detection, itâ€™s just visualization for humans)

1 Like

ohh okayyy got it, thank you so much for your help !