# Getting blobs from similar colored contours in grayscale image

I want to select the staff lines as individual blobs in either of these images:

Is this possible?

• May be you should add 1 more image marked by the regions you want to detect! It is very difficult to understand now:( – Balaji R Jun 4 '15 at 5:25
• The regions should be the 5 staff lines. – John Jun 4 '15 at 5:31
• What exactly is staff line? Can you make some marking in paint & upload it? – Balaji R Jun 4 '15 at 5:33
• The regions that I want to select are shown in this image (roughly, I selected them manually): i.imgur.com/4bpZLNJ.png It's ok if other blobs are getting selected too (I can filter them by their area afterwards). – John Jun 4 '15 at 5:40
• Is it your problem solved? Please mark as answer if this solves your problem. – Balaji R Jun 11 '15 at 6:04

## 1 Answer

Here is a simple steps in c++ to identify the Objects you want to detect! Hope this solves your problem!

void main()
{
Mat mSource_Bgr,mSource_Gray,mSmoothed,mEdge,mThres;
mSource_Bgr= imread(FileName_S.c_str(),IMREAD_COLOR);
cvtColor(mSource_Bgr,mSource_Gray,COLOR_BGR2GRAY);

medianBlur(mSource_Gray,mSmoothed,5); /*Just to smooth the Edges*/
/*This is for Automatically calculating the Canny Threshold values*/
double CannyAccThresh = threshold(mSmoothed,mThres,0,255,CV_THRESH_BINARY|CV_THRESH_OTSU);
double CannyThresh = 0.1 * CannyAccThresh;

Canny(mSmoothed,mEdge,CannyThresh,CannyAccThresh);

Mat kernal_E,kernal_D;
int erosion_size=2,dilation_size=2;
kernal_E= getStructuringElement( MORPH_RECT,Size( 2*erosion_size + 1, 2*erosion_size+1 ),
Point( erosion_size, erosion_size ) );
kernal_D= getStructuringElement( MORPH_RECT,Size( 2*dilation_size + 1, 2*dilation_size+1 ),
Point( dilation_size, dilation_size ) );
dilate(mEdge,mEdge,kernal_D);//Just to make sure all the Edges are closed
erode(mEdge,mEdge,kernal_E);

imshow("mCanny_Edge",mEdge);
imwrite(FileName_S+ "mCanny_Edge.bmp",mEdge);


    vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
/// Find contours
findContours( mEdge.clone(), contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
/// Approximate contours to polygons + get bounding rects and circles
vector<vector<Point> > contours_poly( contours.size() );
for( size_t i = 0; i < contours.size(); i++ )
{
approxPolyDP( Mat(contours[i]), contours_poly[i], 1, true );
}

for (int i = 0; i < contours_poly.size(); i++)
{
if(contours_poly[i].size()>100)//Just a filter You can add more here
drawContours(mSource_Bgr,contours_poly,i,Scalar(0,255,0),2);
}
imshow("mResult",mSource_Bgr);


}