1 | initial version |
Steps
1) Just scan through the whole image and make all pixels which are not of color blue to zero and others to white(255,255,255).
2) Convert to gray
3) Then do a cca.
Don't convert the image to gray scale right from start. You will loose the color information.
2 | No.2 Revision |
Steps
1) Just scan through the whole image and make all pixels which are not of color blue to zero and others to white(255,255,255).
2) Convert to gray
3) Then do a cca.
Don't convert the image to gray scale right from start. You will loose the color information.
Here is the output of the algorithm
This is the extraction of blue rectangles using inRange method
This shows the green bounding boxes drawn over the original image
The code is given below.It is in C++
Mat image;
// read the input image
image = imread("D:\\work\\Forums\\opencv\\omr.png");
// get the blue rectangles
Mat blIm;
Scalar lower(180, 30,30);
Scalar upper(240, 100, 100);
inRange(image, lower, upper, blIm);
imshow("blue image", blIm);
// apply a connected component analysis with statistics
// (from opencv 3.0 onwards)
// cca
Mat labels, stats, centroids;
int numObjects = connectedComponentsWithStats(blIm, labels, stats, centroids);
// Draw bounding boxes for objects in the original image
// exclude background = 0
int i;
for(i=1; i<numObjects; i++)
{
int left = stats.at<int>(i, CC_STAT_LEFT);
int top = stats.at<int>(i, CC_STAT_TOP);
int width = stats.at<int>(i, CC_STAT_WIDTH);
int height = stats.at<int>(i, CC_STAT_HEIGHT);
// draw rectangle
rectangle(image, Rect(left, top, width, height), Scalar(0,255,0), 2);
}
imshow("BoundingBoxes", image);