Ask Your Question
0

union of contours opencv

asked 2017-02-22 18:59:54 -0600

alexander33 gravatar image

I found the contours of one image that content numbers, then I used boundingrect for find the rectangles of those contours the result is:

original img and result

I would know how to unite the final contours for then recognize the letters. the result that i want its something like this:

result that i want

some sugestion for resolve my problem. Thanks"!!!!

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2017-02-23 10:08:30 -0600

pklab gravatar image

I would suggest morphological operator to close the gap before to find contour.

In this case you can use MORPH_CLOSE with a vertical kernel where height >= as distance you want to connect.For example the code below produces this result

Morph Close image description than contour image description

Mat src,dst,bw,connected;
src= imread("../img/numbers.png");
inRange(src, Scalar(200,200,200), Scalar(255,255,255), bw);
// define your kernel as vertical line
int maxDistance = 4; //as distance you want to connect
Mat kernel = Mat(Size(3, maxDistance), CV_8UC1, Scalar(0));
//vert line in the middle
line(kernel, Point(kernel.cols/ 2, 0), Point(kernel.cols / 2, kernel.rows), CL_WHITE, 1);
cv::morphologyEx(bw, connected, MORPH_CLOSE, kernel);
imshow("Connected Numbers", connected);
vector<vector<Point>> contours;
findContours(connected, contours, RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
for (size_t i = 0; i < contours.size(); i++)
{
    Rect rr = boundingRect(contours[i]);
    rectangle(src, rr, CL_GREEN, 1);
}
imshow("Connected Rects", src);
waitKey(0);

do not use too much height because you will close "5" too

edit flag offensive delete link more

Comments

Thanks a lot for the response, works very fine.

alexander33 gravatar imagealexander33 ( 2017-02-23 11:40:46 -0600 )edit
0

answered 2017-02-23 01:16:13 -0600

berak gravatar image

cv::Rect has union('|') and intersection('&') operators. you could use them like this:

Rect r1(5,5,10,10);       //[10 x 10 from (5, 5)]
Rect r2(16,16,10,10);     //[10 x 10 from (16, 16)]
Rect r3 = r1 | r2;        //[21 x 21 from (5, 5)]
edit flag offensive delete link more

Comments

thanks for the response.

alexander33 gravatar imagealexander33 ( 2017-02-23 11:41:08 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-02-22 18:59:54 -0600

Seen: 2,344 times

Last updated: Feb 23 '17