Ask Your Question
1

Include multiple objects in one contour

asked 2014-09-01 14:08:33 -0600

cmc gravatar image

updated 2014-09-02 01:37:11 -0600

berak gravatar image

I have a problem and I am not sure how to accomplish it. I am working with binary Mat objects that contain many small objects in them. I have used findContours to generate each objects contour and used contourArea to calculate their area. If the objects are too small they are removed from the image. The next step tests for the distance between contours, if the contours are close enough they should be grouped together when their contour is drawn. The function works up until the objects need to be grouped into one contour. After a lot of reading and trying things out I am unable to figure this out. If any one is able to provide guidance I would appreciate it.

Here is an image for what I would like the output to be:image description

The white blobs are the objects and the red line is my desired contour.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
2

answered 2014-09-02 02:30:15 -0600

Oscanto gravatar image

Did you try using convexHull()?

It finds the convex hull of a point set, I guess if you can "union" the contours (which are vector of points) that are close enough, and then apply this function, it can do the job.

edit flag offensive delete link more

Comments

How would I "union" the contours?

cmc gravatar imagecmc ( 2014-09-02 12:12:34 -0600 )edit

Just insert every points of one contour into the other one.

Oscanto gravatar imageOscanto ( 2014-09-02 13:45:23 -0600 )edit

I made an image with two objects. I got the two contours then used a for loop to add the points in the second contour to the first using .push_back. This did not work and caused the following error: OpenCV Error: Assertion Failed (0 <= i && i < (int)vv.size()) in cv::_InputArray::getMat, file C:\builds\2_4_PackSlave-win64-vc12-shared\opencv\modules\core\src\matrix.cpp, line977. I'm not sure what I'm doing wrong and have been stuck here for days. I'd appreciate anymore advice you can offer.

cmc gravatar imagecmc ( 2014-09-02 14:28:12 -0600 )edit

Can you provide more information ? When exactly are you issuing this error? When calling the push_back() function ?

Oscanto gravatar imageOscanto ( 2014-09-02 15:34:44 -0600 )edit

No, the push_back function seems to work. When I print out the cv::Points in the contour it shows the coordinates from both contours, so they seem to be added together. However, when I attempt to get the convexHull is when the error is caused. This is the code:

vector<Point> hull; convexHull(Mat(contour), hull, false); drawContours(img, hull, 0, Scalar(255), 1, 8, vector<Vec4i>(), 0, Point());

The error occurs on the convexHull line.

cmc gravatar imagecmc ( 2014-09-02 15:45:51 -0600 )edit

Can't you just use contour instead of Mat(contour) in convexHull ? i.e. convexHull(contour, hull, false) Can you also check that the error is not coming from the instruction "Mat(contour)"

Oscanto gravatar imageOscanto ( 2014-09-02 15:49:59 -0600 )edit

Tried convexHull(contour, hull, false); and got a new error: OpenCV Error: Assertion Failed (i < 0) in cv::_InputArray::getMat, file C:\builds\2_4_PackSlave-win64-vc12-shared\opencv\modules\core\src\matrix.cpp, line963.

cmc gravatar imagecmc ( 2014-09-02 15:59:55 -0600 )edit
0

answered 2014-09-01 15:55:36 -0600

densvr gravatar image

try to use cv::dilate() function before finding contours

edit flag offensive delete link more

Comments

Thanks for the comment. I already tried using dilate but it was not an acceptable approach as maintaining the edges of the objects vital.

cmc gravatar imagecmc ( 2014-09-01 16:27:40 -0600 )edit

Question Tools

Stats

Asked: 2014-09-01 14:08:33 -0600

Seen: 2,275 times

Last updated: Sep 02 '14