1 | initial version |
Please find the code below
Mat image;
// read the input image
image = imread("D:\\omr.jpg");
Mat gray;
cvtColor(image, gray, CV_BGR2GRAY);
GaussianBlur(gray, gray, Size(5,5) , 0, 0);
Mat thres;
threshold(gray, thres, 130, 255, CV_THRESH_BINARY_INV);
imshow("thres", thres);
waitKey();
// apply a connected component analysis with statistics
// (from opencv 3.0 onwards)
// cca
Mat labels, stats, centroids;
int numObjects = connectedComponentsWithStats(thres, 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);
double width = stats.at<int>(i, CC_STAT_WIDTH);
double height = stats.at<int>(i, CC_STAT_HEIGHT);
int area = stats.at<int>(i, CC_STAT_AREA);
double k = width/height;
if(area > 500 && area < 2000 && k > 0.9 && k < 1.1)
{
// draw rectangle
rectangle(image, Rect(left, top, width, height), Scalar(0,255,0), 2);
}
}
imshow("BoundingBoxes", image);
The output image
Regards
Amal
2 | No.2 Revision |
Please find the code below
Mat image;
// read the input image
image = imread("D:\\omr.jpg");
Mat gray;
cvtColor(image, gray, CV_BGR2GRAY);
GaussianBlur(gray, gray, Size(5,5) , 0, 0);
Mat thres;
threshold(gray, thres, 130, 255, CV_THRESH_BINARY_INV);
imshow("thres", thres);
waitKey();
// apply a connected component analysis with statistics
// (from opencv 3.0 onwards)
// cca
Mat labels, stats, centroids;
int numObjects = connectedComponentsWithStats(thres, 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);
double width = stats.at<int>(i, CC_STAT_WIDTH);
double height = stats.at<int>(i, CC_STAT_HEIGHT);
int area = stats.at<int>(i, CC_STAT_AREA);
double k = width/height;
if(area > 500 && area < 2000 && k > 0.9 && k < 1.1)
{
// draw rectangle
rectangle(image, Rect(left, top, width, height), Scalar(0,255,0), 2);
}
}
imshow("BoundingBoxes", image);
The output image
Here is the code using contours. I hope you are comfortable using it in Java.
Mat image;
// read the input image
image = imread("D:\\omr.jpg");
Mat gray;
cvtColor(image, gray, CV_BGR2GRAY);
GaussianBlur(gray, gray, Size(5,5) , 0, 0);
Mat thres;
threshold(gray, thres, 130, 255, CV_THRESH_BINARY_INV);
imshow("thres", thres);
waitKey();
vector<vector<Point> > contours;
// find contours
findContours(thres, contours, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
for( int i = 0; i < contours.size(); i++ )
{
Rect brect = boundingRect(Mat(contours[i]));
double k = (brect.width+0.0)/brect.height;
if(brect.area() > 500 && brect.area() < 2000 && k > 0.9 && k < 1.1)
{
RotatedRect minRect = minAreaRect( Mat(contours[i]) );
// rotated rectangle
Point2f rect_points[4];
minRect.points( rect_points );
for( int j = 0; j < 4; j++ )
line( image, rect_points[j], rect_points[(j+1)%4], Scalar(0,255,0), 2, 8 );
}
}
imshow("BoundingBoxes", image);
Regards
Amal