Ask Your Question

Revision history [back]

click to hide/show revision 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.

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

image description

This is the extraction of blue rectangles using inRange method

image description

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